Due to the amount and breadth of feedback I’ve received regarding this post, I had to write a follow-up to clear the air a bit. After reading this post, continue over to meta-Dear NULL.
To not be or not not to be
This is a quick post, I promise this will not take long.
You’ve certainly made a mess of things, haven’t you?
I would like to know what you really are, but then again, you really aren’t anything. And at the same time, you’re comparable to anything.
Show a little existentialism and do something with yourself.
I find it funny, in a tragic comedic sense, that we must study the semantics of a missing value and how it interacts with an actual value. Although irrelevant for this post, what I find even more funny is when mathematics (such as category theory) provides type constructors for missing values. As if we could actually amplify nothing!
Please note that I am not making a case against NULL, I’m just making some observations.
Well anyways, here is what I stumbled on today that sparked this post:
(I’ve replaced type names to protect myself the innocent)
Given a shirt, I will tell you if it is blue as long as I’ve been given a shirt. Hmmm.
Obviously if the function returns true, you have a blue shirt. What if this function returns false? In that case, your shirt is not blue. Or you do not have a shirt, therefore it cannot possibly be blue. Well which one is it?
Are you laughing yet? If not, answer this question:
Does bigfoot wear a size 32 men's shoe?
If you’re still not laughing, please remember that you must provide a Yes/No answer and either answer carries the implication that bigfoot does exist.
The general problem is that predicates work on subjects. In this example, the subject is Shirt and the predicate is Blue. On the surface it appears we always have a subject, but we do not. We simply have a reference to what could be a shirt. Our subject may not exist, therefore the predicate has nothing to modify (modify here is being used in a grammatical sense).
You might think I’m making a bigger deal out of this than it really is. You might be right. But that does not change the fact that NULL is nothing and anything at the same time. This is a paradox, and one that at least deserves some scrutiny.
The NULL anti-pattern
At this time I would like to introduce the NULL anti-pattern. It is quite simple really, is in the shape of a function, and it look something like this:
There are three ingredients in this anti-pattern:
- The function returns a Boolean value.
- The function has some parameter, p, comparable to NULL.
- The function tests some instance value of p.
The reason I call this an anti-pattern is because the function makes no distinction between an argument exhibiting some behavior and the argument having a value that can exhibit the behavior. In other words, our predicate may not have a subject to modify.
It is not difficult to imagine a more dangerous scenario:
(this is just arbitrary code)
When this function returns true it means the file is in use, and therefore must exist. But what does it mean if the function returns false? A file that exists might not be in use. But does a file that does not exist exhibit any type of interaction with its environment? I think not.
You could argue that we might simply skew our perspective a bit and accept the fact that a single Boolean value might carry more meaning than simply true or false. That’s too shallow an argument however and certainly is not a solution.
We could fix this problem by introducing another Boolean value, which I will call NULL. Ha, I crack myself up sometimes.
There are two reasonable solutions to this problem, perhaps more. Both solutions are quite obvious and you’ve more than likely thought of them already. The first is a simple exception:
The second is an instance method:
I never said this was going to be complicated.
A mountain out of a molehill
I have to imagine that bugs exist due to the NULL anti-pattern. This may be especially true in organization which routinely use multiple languages since there is not much consistency regarding how different languages treat NULL. If you know SQL, you know what I mean.
I might just be at the bottom of the molehill.