-
Notifications
You must be signed in to change notification settings - Fork 280
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
to x == null
#2749
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for cleaning them up; I always want to fix these conditions.
The !=
conditions can be addressed similarly. I started suggesting the code change, but it would be better to be done on your side. I'll push my suggestions so far. Also, using is
or is not
instead of ==
or !=
can be slightly more efficient because it avoids the potential overhead of an overloaded operator. The approach you're taken is the safest, but feel free to choose the other one if you're ready for the potential headache of verifying possible overloads.
@@ -43,7 +43,7 @@ internal static string ExpandDataDirectory(string keyword, string value) | |||
// find the replacement path | |||
object rootFolderObject = AppDomain.CurrentDomain.GetData("DataDirectory"); | |||
var rootFolderPath = (rootFolderObject as string); | |||
if ((null != rootFolderObject) && (null == rootFolderPath)) | |||
if ((null != rootFolderObject) && rootFolderPath == null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ((null != rootFolderObject) && rootFolderPath == null) | |
if ((rootFolderObject != null) && rootFolderPath == null) |
@@ -37,7 +37,7 @@ protected internal Transaction EnlistedTransaction | |||
set | |||
{ | |||
Transaction currentEnlistedTransaction = _enlistedTransaction; | |||
if (((null == currentEnlistedTransaction) && (null != value)) | |||
if ((currentEnlistedTransaction == null && (null != value)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ((currentEnlistedTransaction == null && (null != value)) | |
if ((currentEnlistedTransaction == null && (value != null)) |
@@ -85,8 +85,7 @@ override protected DbConnectionInternal CreateConnection(DbConnectionOptions opt | |||
redirectedUserInstance = true; | |||
string instanceName; | |||
|
|||
if ((null == pool) || | |||
(null != pool && pool.Count <= 0)) | |||
if (pool == null || (null != pool && pool.Count <= 0)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (pool == null || (null != pool && pool.Count <= 0)) | |
if (pool == null || (pool != null && pool.Count <= 0)) |
|| null == _currentTransaction | ||
|| null == value | ||
|| _currentTransaction == null | ||
|| value == null | ||
|| (null != _currentTransaction && !_currentTransaction.IsLocal), "attempting to change current transaction?"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|| (null != _currentTransaction && !_currentTransaction.IsLocal), "attempting to change current transaction?"); | |
|| (_currentTransaction != null && !_currentTransaction.IsLocal), "attempting to change current transaction?"); |
if ((null == _currentTransaction && null != value) | ||
|| (null != _currentTransaction && null == value)) | ||
if ((_currentTransaction == null && null != value) | ||
|| (null != _currentTransaction && value == null)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|| (null != _currentTransaction && value == null)) | |
|| (_currentTransaction != null && value == null)) |
@@ -160,7 +160,7 @@ internal string Restrictions | |||
get | |||
{ | |||
string restrictions = _restrictions; | |||
if (null == restrictions) | |||
if (restrictions == null) | |||
{ | |||
string[] restrictionValues = _restrictionValues; | |||
if ((null != restrictionValues) && (0 < restrictionValues.Length)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ((null != restrictionValues) && (0 < restrictionValues.Length)) | |
if ((restrictionValues != null) && (0 < restrictionValues.Length)) |
{ | ||
// find the replacement path | ||
object rootFolderObject = AppDomain.CurrentDomain.GetData("DataDirectory"); | ||
rootFolderPath = (rootFolderObject as string); | ||
if ((null != rootFolderObject) && (null == rootFolderPath)) | ||
if ((null != rootFolderObject) && rootFolderPath == null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ((null != rootFolderObject) && rootFolderPath == null) | |
if ((rootFolderObject != null) && rootFolderPath == null) |
@@ -449,7 +449,7 @@ internal DbConnectionPoolGroup GetConnectionPoolGroup(DbConnectionPoolKey key, D | |||
} | |||
|
|||
// We don't support connection pooling on Win9x; it lacks too many of the APIs we require. | |||
if ((null == poolOptions) && ADP.s_isWindowsNT) | |||
if (poolOptions == null && ADP.s_isWindowsNT) | |||
{ | |||
if (null != connectionPoolGroup) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (null != connectionPoolGroup) | |
if (connectionPoolGroup != null) |
@@ -492,7 +492,7 @@ internal DbConnectionPoolGroup GetConnectionPoolGroup(DbConnectionPoolKey key, D | |||
Debug.Assert(null != connectionPoolGroup, "how did we not create a pool entry?"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug.Assert(null != connectionPoolGroup, "how did we not create a pool entry?"); | |
Debug.Assert(connectionPoolGroup != null, "how did we not create a pool entry?"); |
@@ -492,7 +492,7 @@ internal DbConnectionPoolGroup GetConnectionPoolGroup(DbConnectionPoolKey key, D | |||
Debug.Assert(null != connectionPoolGroup, "how did we not create a pool entry?"); | |||
Debug.Assert(null != userConnectionOptions, "how did we not have user connection options?"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug.Assert(null != userConnectionOptions, "how did we not have user connection options?"); | |
Debug.Assert(userConnectionOptions != null, "how did we not have user connection options?"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also prefer this direction. 👍
src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/ProviderBase/DbConnectionInternal.cs
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlClientPermission.cs
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlConnectionFactory.cs
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs
Outdated
Show resolved
Hide resolved
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2749 +/- ##
==========================================
- Coverage 71.74% 71.69% -0.06%
==========================================
Files 308 308
Lines 62301 62303 +2
==========================================
- Hits 44700 44668 -32
- Misses 17601 17635 +34
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
@DavoudEshtehari @David-Engel Once this PR goes through, I have all the code changes lined up for doing |
…t/TdsParser.cs Co-authored-by: David Engel <[email protected]>
Just noting that it's becoming increasingly common/standard to do |
@roji Acknowledged - I think I'm being overly cautious here just because it wouldn't surprise me if buried somewhere in the code there's an We can convert to |
Did you make the changes manually or did you write an analyzer+codefixer to do it for you? |
@Wraith2 definitely not the latter, but not entirely the former. I had some automated finding and making the changes, but I assessed each one (very quickly, that is) as I made the change. |
commit ac1012a Author: dauinsight <[email protected]> Date: Fri Aug 16 13:22:55 2024 -0700 Hotfix v3.1.6 Release notes (dotnet#2767) commit 619fa74 Author: Javad Rahnama <[email protected]> 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 <[email protected]> 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 <[email protected]> Date: Fri Aug 9 12:40:30 2024 -0500 `null != x` => `x != null` (dotnet#2751) commit 6fbbb78 Author: Benjamin Russell <[email protected]> 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 <[email protected]> --------- Co-authored-by: David Engel <[email protected]> commit 6f177e3 Author: Aris Rellegue <[email protected]> Date: Tue Jul 30 19:52:38 2024 +0000 Fix | Fixed some Azure managed identity authentication unit test failures (dotnet#2652)
Description: This PR is part of a series that aims to remove "yoda conditions" from the codebase. In my opinion, these make the code harder to read due to the natural tendency to read an if condition like "x equals y". In this case "null equals x" is unnatural compared to "x equals null". In most if not all cases, there is no semantic difference between
x == null
andnull == x
, meaning these changes can be safely made without impact to the behavior.AI Analysis: Since this PR is large and boring for people to review, I ran the patch through a large language model to ask it to assess the safety of it. Here's is its analysis:
Testing: The code builds, I will wait for CI to verify it more thoroughly.
Note, this is only part 1 of several parts