(no title)
nlkl | 9 years ago
There are of course several ways to represent the absence of a value, fit for different paradigms and programming styles, but I think most of us can agree at this point that having untyped null values is a very bad idea in most cases.
Whenever I program in C#, I always have to be overly paranoid, as more or less anything can be null. Option types certainly introduce some development overhead up front, by forcing the developer to handle the possibility of an absent value, but in my experience this is still far less than the amount of defensive programming necessary to handle nulls correctly.
As a response, I decided to implement a practical and safety-focused C# option type, and now use it more or less everywhere.
Reference: https://github.com/nlkl/Optional
Certainly, this does not guarantee that values are not null, but it allows you to be explicit about absent values in your own APIs, which is of great value. In turn, null checks become much easier as you do not have to accept nulls as legal values (except at boundaries to external code), and working with potentially absent values become much less cumbersome, due to a convenient API.
Interesting things are happening to programming languages these days, and I am quite curious to see the next steps. The C# language team as well has some focus on this (https://github.com/dotnet/roslyn/blob/features/NullableRefer...).
No comments yet.