>Something I’ve seen a lot in code over the years, no matter which language I happened to be coding in at the time, are checks for Nulls. They end up being scattered through out code for various reasons. The question I have to ask is, with the Null Object Pattern….shouldn’t we be trying to return Null Objects instead of returning NULL when a class is to be returned or a value?
Martin Fowler’s catalog of Refactorings has this as the Introduce Null Object refactoring. I’ve seen it stated as the Null Object Design Pattern as well. Shouldn’t our objects try to be good citizens and not make everybody check for Nulls? The W3C DOM is notorious for returning NULL, and it is one of many reasons it’s not my favorite model to deal with when working with XML. However, this is also spread through out other frameworks as well. It seems we are just shifting the responsibility for our mess from the framework to the implementers that have to deal with all these nulls.
Another excellent resource on this is Tor Norbye’s Code Tip #9.