Handling Missing Info in .NET

Hi everyone,

C# 8.0 has brought up a lot of great features and among them there is the ‘Nullable Reference Type’ feature.

Probably the most impactful feature of C# 8.0 is Nullable Reference Types (NRTs). It lets you make the flow of nulls explicit in your code, and warns you when you don’t act according to intent.

Source: https://devblogs.microsoft.com/dotnet/embracing-nullable-reference-types/

They have the ability of drastically improve the code quality of a code base by making explicit areas of code where you expect missing values. As you’re trying to reach for that data, the compiler is flagging the developer at design-time that they’re trying to dereference a potential missing value. This will trigger a warning.

Within the C# type system there is now 2 things in the toolbox for developers to make potentially missing information explicit:

The type system doesn’t although protect fully a developer while they’re developing their approach. You should make sure that the value you’re trying to access isn’t null otherwise it’ll blow up in your face at runtime. One of the shortcomings of the type system is that even though you wrap your data inside either an NRT or an NVT, you can still reach for the data unsafely and cause a lot of problems.

That’s where F# shines greatly. In the native library of the language, we currently have access to a type named ‘Option’.

The option type in F# is used when an actual value might not exist for a named value or variable. An option has an underlying type and can hold a value of that type, or it might not have a value.

The value None is used when an option does not have an actual value. Otherwise, the expression Some( ... ) gives the option a value. The values Some and None are useful in pattern matching, as in the following function exists, which returns true if the option has a value and false if it does not.

Source: https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/options

F# isn’t a niche programming language but a very strong contender for end-to-end application development. In the myriad of great features and tools it provides to developers, the option type excels with the combination of the strict type system of F#. You’re forced to consider all paths of your data. This means that both potentially missing and potentially present must be handle in a match expression.

The option type has functions such as IsSome, IsNone and Value, which allow you to access the “wrapped” value without doing pattern matching. Don’t use them! Not only it is not idiomatic, but it is dangerous and can cause exceptions.

Source: https://fsharpforfunandprofit.com/posts/the-option-type/

In a language like C# or Java, “null” means a reference or pointer to an object that doesn’t exist. The “null” has exactly the same type as the object, so you can’t tell from the type system that you have a null.

Now let’s look at the nearest F# equivalent of the C# example above. In F#, to indicate missing data, you would use an option type and set it to None.

In the F# version, we get a compile-time error immediately. The None is not a string, it’s a different type altogether, so you can’t call Length on it directly. And to be clear, Some [string] is also not the same type as string, so you can’t call Length on it either!

So if Option<string> is not a string, but you want to do something with the string it (might) contain, you are forced to have to pattern match on it (assuming you don’t do bad things as described earlier).

The power that gives you as a developer shouldn’t be quickly disregard. As we’re developing any kind of applications, it kind of blows to go back on a feature because a missing test case or because you’ve missed coverage in one of your test. Having a type system to catch silly errors and provide you compile-time errors for things you’d catch usually at runtime makes a huge difference in both delivered quality and productivity to release your software.

Although F# can be seen as a tool for data scientists or for domain modelling, it’s a general-purpose language and it really does great things, at the sake of repeating myself. I invite you to take a look at both of the following articles to see why you should consider F# for your next project:

 

Thanks,

Kevin

Advertisement

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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