I was happily and randomly tweeting yesterday (as you do) and it occurred to me that I take null for granted. More importantly I am not sure that I understand what the C# languages would look without the idea of a null reference.

Charles Antony Richard Hoare a brilliant British computer scientist apologized for inventing the null reference

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.”

If this was indeed a mistake then a billion dollars maybe underselling it by an order of magnitude or two. Either way I was actually more interested in the part of the quote that eluded to the fact that it easy done this way because it was “easy to implement”.

This led me to ask this question, I am wondering what problems we would create if we did not have null as a programming concept? (I understand some of what we could resolve)”

I am clearly no language designer but it appears that in the absence of a null reference catch all, each class would need to need to implement a “default value” of its own maybe by using a specialized constructor that allows the compiler to know what the equivalent of null looks like for this class.

In C# there already is a default value, for value types this is essentially zero or false, but for reference type we end up back at null. Taking the following code sample would normally create a null object as your class constructor would not be called without using the new keyword.

MyClass mc = null;

I would propose that this (or maybe = default) would call a “null constructor” that defines the equivalent of null for MyClass.

Does this buy us anything? What problems are we missing or even adding?

This whole topic reminds me that every line of code is a decision based on deliberate design or more usually the path of least resistance. We have to consider the cost of these many decisions for the sake of consumers and future developers, while these decision may cost you nothing it may cost others a whole bunch.

Comment Section

Comments are closed.