Jeremy D. Miller -- The Shade Tree Developer

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
The Null Pattern is a Good Thing

That's all I've got to say this morning.  Check it our here:  http://en.wikipedia.org/wiki/Null_Object_pattern


Posted 08-18-2008 9:13 AM by Jeremy D. Miller

[Advertisement]

Comments

Kyle Szklenski wrote re: The Null Pattern is a Good Thing
on 08-18-2008 12:52 PM

Funny. For some reason, it always seemed like the *ultimate* in degenerative cases to have an object with no method bodies/default method bodies. Having said that, I've used it all over the place due to its usefulness!

Bryan Watts wrote re: The Null Pattern is a Good Thing
on 08-18-2008 1:19 PM

Let's say I wanted to have a conversation with someone. I call them over to my desk, and start talking. If they show up, everything is fine. If I talk to thin air, my coworkers are going to question my sanity.

The absence of an object when its presence is required is an exceptional situation. It means you can't do the work you planned on because one of your prerequisites is not met.

Martin Fowler suggests a "Special Case" object. That makes me laugh for 2 reasons:

1. Instead of repetitive null-checking, you have repetitive derivations. The problem didn't go away, it just takes more typing and artifacts to express it. This approach cascades all the way down every single inheritance hierarchy and could have just been solved with a simple null-check.

2. What will NullCustomer.getPlan() do if not throw an exception? Will it return a NullPlan, which in turn returns other derivations?

At some point, you have to address the absence of your target object. If that is not done on the *first* operation, in my opinion that violates the Principle of Least Surprise.

Bryan Watts wrote re: The Null Pattern is a Good Thing
on 08-18-2008 1:37 PM

Updated the article to remove the obvious bias.

Jeremy D. Miller wrote re: The Null Pattern is a Good Thing
on 08-18-2008 2:12 PM

@Bryan,

I guess that's your point of view.  For me it's often a matter of using a nullo object that does nothing or putting in if/then logic.  My vote is that the nullo object gives you cleaner code and fewer problems.  The nullo is generally there for optional activities.

Joe wrote re: The Null Pattern is a Good Thing
on 08-18-2008 4:19 PM

Except for optional activities,  I would agree with Bryan.

Ben wrote re: The Null Pattern is a Good Thing
on 08-18-2008 4:44 PM

Here here

Dave Newman wrote re: The Null Pattern is a Good Thing
on 08-18-2008 11:48 PM

Having null be a first class object in the language would be even better!

Ah ruby

Jeremy Gray wrote re: The Null Pattern is a Good Thing
on 08-19-2008 12:11 AM

Conditionals are a code smell. Null/missing object pattern is a very nice way to eliminate a certain subset of said smell.

HMK's Spurious Thoughts wrote The Null Pattern is a Good Thing
on 08-19-2008 4:40 AM

Great reminder: The Null Pattern is a Good Thing....

Kent Boogaart wrote re: The Null Pattern is a Good Thing
on 08-19-2008 8:02 AM

@Jeremy G: Conditionals are a code smell? Really? How long, then, until code is a code smell? ;)

Like Joe, I am of the opinion that a nullo is good for an optional object. I just think of it as the nullo instance being the default state of the field rather than null being the default (almost like it's a value type rather than a ref type). Providing a nullo for non-optional dependencies seems dangerous to me.

Scott wrote re: The Null Pattern is a Good Thing
on 08-19-2008 5:33 PM

Yes, it's a great thing. I did a series of posts on it (geekswithblogs.net/.../null-object-pattern.aspx and geekswithblogs.net/.../null-object-pattern-follow-up.aspx as well.

Nullability really should be a first class language citizen where the . operator is lifted so that calling a method or property on a null object simply returns null as well.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?