Mimetis / Dotmim.Sync

A brand new database synchronization framework, multi platform, multi databases, developed on top of .Net Standard 2.0. https://dotmimsync.readthedocs.io/
MIT License
856 stars 188 forks source link

Turn on nullable types #1211

Open symbiogenesis opened 1 week ago

symbiogenesis commented 1 week ago

The dreaded NullReferenceException comes from a mistake by the legendary Tony Hoare in 1966, and he has since apologized for it. But most languages have, nevertheless, inherited this mistake.

.NET doesn't provide a real solution at the type level to fully eliminate them, but with the latest .NET versions if you turn on nullable types you can come pretty close to solving this class of problems entirely at compile-time.

And we can do it with confidence that we aren't breaking anything with the following process:

  1. Set <Nullable>enable</Nullable> on a selected project
  2. Put #nullable disable on the top of every code file
  3. Remove that line from the previous step on a file-by-file basis, and modify each file by affixing ? modifiers accordingly.

If enough progress is made we can simply move the <Nullable>enable</Nullable> setting into Directory.Build.props and have it be set for every project in the solution.

I have been looking for a solution that can just automatically add nullability modifiers in a dumb way, which replicates existing nullability guarantees. Haven't found anything.

But, given that it isn't really enforced at the type level, if it compiles then it is going to work as before.

symbiogenesis commented 1 week ago

I say that it isn't real because when you turn on nullable types, a variable that is declared without a ? modifier is actually still fully capable of holding a null value, such as when it is a public function and referenced from code that doesn't have nullable types turned on. Because it is all just akin to fancy analyzers, rather than part of the type system.

But far, far, better than nothing. And also, there's yet more analyzers that warn you about unguarded use of non-nullable parameters of public functions.