Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the null object pattern worth it?

I was looking at the null object pattern and i am wondering if it worths implementing it or use "if" checks for nulls in my code intsead. As i looked at the implementations it seems hard to keep objects well synchronized with their null implementations. By making changes to the main object we have to check that the null object behaves as expected and its easy to make implementation errors. Isn't it ?

EDIT

Avoid checking for null: http://www.invisible-city.com/sharon/2009/03/null-object-pattern-when-slacker-is.html or http://journalofasoftwaredev.wordpress.com/2008/08/19/null-object-pattern-by-example/

like image 333
George Statis Avatar asked Aug 05 '09 19:08

George Statis


1 Answers

If your object being null is going to be a norm throughout your code, you'd might as well use the pattern. I check for null when an object has not been created and it needs to be instantiated. I do not really use null for anything else, and I certainly don't use it to mean no behavior defined.

If however, you are using null to mean the behavior hasn't been defined yet or the default behavior is nothing, then certainly use the pattern.

like image 85
AlbertoPL Avatar answered Sep 30 '22 13:09

AlbertoPL