CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeremy D. Miller -- The Shade Tree Developer

Under the hood and working with .Net, TDD, Software Design, and Agile Stuff

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



Comments

Bryan Watts said:

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.

# August 18, 2008 1:19 PM

Bryan Watts said:

Updated the article to remove the obvious bias.

# August 18, 2008 1:37 PM

Jeremy D. Miller said:

@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.

# August 18, 2008 2:12 PM

Joe said:

Except for optional activities,  I would agree with Bryan.

# August 18, 2008 4:19 PM

Ben said:

Here here

# August 18, 2008 4:44 PM

Dave Newman said:

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

Ah ruby

# August 18, 2008 11:48 PM

Jeremy Gray said:

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

# August 19, 2008 12:11 AM

HMK's Spurious Thoughts said:

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

# August 19, 2008 4:40 AM

Kent Boogaart said:

@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.

# August 19, 2008 8:02 AM

Scott said:

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.

# August 19, 2008 5:33 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Jeremy D. Miller

Jeremy began his IT career writing "Shadow IT" applications to automate his engineering documentation, then wandered into software development because it looked like more fun. Jeremy previously worked as a systems architect building mission critical supply chain software for a Fortune 100 company and learned agile development practices as a .Net consultant at ThoughtWorks, one of the pioneers of agile development. Jeremy is the author of the open source StructureMap (http://structuremap.sourceforge.net) tool for Dependency Injection with .Net and the forthcoming StoryTeller (http://storyteller.tigris.org) tool for supercharged FIT testing in .Net. Jeremy's thoughts on just about everything software related can be found on his weblog "The Shade Tree Developer" at http://codebetter.com/blogs/jeremy.miller, part of the popular CodeBetter site. Jeremy is a Microsoft MVP for C#. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News

All opinions expressed here constitute my (Jeremy D. Miller's) personal opinion, and do not necessarily represent the opinion of any other organization or person, including (but not limited to) my fellow employees, my employer, its clients or their agents.

About Me

"Best Of" Compendium

StructureMap (Dependency Injection for .Net)

StoryTeller (Supercharged Fit)

Build your own Cab

TestDriven

MVP