Null Object Pattern

One of more dangerous patterns that I have seen is Null object pattern. Premise is simple enough: Instead of your function returning null (e.g. when it cannot find an item) it should return special empty object. This avoids null references exception if such object is accidentally used. With simple change to way how we return object we just got rid of crashes. What could go wrong?

Well, someone might implement new functionality a year down the road. Not knowing about this behavior he will check for null in some border line case. Since change is small, no one will do full testing (of course, in real world, any change triggers full retesting :)). His borderline behavior just went from well defined (check for null and take action) to pray that empty object does not get inserted into main program flow.

Exceptions are your friend. That kind of friend that will kick you in the arse when you do something wrong. Having empty object instead of null will indeed stop the crash. However, there is now empty object floating around. Programs are complex and this object is bound to get into wrong place. Best case scenario is that no data gets corrupted.

Null reference exceptions that you would get traditionally are probably among simplest exceptions that you can find in the wild. From stack trace you can see where object is null and just backtrack from there. And probability of data corruption is quite low since program crashed before actually doing anything with affected object. Even if something wrong got inside, crash is quite a clear signal that something is amiss.

Debugging any errors produced by this pattern is not a trivial task. You will probably only notice that something is wrong on data. And you will not notice that error immediately. No, it will be in database for days, weeks if not years until some TPS report exposes it to public. And then you need to find offending code. Talk about needle in haystack...

I view using this pattern as telling someone to kick you in the balls. Maybe there is good reason to take such action, maybe there are even some benefits. Nevertheless there will be some pain involved and one should better be sure that this is really action that is needed.

One thought to “Null Object Pattern”

  1. Although I never used it, it actually makes sense:

    Whole idea behind the null pattern is that most people will forget to check null value and get non-descriptive NullPointerException in the place where they will be unable to figure out from where the null came. In that case it is more of a patch for lack of proper design by contract features (but patching language is one of the main purposes of OO patterns).

    Another application is when you have more than one kind of null, e.g. if you have patient record with “number of pregnancies” field, what is the meaning of null for male and what is the meaning for female patients? It could be either “unknown” or “not applicable” (see excellent Joe Celko’s book “SQL for Smarties”). In this case it makes perfect sense to introduce null objects with addition of “no nulls” checks.

Leave a Reply to Zeljko Trogrlic Cancel reply

Your email address will not be published. Required fields are marked *