Jmaxxz.Deet 98.9% null proof

I am happy to introduce Jmaxxz Deet.  The Deet assembly contains a single struct, NotNull. This struct provides a mechanism to formally specify that a reference can NEVER be null. This ability is present in both Spec# and Sing#. However, for those of us still making use of VB, C#, F# and other traditional .Net languages still do not have a mechanism to do so. NotNull brings this capability to all of those languages.

Why would one want to prevent null-able reference types? Well, I would argue that most of the time Null is nothing more than an annoying thing to check for, and the result of poor domain modeling. With NotNull the following code becomes unnecessary:

public void DoSomething(object param1)
if(param1 == null)
throw new ArgumentNullException("param1");
//...finish doing something cool

This code now becomes:

public void DoSomething(NotNull param1)
//...doing something cool and skip the boiler plate code

As a clever developer you may be quick to point out that since it is a struct and thus has a default constructor which I could not possibly override you can create a default instance whose value is null. Fortunately that is not what will happen, because I DID override the default constructor. While C# can not override it one can create and override the default constructor in IL.  Thus if you were to try to call the default constructor you would find the compiler would throw an error as it has been tagged obsolete and set to cause an error instead of a warning.  Even if you were really clever and managed to bypass the compiler error or call it with reflection you would get a runtime exception which would prevent the creation of a default instance of NotNull.


So why in my title did I say it was only 98.9% null proof then? Well it is quite simple. There is a rather evil little method in the .Net runtime called


This method bypasses all constructors and creates a truly uninitialized object. However, calling this method is super risky in and of itself because the resulting objects are in a state which the designers of the object never intended, thus it should not be used.

For more on why null is bad I recommend this video featuring Tony Hoare the creator of ALGOL 60 and self-described inventor of null in modern langueges:


DOWNLOAD Jmaxxz.Deet.NotNull




Previous Post