You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: drafts/posts/null-is-the-absence-of-a-thing.md
+12-4Lines changed: 12 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -130,7 +130,7 @@ The "see no evil" approach causes defects. Turning on the Nullable Reference Ty
130
130
131
131
Next, we'll talk about `default`. In general value types have a value. The `default(int)` is `0`. So if you create a reference type, guess what the `default` value is? That's right `null`. That means we can't return things like `default(T)!` and expect the application not to throw `NullReferenceException`. This was one of those no-brainer things when I thought about it. But I had to think about it. This means that every reference type by default is `null`. Not just once I turn on the feature, it has always been and I haven't always ensured there is a default value.
132
132
133
-
It's worth talking about [sentinel values](https://en.wikipedia.org/wiki/Sentinel_value) here. I haven't historically been a fan of them in C#. The problem is checking for _null-state_ doesn't guarantee the default value. It's just the default of a constructed object is `null`. So while I am not a fan of sentinel values, I do think there is a place for a type having a `Default`.
133
+
It's worth talking about [sentinel values](https://en.wikipedia.org/wiki/Sentinel_value) here. I haven't historically been a fan of them in C#. The problem is checking for _null-state_ doesn't guarantee the default value. It's just the default of a constructed object is `null`. So while I am not a fan of sentinel values, I do think there is a place for a type having a `Default`. In many functional languages the concept of `None` or [`Optional<T>`](https://github.com/louthy/language-ext?tab=readme-ov-file#optional-and-alternative-value-monads) exists. These provide you access to a default value that is not `null`. So while I won't recommend returning -999 for an `int` return value. I _might_ recommend a more type safe functional approach to ensure that `null` doesn't cause mischief.
134
134
135
135
### `OrDefault()` methods return `null`
136
136
@@ -179,9 +179,13 @@ At first glance, this operator seems like a good idea, but it defeats the purpos
179
179
180
180
### Pay attention to methods that return null but could benefit from returning a `default`
181
181
182
-
-`IEnumerable<T>?` => `[]`
183
-
-`bool?` => `false`
184
-
182
+
- Methods that return lists
183
+
-`IEnumerable<T>?` => `[]`
184
+
- Methods that return nullable bool
185
+
-`bool?` => `false`
186
+
- Methods that _might_ be better returning a default value
187
+
-`enum`
188
+
_
185
189
### Use object initialization syntax
186
190
187
191
Some developers prefer constructors and some object initialization. If you prefer object initialization for concerns like data transfer objects, consider using the [`init`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/init) keyword. This will signal to the compiler that the object should be initialized with a value, and that the value should not be null, because you cannot set it again after it has received its initial value.
@@ -190,6 +194,10 @@ Some developers prefer constructors and some object initialization. If you pref
190
194
191
195
I am not going to tell you to test all the things™. I am going to tell you that verifying the behavior of something that returns `null` is a good idea. You want to know how your system responds to `null` inputs and outputs. You don't want to leave it to chance. A few well-constructed tests can give you some clarity and potentially reduce regressions.
192
196
197
+
## What's next?!
198
+
199
+
Hopefully this provides you some ideas to turning on C# Nullable Reference Types in a codebase. I know this isn't a comprehensive list, nor is it a step by step guide. I have tried to mimic one approach for every codebase I encounter. I am learning that using the same guidelines and finding the right seams is a manageable approach. So go forth and capture the _null-state_, don't allow it to bring down applications. Enable the feature at your pace and understand what `nullability` means in your codebase!
0 commit comments