Whilst refactoring code I changed the all the if not null conditions to follow the majority convention in my code of
if (!(foo == null)) instead of
if (foo != null) Is there any advantage in either statement?
Is there any advantage in either statement in c#?
011 Answers
I find the second one more readable.
Apart from that, there is no difference.
It is more important to pick a convention with your team and stick to it within any one particular codebase.
4Assuming you don't have broken == / != operator overloads, I'd just use the second form for the benefit of simplicity / readability. If you do have broken overloads such that there's a semantic difference between the two, then I'd suggest fixing those overloads :)
In the rare case where foo == null is a clearer indication of something, I'd probably refactor it to use a local variable:
bool somethingIsMissing = foo == null; if (!somethingIsMissing) { ... } Parentheses round the foo == null are now optional - use or don't, according to taste. The main thing is that you can use the variable name to make the semantic meaning really clear.
normally if (!(foo == null)) is used when you have more variables to considerate, for example
if (!(f1 == 'a' && f2 != 'b')) sometimes is just easier this way that transform everything to the opposite, specially when you using bitwise operators.
5The first uses two operators, the second uses one. So technically, the second one is simpler.
2The only place where i would use !(a == b) would within the operator implementation of != like this way:
public static bool operator != (MyType a, MyType b) { return !(a == b); } In my opinion, there is no difference, the compiler will optimize the code anyway. But I would prefer if(foo != null). Less parentheses and easier to read.
My prefered one is the second one, as it is a bit more legible than the the first.
They make no difference so it is just a matter of choice for you.
However, if you have lots of other variables in your if clause, the first may be the one to use.
Your choice in the end.
Readability is key, so when you're using an equality operator you should put the != there to make it more clear. But when the comparison gets more complicated there are situations where the other version can be better
if ( (a != b) and (a != c) ) is more clear than if (!( (a == b) or (a == c) )). However the if !() format is better when you have functions that return boolean values like if !(a.valid()) or if !( String.IsNullOrEmpty(a) )
Also if your equality expression gets particularly complex that can also hurt readability, at which point it's better to move the test(s) to a function with a clear name like if !( this.AllSettingsValid() )
In C# 8 you can now write "where value is something"
if (value is { })
It also goes well with the nullable refence feature that will be turned on in 2021 for everything.
There is not such a difference, by using if (!(foo == null)), code optimization might be violated since it has to verify the condition first and then reverse it.
On another side, if (foo != null) will iterate condition only once. Hence you can say, if (foo != null) is a more optimized way.
Since, the first form uses 2 vs. 1 C# Operators, it might compile to more CIL code albeit, in most use cases most would encounter, it probably won't make a significant code size or performance difference.
What IS likely to make a significant difference is readability and therefore chance of errors when writing / modifying. I would (and you Sheldons may want to cover your eyes now) avoid the use of the "!" Operator com-PLETELY. "Gasp! My - WORD!" Yes, I said it. And I MEANT IT! With a PASSION! It's too easy to miss as it's often next to a similar-looking punctuation character (i.e. "(" or "||") on its left and a similar-looking Identifier letter (i.e. "l" or "I") on its right. And missing it could have MAJOR consequences, as it would mean you think the code is doing the exact OPPOSITE of what it's actually doing! And it could also be significantly more likely to be missed by dyslexics making it potentially a disability rights issue protected by the ADA.
Consider the following very likely example:
if (!IsIisServerConnected) if (IsOfflineMode || !(IsIisLocalServerConnected || IsIisCloudServerConnected)) I would instead write the following:
if (false == IsIisServerConnected) if ( IsOfflineMode || ( false == ( IsIisLocalServerConnected || IsIisCloudServerConnected ) ) ) Or in your case:
if (false == (foo == null)) Remember that C (the language that C#, C++, JavaScript, Java and numerous other currently popular languages ultimately inherited their basic syntax from) was created at a time when Punch Cards were still common. With modern day CPUs, RAM, monitors and IDE's (that "false.Equals(?)" snippet could be generated with a custom Keyboard Shortcut), for most people for most apps, readability is much, Much, MUCH (did I mention "much"?) more important than saving a few code characters.
P.S. You could also add a Extension Method to the Boolean Struct calling it, oh, I don't know, Not! ;D So you could write:
if (IsIisServerConnected.Not()) if ((foo == null).Not()) 5