Posts Tagged ‘null pattern’

The null object pattern in use

April 16, 2011 Leave a comment

What is the null object pattern and why should you care? Well, if you’re a masochist who likes to add superfluous validation code to any give project or if you are an obsesive compulsive about adding such superfluous code… then I suppose you wouldn’t care. However, for the rest of us this can be a quite useful design pattern that can lead to simpler code that is more maintaianable and better testable. That covers the why, so here is the what in a nutshell.

Design patterns provide an architectural or algorithmic set of steps or procedures to solve specific programming design issues. This design pattern addreses the common issue with .NET development where an instance of a class, or a reference object, is tested for having a null reference before using the object in code. Why would you do this in the first place? Consider that if you attempt to access a method or a property on a reference object that is null (or Nothing in VB.NET) you will encounter a nasty NullReferenceException. This is not the type of error you would want to introduce to your user, but even if you are savvy enough to catch such an error before it crashes your application you are still posed with the issue of restoring the normal flow of operation for that application in light of this unexpected error. Read more…