C#. Switching nullable off

Discussion of chess software programming and technical issues.

Moderator: Ras

Henk
Posts: 7251
Joined: Mon May 27, 2013 10:31 am

C#. Switching nullable off

Post by Henk »

Getting code like this with two exclamation marks. So I switch it off in project settings. It is called Nullable.

Code: Select all

Assert(!Board!.CanCaptureKing(Board.OpponentColor))  // verify not in check
These extra exclamation marks are so annoying. I know Board is never null.
Maybe these exclamation marks are handy when Board might be null.
User avatar
lithander
Posts: 915
Joined: Sun Dec 27, 2020 2:40 am
Location: Bremen, Germany
Full name: Thomas Jahn

Re: C#. Switching nullable off

Post by lithander »

Yeah, I gave that new "feature" 3 minutes before I googled how to get rid of it. :)
Minimal Chess (simple, open source, C#) - Youtube & Github
Leorik (competitive, in active development, C#) - Github & Lichess
JoAnnP38
Posts: 253
Joined: Mon Aug 26, 2019 4:34 pm
Location: Clearwater, Florida USA
Full name: JoAnn Peeler

Re: C#. Switching nullable off

Post by JoAnnP38 »

lithander wrote: Tue Jan 24, 2023 1:00 pm Yeah, I gave that new "feature" 3 minutes before I googled how to get rid of it. :)
I'm the other way for the moment. I just check the nullability option at the project level that forces all reference variables to be not null. Then when you want a nullable reference, you have to declare it that way. At first it took some adjustment, but now I leave it on. I hated having to test for null when writing routines other developers might call. It is amazing just how many unit tests we had to write to guarantee that the appropriate exception was being thrown when a null parameter was passed. The "!" nullability override is ugly -- on that we can agree.

Of course, if you have had to write as much parameter checking code for all my methods to prevent calling with null as I did in my former career (before I retired) maybe you would feel the same.

You could always Board?.CanCaptureKing(Board.OpponentColor) ?? throw new ..... (Yeah, that's pretty ugly too.)
Mike Sherwin
Posts: 965
Joined: Fri Aug 21, 2020 1:25 am
Location: Planet Earth, Sol system
Full name: Michael J Sherwin

Re: C#. Switching nullable off

Post by Mike Sherwin »

JoAnnP38 wrote: Tue Jan 24, 2023 9:54 pm
lithander wrote: Tue Jan 24, 2023 1:00 pm Yeah, I gave that new "feature" 3 minutes before I googled how to get rid of it. :)
I'm the other way for the moment. I just check the nullability option at the project level that forces all reference variables to be not null. Then when you want a nullable reference, you have to declare it that way. At first it took some adjustment, but now I leave it on. I hated having to test for null when writing routines other developers might call. It is amazing just how many unit tests we had to write to guarantee that the appropriate exception was being thrown when a null parameter was passed. The "!" nullability override is ugly -- on that we can agree.

Of course, if you have had to write as much parameter checking code for all my methods to prevent calling with null as I did in my former career (before I retired) maybe you would feel the same.

You could always Board?.CanCaptureKing(Board.OpponentColor) ?? throw new ..... (Yeah, that's pretty ugly too.)
!!! I'm glad that I don't program in C#! :P :lol: !!!
Henk
Posts: 7251
Joined: Mon May 27, 2013 10:31 am

Re: C#. Switching nullable off

Post by Henk »

I worked in the past on a project where you had to check everything with an ok boolean or something. Making your code much more difficult to read when you were only interested in the main line.

Fortunately this is my own project. No unnecessary checks. No checks on events that never happen. And if they happen I will repair it.

By the way it is easier to switch nullable off then switch nullable on and repair the warnings.

You need to remove ? as well as ! and that is relatively easy work.
Henk
Posts: 7251
Joined: Mon May 27, 2013 10:31 am

Re: C#. Switching nullable off

Post by Henk »

By the way would be nice to introduce the 'not' operator as well if you want to improve C#.
JoAnnP38
Posts: 253
Joined: Mon Aug 26, 2019 4:34 pm
Location: Clearwater, Florida USA
Full name: JoAnn Peeler

Re: C#. Switching nullable off

Post by JoAnnP38 »

Henk wrote: Wed Jan 25, 2023 11:39 am By the way would be nice to introduce the 'not' operator as well if you want to improve C#.
They actually did introduce the is not operation that is used for pattern matching. I sometimes use that to shut up the Resharpener nags about transforming my legacy comparisons.