Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 637737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:39:23+00:00 2026-05-13T20:39:23+00:00

The main goal of the Null Object Pattern is to ensure that a usable

  • 0

The main goal of the Null Object Pattern is to ensure that a usable object is provided to the client. So we want to replace the following code…

void Class::SetPrivateMemberA() {
    m_A = GetObject();
}

void Class::UseA() {
    if (m_A != null) {
        m_A.Method();
    } else {
        // assert or log the error
    }
}

…with this implementation:

void Class::SetPrivateMemberA() {
    m_A = GetObject();
}

void Class::UseA() {
    m_A.Method();
}

The problem I am thinking of is that GetObject() still returns an object, a NULL Object or otherwise. I like the idea of not checking for null repeatedly and trusting that the object sent back is usable, but why wouldn’t I just do that in the first implementation?

Is the advantage of the Null Object pattern just a slight increase in trust to clean up code? With the second implementation, is it not still a good practice to check that it is not null before calling A.Method()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:39:24+00:00Added an answer on May 13, 2026 at 8:39 pm

    You’re correct that, if you’re sure you’re never returning nulls, just skip the null check before calling the method in your first implementation. Likewise, if you do need to do something special in the case that UseA() needs to do something differently on a null object, that you need to explicitly check for a null object anyway. However, what null object pattern really helps with is those situations where it doesn’t really matter.

    Take, for example, most observer patterns. If you implement your observer pattern as a member of your class for which there can only be one observer, and want to announce to the observer that your class did something, it doesn’t matter to the class whether the observer is null or not.

    This is also illustrated with empty container classes, which are essentially the null object pattern: Instead of returning a null container from a query, you simply return an empty container. For things like iterating through all entries of a container, it often won’t matter whether it’s empty or not, so getting rid of the need of a null check makes the code more maintainable/more readable. However, if you want to populate a view of your data set, you still need to explicitly show a different “No entries.” that checks for an empty container.

    Edit for clarity

    One problem is only looking at it from the call site. Like most design patterns, this needs to encompass both sides to be fully utilized. Consider:

    public PossiblyNull GetSomethingNull()
    {
        if (someBadSituation())
            return null;
        else
            return SomehowProduceSomething();
    }
    

    vs

    public PossiblyEmpty GetSomethingEmpty()
    {
        if (someBadSituation())
            return StaticEmptySomething();
        else
            return ProdueSomethingYay();
    }
    

    Now, your call code, instead of looking like

    public void DoSomethingWithChild(Foo foo)
    {
        if (foo != null)
        {
            PossiblyNull bar = foo.GetSomething();
            if (bar != null)
                bar.DoSomething();
        }
    }
    

    it can be

    public void DoSomethingWithChild(Foo foo)
    {
        if (foo != null)
            foo.GetSomething().DoSomething();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Current, I've got a stored procedure that has a main goal of doing a
My main goal is to generate an XML file that can be downloaded. The
My Goal I would like to have a main processing thread (non GUI), and
My goal is to maintain a web file server separately from my main ASP.NET
Goal: Create Photomosaics programmatically using .NET and C#. Main reason I'd like to do
i have a problem with this class. the goal is to make the main
In the main gadget html, there is a div with an onclick that calls
My goal is to build an engine that takes the latest HL7 3.0 CDA
this might be a bit long so my apologies. consider the following code (i've
The main web application of my company is crying out for a nifty set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.