Declaritive Nullability In Programming Language Constructors

I’ve often thought shallowly about but never had much chance to explore the idea of a new programming language or enhancements to existing programming languages for unit testing. Unit testing has come a long away over the past 5 years but I still feel like there’s a lot of room for improvement.

I’m a big fan of constructor dependency injection because it makes the depdencies of your class crystal clear. If you add a dependency, you have to review all of the code that instantiates instances of your class, which usually is a very good thing. I frequenty add a check in the constructor that throw a null argument exception if the actual parameter passed in is null. Resharper makes this easier, but I’d really like to see a way to do this declaritively, like field nullability in table declarations in SQL DDL. EG, in SQL you have:

CREATE TABLE Blah (counter int not null)

Wouldn’t it be nice to have something like this in a c# constructor?

public SomeClass(notnull IDataAccess access)

Instead of adding a check for null and throwing an exception yourself (or having resharper do it), the compiler would take care of this for you. Also, you wouldn’t need to test that the constructor throws an exception when a null actual parameter is passed in.

Actually, the more I think about this, the more I’d prefer that constructor arguments default to not being nullable and instead have a nullable key word.

Has anyone thought of any other things that you could do to tweak a language to make it more unit test friendly?

6 Responses to “Declaritive Nullability In Programming Language Constructors”

  1. Ilya Ryzhenkov Says:

    ReSharper has null analysis feature also. You can mark parameters with [NotNull]/[CanBeNull] and ReSharper will warn you if you fail to check for null or pass null to [NotNull] parameter.

  2. jopincar Says:

    Cool. I’ll have to check that out. But you’d still need built-in support for this to be bullet-proof because other people not using resharper may call the constructor. I’d also want the run-time check in there.

  3. Scott Allen Says:

    Have you seen the preconditions / postconditions in Spec#? – similar concept

  4. jopincar Says:

    Scott, I just checked it out. Looks very cool. I need to d/l that and play around with it some.

  5. Unhoused Says:

    Somehow i missed the point. Probably lost in translation :) Anyway … nice blog to visit.

    cheers, Unhoused.

  6. jopincar Says:

    The point would be not having to add if (parameter == null) throw new ArgumentNullException… for every parameter of your constructors. A keyword like notnull would allow compile time checking and warnings as well as handle the tedious run-time check.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: