Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Cleanup | null != x => x != null #2751

Merged
merged 1 commit into from
Aug 9, 2024

Conversation

benrr101
Copy link
Contributor

@benrr101 benrr101 commented Aug 3, 2024

Description: In the second of a series of code cleanup PRs, this PR removes the Yoda conditions null != x and replaces it with x != null. This is the most requested PR from my second PR, so hopefully it will be appreciated :)

As with the previous PR, I have opted to only flip the operands and not replace the condition with x is not null. This is to make the changes as safe as possible, since is not null will potentially behave differently than != null.

AI Analysis: I ran the patch file through a large language model to assess safety but uhhhh, it didn't like me. and it seems to approve of this PR
image

Testing: projecs build andif CI goes through we should be good

@benrr101 benrr101 added the ➕ Code Health Changes related to source code improvements label Aug 3, 2024
@Tornhoof
Copy link

Tornhoof commented Aug 6, 2024

AI Analysis: I ran the patch file through a large language model to assess safety but uhhhh, it didn't like me. and it seems to approve of this PR

Your AI Analysis is not really correct afaik, technically they are different:

x != null calls the inequality operator != with x on the left hand and null != x with x on the right hand.
If there are maniacs who implement != that wrong, i.e. with different null behavior for left hand vs. right hand the result will be different. You can run https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEUCuA7AHwAEAmARgFgAoIgZgAJT6Bhegb2vq8ZJPc+6CAsgE9mAGwCGAZ2n0E9ALz08MAO71REmdLYA1SeJwwl9MgF8A3AMFcAlgDMAFHhzjx9AITKEAShu2HFS2IYxkAJxOAESu7lG+1sGh5gGCjk4K3ipu4v5JIUGhtkQR0bHi8YlFKfm2qdzpCnZyeBAY2e55RYVF3CWRMTmV9Vw1gjU11HQ8mmJSstQ9XNN2eO0GRiZsAOYwGJbSe5Zj3AHTJUj0wBAQHhAADjBQkhjQSopOWvNyCGiz2rJ6CIutwlsUAOzyAB0G2M7yBMMMxiq3BOywYFyuNzuj2erygXg+Xx08j+xMBwICYMEI3ojTpLRyIIKtME/WiTUZcQStLRdVqaWcIgZHVytOpoXZUWFzVFwwFqNptKIkPkAH5EZtCUCNbCYCjRtRzEA=

for an example, to see the call difference.

That's why the whole is not null syntax is superior, it bypasses the inequality operator alltogether.

Having said that, the only way I know of, how x != null or null != x behaves differently than x is not null is, if someone overloads the inequality operator != in the source code, a short look through the code via GitHub code search only shows one instance in a test and that one looks fine. I assume here that types in external libraries, e.g. BCL are fine.

Personally I'd recommend normalizing x != null null != x to is not null as it is either equally wrong as your changes or equally correct, but infinitely more readable.

Note: The same applies to the previously merged PR with ==, I was just not fast enough there to comment on it.

@benrr101
Copy link
Contributor Author

benrr101 commented Aug 6, 2024

@Tornhoof I won't disagree too hard - you're right that it is possible someone wrote a ==/!= overload that treats left and right nulls differently, and you're right that a person doing that would be a maniac. I slightly disagree that replacing with x is not null would be equally wrong vs x != null. The way I see it, there is three scenarios

  • != operator was overloaded to treat left and right null differently - maniacal, and would be broken with both replacements
  • != operator was overloaded to treat null differently - not ideal, but is not null would break it, != null would not
  • != operator is not overloaded - != null and is not null behave the same

It all likelihood, the last case is probably 90+% of the cases in the code, and in that case I would indeed advocate for using is not null. But, I want to take these changes one step at a time and try to break as little in the process as possible. I have a slew more of these cleanup PRs ready to go, and I plan to do one that addresses == null to is null at the same time as using conditional access and pattern matching (to some extent, not the x is { foo: bar } kind)

@DavoudEshtehari DavoudEshtehari added this to the 6.0-preview1 milestone Aug 9, 2024
Copy link

codecov bot commented Aug 9, 2024

Codecov Report

Attention: Patch coverage is 75.31250% with 158 lines in your changes missing coverage. Please review.

Project coverage is 71.76%. Comparing base (6f177e3) to head (117ed98).
Report is 1 commits behind head on main.

Files Patch % Lines
...etfx/src/Microsoft/Data/SqlClient/SqlDataReader.cs 34.88% 28 Missing ⚠️
...crosoft/Data/SqlClient/SqlInternalConnectionSmi.cs 0.00% 26 Missing ⚠️
...t/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs 72.72% 15 Missing ⚠️
...rc/Microsoft/Data/SqlClient/SqlClientPermission.cs 18.75% 13 Missing ⚠️
...n/src/Microsoft/Data/Common/NameValuePermission.cs 47.36% 10 Missing ⚠️
...fx/src/Microsoft/Data/Common/DBConnectionString.cs 0.00% 8 Missing ⚠️
.../netcore/src/Microsoft/Data/SqlClient/TdsParser.cs 82.35% 6 Missing ⚠️
...nt/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs 85.71% 5 Missing ⚠️
...soft/Data/SqlClient/Server/SmiEventSink_Default.cs 28.57% 5 Missing ⚠️
...etfx/src/Microsoft/Data/SqlClient/SqlConnection.cs 84.00% 4 Missing ⚠️
... and 26 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2751      +/-   ##
==========================================
+ Coverage   71.74%   71.76%   +0.01%     
==========================================
  Files         308      308              
  Lines       62301    62314      +13     
==========================================
+ Hits        44700    44717      +17     
+ Misses      17601    17597       -4     
Flag Coverage Δ
addons 92.90% <ø> (+0.02%) ⬆️
netcore 76.03% <89.00%> (+0.03%) ⬆️
netfx 69.71% <68.26%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@benrr101 benrr101 merged commit 7216e84 into dotnet:main Aug 9, 2024
131 checks passed
@benrr101 benrr101 deleted the russellben/yoda-not-equals-null branch August 9, 2024 17:42
@JRahnama
Copy link
Member

JRahnama commented Aug 9, 2024

I think, we can also have RoslynAnalyzer to enforce some coding standards to prevent this in future as this cannot be done in editorconfig file.

DavoudEshtehari added a commit to arellegue/SqlClient that referenced this pull request Aug 19, 2024
commit ac1012a
Author: dauinsight <145612907+dauinsight@users.noreply.github.com>
Date:   Fri Aug 16 13:22:55 2024 -0700

    Hotfix v3.1.6 Release notes (dotnet#2767)

commit 619fa74
Author: Javad Rahnama <v-jarahn@microsoft.com>
Date:   Thu Aug 15 13:48:15 2024 -0700

    Fix | Fix the issue with Socke.Connect in managed SNI (dotnet#2777)

commit d3658ed
Author: DavoudEshtehari <61173489+DavoudEshtehari@users.noreply.github.com>
Date:   Wed Aug 14 17:18:51 2024 -0700

    Update SNI version to 6.0.0-preview1.24226.4 (dotnet#2772)

commit 7216e84
Author: Benjamin Russell <russellben@microsoft.com>
Date:   Fri Aug 9 12:40:30 2024 -0500

    `null != x` => `x != null` (dotnet#2751)

commit 6fbbb78
Author: Benjamin Russell <russellben@microsoft.com>
Date:   Sat Aug 3 11:47:53 2024 -0500

    Code Cleanup | `null == x` to `x == null` (dotnet#2749)

    - With one approval and CI success, I think that's enough to move ahead on this one.

    * `null == x` to `x == null`

    * Update src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs

    Co-authored-by: David Engel <dengel1012@gmail.com>

    ---------

    Co-authored-by: David Engel <dengel1012@gmail.com>

commit 6f177e3
Author: Aris Rellegue <134557572+arellegue@users.noreply.github.com>
Date:   Tue Jul 30 19:52:38 2024 +0000

    Fix | Fixed some Azure managed identity authentication unit test failures (dotnet#2652)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
➕ Code Health Changes related to source code improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants