views:

264

answers:

2

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()?

+4  A: 

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();
}
Tanzelax
I agree with your comments and Carl's below. It think it is overly paranoid, complicates the code, slows code reviews, etc. The issue is defending against the 'what-if' argument: "What if 'something' happens in production and we crash because we didn't check for null?How would you respond to that?
TERACytE
@TERACyTE: Well, adding code branches for null states when they're not needed also adds complexity which is harder to maintain. The idea behind not-checking-for-null is pretty separate from the null object pattern, which is about using null as a by-design regular-occurance and treating it more as an exceptional case.
Tanzelax
(editted the post with an example) The idea here is that if things can still be null, you still need to check for nulls. However, by taking possible **sources** of those nulls, you make code branches more maintainable.
Tanzelax
I appreciate your feedback. Thank-you.
TERACytE
A: 

With the second implementation, is it not still a good practice to check that it is not null before calling A.Method()?

No. If you know that m_A is not null, then the check is superfluous; it's an example of paranoid coding. What harm does it do? It complicates your code - unnecessarily; it makes it harder to read, harder to debug.

Carl Manaster