Bad Developer, No Twinkie Part 2: Improper use of null values

Null values are okay in certain circumstances.

You may have heard null values called the “billion dollar mistake”  because the amount of problems that they can potentially cause. This is true – using null variables where you shouldn’t be is a bad idea. In general, I avoid them, but there are some cases where they can be handy, and can actually save headaches later on.

Let’s take an example. Suppose that you are writing some sort of application that tracks your personal contacts. For each person, you are tracking a name. email address, phone number, and a birth date. Now suppose you don’t know the birth date for a new contact. If the birth date field is not nullable, you end up having to enter some sort of default value – like January 1, 1900. Later on, you add a feature that reminds you when people’s birthdays are, and you are suddenly inundated with reminds about tons of birthdays on January 1st.

In a case like this, it makes perfect sense to make the birth date field nullable. A null value basically means “I don’t know what this is right now, but I might know later on”.  This does mean that you will have to do proper null checks – especially when displaying things on a UI, but at least now you have an easy way of telling if you actually have the birth date for a given person rather than relying on some magical date which may or may not actually mean what it is.  This sort of thing – using magical default values to mean something that you really don’t know – leads to anger.  And as we all know, anger leads to suffering, which leads to hate, which leads to suffering, and before you know it, you’ve crossed over into the dark side.

To use a more personal example, one project I recently touched had something like this. In a particular piece of data, they were using the value of 0.01 to indicate that they didn’t actually know what the real value was. (In this particular domain, valid values could have ranged from, say, 20 to 200, so seeing a value below 20 is already a bit suspicious, but still).  So why not use a null value?  I have no idea why they didn’t.  But shame on them.

My recommendation for null values is this: start with making everything not-null, then allow nulls as required by business needs.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s