.NET and the nullable configuration
-
I found it rather jarring the very first time I built a .NET project using .NET 7 (something that had actually started life as a .NET Framework 4.8 project), so there were quite a few "fixes" to implement. But I came around rather quickly, and now kinda wished older versions were that restrictive/explicit.
You and others are convincing me that I should just bite the bullet and fix the code. ;)
Latest Articles:
A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework -
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockWe do, and at first I hated it, but now I've embraced it. We find it cuts down on lots of null-ref opportunities. Sure, it can be annoying sometimes, but the occasional "!" on the end of a var, combined with the much nicer "if x is not null" makes the code very clear. Assuming you know what a trailing ! is...
cheers Chris Maunder
-
Some people seem to find some computer idioms and technologies difficult to understand. And then they then attempt to popularize that poor understanding as being a failure in the idiom/technology rather than leaving it where the problem actually belongs. Myself I like null. Easier than coming up with a long list of magic values that represent the same thing as 'no value'.
-
You and others are convincing me that I should just bite the bullet and fix the code. ;)
Latest Articles:
A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity FrameworkMy first reaction was, "ugh, did they really do that?" followed by "am I really gonna have to propagate those fixes throughout?" If I would've had to do that throughout my entire codebase (everything I'm still maintaining), there's no way I would've done it. But starting with a small project that hadn't had much built into it yet...the changes were pretty quick and painless. I don't think I've ever adopted a language tweak this quickly. I'm all in. Greatest thing is, wherever I can provide a class instance as a param to a function, and it's *not* explicitly declared as nullable, then I'm guaranteed I don't have to bother with null checks. So I'm still trying to write all functions so nulls are (explicitly) *not* allowed.
-
We do, and at first I hated it, but now I've embraced it. We find it cuts down on lots of null-ref opportunities. Sure, it can be annoying sometimes, but the occasional "!" on the end of a var, combined with the much nicer "if x is not null" makes the code very clear. Assuming you know what a trailing ! is...
cheers Chris Maunder
Chris Maunder wrote:
Assuming you know what a trailing ! is
You nailed it on its ! head. Time to google. Technology keeps leaping forward and I am stumbling around trying to keep up while at the same time wondering why I'm trying to keep up. :laugh:
Latest Articles:
A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework -
Chris Maunder wrote:
Assuming you know what a trailing ! is
You nailed it on its ! head. Time to google. Technology keeps leaping forward and I am stumbling around trying to keep up while at the same time wondering why I'm trying to keep up. :laugh:
Latest Articles:
A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity FrameworkI hear ya. Reminds me of the 2 day long conversation Matthew and I had about the extra negation in
if not "!arg_name:enableGPU=!" == "!arg_name!"
. I didn't see the double negation. He's pointing at his screen (over FaceTime) saying "there!". The issue was the "!" meant delayed expansion of a variable substitution in the Batch file, but to any reasonable developer it's "not arg_name". We imbue symbols with way too much meaning.cheers Chris Maunder
-
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockAll the time, and I fix the warnings if they come up. It’s greatly reduced the number of null exceptions that get thrown. I’d prefer it though if they were reported as build failures by default.
Eagles may soar, but weasels don't get sucked into jet engines
-
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockI have been using it on all of my work and hobby projects since it was introduced to .NET. #nullable #enable 🤣.
-
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockAgree these warnings are pretty annoying in a project that didn't start out using nullable ref types correctly from the start. I have a large project in this situation, and fixing all these warnings is pretty daunting, requiring significant rework and refactoring. But I've come to see the value of using nullable ref types, so I've adopted it in smaller projects, and have been using it going forward. It's just not feasible to retrofit in all cases.
-
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockYes, I use it, and I like it very much. Removed all possible NRE, and it's perfect to make sure juniors do not introduce tons of bugs. Sometimes a IF xx != null is enough, some other times we need "!", I hope the compiler could become smarter like Typescript is.
-
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockThis entire push to eliminate Null is misguided. The reason is that in the real world sometimes you really have no information, which is what Null represents. Look at numeric data types, zero is not the same as NULL. Zero is a value but NULL represents no value. This is a fundamental semantic difference.
-
No one is suggesting you should not use null. The nullable check makes it explicit where you intend to use it - just as it already is for value types.
-
Yes, I use it, and I like it very much. Removed all possible NRE, and it's perfect to make sure juniors do not introduce tons of bugs. Sometimes a IF xx != null is enough, some other times we need "!", I hope the compiler could become smarter like Typescript is.
-
This entire push to eliminate Null is misguided. The reason is that in the real world sometimes you really have no information, which is what Null represents. Look at numeric data types, zero is not the same as NULL. Zero is a value but NULL represents no value. This is a fundamental semantic difference.
Generally the idea is thus... Someone misused it - thus it must be a problem. Then they come up with an idiom that could fix it, that single problem, while introducing other potential problems (like using magic values for no value) and then proclaim that the world is a wonderful place. Thus hoping that a technology fix can absolve them of relying on process (non-technology) solutions which have been proven (studies) to reduce problems.
-
lmoelleb wrote:
makes it explicit where you intend to use it
Your assumption is that I do not understand what it does. You are incorrect in that assumption.
-
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockNot using .Net 7 yet, but using this in .Net 4.8, .Net Core 31, 5, and 6. Yeah, the warnings can be annoying, but the benefit can be significant too. Especially with junior devs who aren't used to null checking everywhere/don't have the experience to spot all the potential problems... but it's good for us oldies too, everybody misses stuff sometimes. The first warning in that list is the one that annoys me most. There are several potential work arounds depending on the specifics, but I often feel none are good :(
-
You and others are convincing me that I should just bite the bullet and fix the code. ;)
Latest Articles:
A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity FrameworkI suppose it's a bit like `const`. You can't just sprinkle a few around the place, you have to go all-in.
Paul Sanders. If I had more time, I would have written a shorter letter - Blaise Pascal. Some of my best work is in the undo buffer.
-
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog Clock -
This entire push to eliminate Null is misguided. The reason is that in the real world sometimes you really have no information, which is what Null represents. Look at numeric data types, zero is not the same as NULL. Zero is a value but NULL represents no value. This is a fundamental semantic difference.
I fully agree with everything you wrote, however, nullable checking is not necessarily about eliminating the use of null per se. It's about telling things that can legitimately be null (then it reminds you to check) apart from things which should never be null (then it reminds you to not put a null in there).