Rash thoughts about .NET, C#, F# and Dynamics NAV.

"Every solution will only lead to new problems."

Wednesday, 3. April 2013

A tale of nulls – part I

Filed under: F#,Informatik — Steffen Forkmann at 17:43 Uhr

Triggered by a short tweet by Tomas Petricek and a blog post by Don Syme I had a couple of conservations about null as a value, Null as a type (Nothing in Scala), the null object pattern and the Maybe monad (or Option type in F#). I decided to share my opinion on this topic in order to help others to make their own opinion.

Part I – Null as a value

Unfortunately most programming languages have some kind of null pointer. We are now even at a point where some people claim that Tony Hoare’s famous billion dollar mistake costs actually a billion dollar per year.

But before we start to think about solutions to the null pointer problem let us first see where null values might come in handy.

Uninitialized values

In a statement oriented language like C# it is sometimes not that easy to represent uninitialized state.

As you can see we forgot to initialize peter in the missing "else-branch" which might lead to a NullReferenceException. There are a couple of things we can do to prevent this from happening. First of all we should omit the explicit initialization with null:

In this case the C# compiler throws an error telling us "Use of unassigned local variable ‘peter’".

In expression oriented languages like F# the problem doesn’t exist. We usually write initializations like this:


There is no way to forget the else branch since the types don’t match.

Unknown values

Consider a search function which tries to find a Person in a list:

Unknown values are probably the most common cause for the introduction of nulls and in contrast to uninitialized values they have much more global effects.

Solutions to unknown values

1. Using explicit exceptions

One easy solution to get rid of the nulls is to make the exception explicit:

One benefit here is that we have a concrete exception which is telling us what went wrong. Unfortunately nothing forces us to handle this exception in the calling method. This means the program can still crash. Another problem is that if we use try/catch as a control flow statement (i.e. use it very very often) then we might slow down our program.

And there is still a philosophical question: is it really an exception if we don’t have a person in our repository?

2. Using out parameters and returning bools

If we want to make the result of our search a little more explicit we can also introduce a boolean return value:

This is a pattern which is actually used everywhere in the .NET framework, so it must be good right?!

Just a simple question here: What did we gain exactly? If we forget to check for nulls why would we remember to check the bool?

There is another interesting observation here. Due to the introduction of out parameters we also introduce the uninitialized value problem in every calling method, which is bizarre.

3. Null object pattern

“In object-oriented computer programming, a Null Object is an object with defined neutral ("null") behavior. The Null Object design pattern describes the uses of such objects and their behavior (or lack thereof).”


So obviously this makes only sense in a context where objects have observable behavior. So let’s add some behavior to our Person class:

Let’s look at this solution for a moment.

  • Pro: Implementation of missing behavior is trivial
  • Pro: We don’t introduce null values –> we won’t get NullReferenceExceptions
  • Pro: The name of the class carries information about the reason for the null value
  • Cons: In order to to get rid of the Person(string name, int age) constructor in the null-object we had to introduce an interface
  • Cons: In order to use the interface we had to modify the Person class. This might be a real problem if we don’t have access to the source. We would need some kind of ad-hoc polymorphism like Haskell’s type classes, Scala’s Traits or Clojure’s Protocols (read more about the expression problem)
  • Cons: Everything we want to do with Persons has to be part of the IPerson interface or we have to fall back to explicit runtime type tests, because there is no way to get to the data:


  • Cons: Without ad-hoc polymorphism the Person class is getting more and more complex over time since we are adding all the behavior to it. Compare it with our starting point where it was a simple data representation. We totally lost that.
  • Cons: Errors/bugs might appear as normal program execution. [see Fowler, Martin (1999). Refactoring pp. 261]

Don’t use this approach it’s really not a good solution.

4. The Option type (a.k.a. Maybe monad)

The option type is a way to push the idea from 2. “returning bools” into the type system. There are lots of tutorials about the option type on the web, but for now it’s enough to know that it is something which is based on the following idea:

In order to allow pattern matching in C# we introduce a small helper:

With this idea we can get rid of all null values in the program, we don’t have to introduce an IPerson interface, the null case is type checked and the best thing is it’s really easy to create functions like IsOlderThan12:

Please don’t implement the option type yourself. Even in C# it’s much easier to just reference FSharp.Core.dll and use FSharpx (read 10 reasons to use the F# runtime in your C# app).

Of course this is only the tip of the iceberg. If you read more about the option type you will see that its monadic behavior allows all kinds of awesome applications (e.g. LINQ, folds, map, …).

5. The Either type

One of the benefits of the null object pattern above was that it allows to encode the reason for the missing value. In some cases we need the same thing for the maybe monad. So let’s try to capture the reason in the None case:

Now we can use this in the TryFindPerson method:

As with the option type please don’t rewrite this stuff yourself. There is a Choice type in F# which helps to capture this idea and Mauricio has a couple of blog posts which explain how to use it from C# (of course FSharpx is helping here again).

6. The (singleton) list monad

The option type captures the idea that we can either have one value or nothing. We can do the same thing with List<T>:

Of course this uses only a small subset of the power of IEnumerable<T> but I wanted to show the similarity to the maybe monad. You can read more about the “poor man’s option type” on Mauricio’s blog.


We saw a couple of very different solutions to get rid of null values. Normally I’d only use the option type or the either type (if I need to carry the reason). In some very rare situations (e.g. C# only projects) I would also use singleton lists.

I wouldn’t use the other solutions. They are potentially dangerous, especially if your project gets bigger.

I would be happy to read your opinions.

Tags: , ,


  1. Great post!

    I think there’s a typo in the TryFindPerson() – the out person is always null.

    Comment by Vasily Kirichenko — Friday, 5. April 2013 um 15:28 Uhr

  2. Oh the irony. Thanks, fixed.

    Comment by Steffen Forkmann — Friday, 5. April 2013 um 15:37 Uhr

  3. […] Steffen Forkmann blogged “A tale of nulls – part I“. […]

    Pingback by F# Weekly #14, 2013 | Sergey Tihon's Blog — Sunday, 7. April 2013 um 21:01 Uhr

  4. Thank you for the post – this is more or less exactly how I feel about the issue (and way more constructive than our little twitter discussion – yeah those tent to escalade into nonsense pretty quickly – must be a 140character thing).

    Here are my 50cts:

    Why I think the Maybe/Either is much better then the NullObjectPattern:

    IMHO the real problem is that FindPerson is a partial function (and indeed TryFindPerson is much better than FindPerson – both in name and in usage, even throwing an exception does communicate this better – if documented).

    If we use the NullObjectPattern we do not see this – indeed the function is now a total function but at the cost of writing useless stuff (NullPerson.SayName … huh?)

    Maybe (or Option or whatever you like) on the other hand makes it perfectly clear that the function is partial (even without reading it’s name or it’s documentation).
    Even more: now the compiler will help us “not to forget” this.

    You could even go further and hide the internal Value (no .Value property in .net or not exporting the constructors in Haskell – a feat. that (sadly) you cannot do in F#) and force the user to deconstruct with techniques like monads or higher-order functions like map, filter, bind – so you cannot even get a NullReferenceException with using returnedValue.Value (<- FSharpOption will be null here too (see http://msdn.microsoft.com/en-us/library/ee353806.aspx) O.o )

    Just stick with NOOO: "… Types over classes" 😀

    Comment by Carsten — Monday, 8. April 2013 um 6:08 Uhr

  5. BTW: I find the Match function you use there quite interesting – it’s like a blend of you usuall map and defaultArg – what I don’t quite qet is why you use a Func instead of just S for the “default on None” – of course this might come in handy if you want some kind of lazy behaviour in there but if your functions are more or less pure there should be no reason to use this (if you need something lazy/delayed just return Lazy or something)

    I think I have seen this somewhere else before and surely there is a reason I just cannot get to right now – so maybe you can help me.

    (Background: I usually just go with defaultArg but maybe I missuse the pattern here)

    Comment by Carsten — Monday, 8. April 2013 um 6:14 Uhr

  6. […] P.S. Read more tales of null in the Steffen Forkmann’s blog. […]

    Pingback by F# null trick | Sergey Tihon's Blog — Wednesday, 10. April 2013 um 20:35 Uhr

  7. […] einigen Tagen habe ich einen interessanten Artikel von Steffen Forkmann über die Behandlung von Null-Values gelesen. So wurde ich auf die […]

    Pingback by Option-Types & ASP.NET Web API | Blog about Continuous Learning — Friday, 26. April 2013 um 22:41 Uhr

  8. […] einigen Tagen habe ich den Artikel A tale of nulls von Steffen Forkmann über die Behandlung von Null-Values gelesen. So wurde ich auf die […]

    Pingback by Option-Types & ASP.NET Web API | Empathic Business Design — Thursday, 17. April 2014 um 7:01 Uhr

RSS feed for comments on this post. | TrackBack URI

Leave a comment

XHTML ( You can use these tags): <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> .