End “Null Reference” Now

Stop Nulls

Here’s an interesting statistic, 87.35% of exceptions that occur in the wild are “null reference” exceptions. Here’s another interesting stat, 74.32% of all statistics are made up.

Ok, so I have no idea what the actual percentages are, but my experiance is that “null reference” is hands down the most common exception that I run in to. Well, I’d like to see that put to an end. It’s 2009. We’ve put a man on the moon. We can solve this.

Now I’m no expert when it comes to creating programming languages, but I’d love to see the compiler help out with this. Imagine something like this (notice the attribute in the parameter decleration)…

public void MyNullSafeFunction([NonNull]object value)
{
     // put code here with a smile
     // because you know that "value" can't be null.
}

And then later when some idiot (most likely me) tries to code this…

object value = null;
MyNullSafeFunction(value);

…the compiler would give a nice friendly reminder that I can’t pass a null value as the first argument to MyNullSafeFunction. Infact, there’s no reason it couldn’t even be smart enough to handle this…

object value = null;

if (someVariableThatMyOrMayNotEqualTrue)
{
     value = 4;
}

MyNullSafeFunction(value);

Again, the compiler could warn me that the variable “value” cannot be garanteed to be non-null in all cases. But, what about this case…

object value = FunctionFromLibraryIDoNotHaveSourceCodeFor();
MyNullSafeFunction(value);

Sure, the compiler could analyse the function to verify that it couldn’t possibly return null, but that’s just not very efficent. Especially if that functions return value is dependant on the return value from another function, which is dependant on the return value from another function, which is dependant on the return value from another function, which is dependant on the return value from another function, which is dependant on the return value from another function, which is dependant on the return value from another function. This is where things get harder. But, what if FunctionFromLibraryIDoNotHaveSourceCodeFor had a signature like this…

[NonNull]
public object FunctionFromLibraryIDoNotHaveSourceCodeFor()
{
     // really awesome code here...
}

Ah, problem solved. The compiler could once again verify that the function would not return null in an efficent manner.

However, this is where the problems start to come in. This would require the small task of retro fitting years worth of code with [NonNull] attributes. Also, it could easily end up like the c++ “const” keyword (i.e. really helpful… if everyone uses it). If half the development community adopted the practice and the other half didn’t it could end up being a real nightmare.

However, this isn’t reality land. This is blog-ality land where I can happily ignore the messy details. And so, this marks the begining of my offical campaign to end null references errors forever (I’ll leave working out the details as an excercise for the implementors).

  • Share/Save/Bookmark

Leave a Reply