[OT] my experience with nullable types (C#)

Kagamin spam at here.lot
Wed Apr 30 07:17:37 UTC 2025


Finally I had my chance to cope with nullable types in one of our 
C# codebases, and the experience wasn't nice.

I had a prejudice that nullable types give some kind of promise 
that you will have only a few of them, but now that I think about 
it I can't remember anyone making this promise, and reality is 
quick to shatter this prejudice in a very ugly way.
If you have a nullable type, all code now sees it as nullable, 
and you must insert null checks everywhere, even if you know it's 
not null by that point, and the volume of this null check spam is 
uncomfortably large. It's also not very clear what's the 
difference between me spamming null checks everywhere by hand and 
processor doing the same automatically.
Nullable types are retrofitted in dotnet and all legacy 
interfaces return nullable types, this greatly increases number 
of null checks.
DTOs are destroyed. How can you even have a DTO with nonnullable 
field? Initialize it to a stub value in default constructor? 
That's 1) NullObject pattern, 2) allocates extra garbage. The 
number of null checks is greatly increased.
Null check operator has pretty high precedence. If you want to 
null check an expression, you'll need to surround it with braces, 
this is especially annoying with await expression as checking the 
result of await is the best place for null check. Exclamation 
also looks like negation, imagine parsing `if(a! == b)`, this 
happens unexpectedly often.


More information about the Digitalmars-d mailing list