📎 Webclip
Why null in C# is so bad
The article argues that null is a major source of bugs in C# because it breaks assumptions about types and behavior. It frames null as a special value that can stand in for any reference type, which forces code to branch and handle a separate null case.
It presents defensive null checks as verbose and easy to forget, making NullReferenceException likely in production. As the practical fix, it recommends enabling C# 8.0 nullable reference types with <Nullable>enable</Nullable>, especially in greenfield projects.
Reading notes#
- More than half of the author’s bugs were related to null values propagating where they should not have been.
- The null problem is described as the “billion-dollar mistake” because it breaks assumptions about the language.
- The article links this to the Liskov substitution principle, where subtypes should be usable in place of parent types without changing behavior.
- Null is presented as a special instruction that can substitute any type, so code must check whether a value is null before using it.
- Defensive programming with null checks makes code verbose and error-prone, and missing one check can lead to NullReferenceException in production.
- The Elvis operator
?.is described as improving readability, but not solving the underlying issue. - The only real solution presented is to avoid null altogether, though the author says this is hard to enforce across a team.
- C# 8.0 nullable reference types are presented as the main tool to let the compiler check null across code paths.
- The suggested project setting is
<Nullable>enable</Nullable>in the.csprojfile. - The author recommends enabling it for greenfield projects, even if it may break an existing brownfield project at first.
