If zero must be checked left or right

When looking at some code, I saw:

if (null == condition) { ... } 

and I also saw:

 if (condition == null) { ... } 

I seem to remember that there is an advantage of having null on the left side, but I don't remember it and thought it was an older run-time element that was optimized with later versions of .NET. I tend to use the latest zero check, so the first is striking.

So this is a style question, or is there an advantage to having null on the left or right side of the score?

+5
source share
3 answers

This can make a difference in three cases.

Firstly, a typo of condition = null valid in if . This is more common in C-style languages ​​that allow null in if (evaluated as false ), which is most of them, but not C #.

In C #, you can create a type that has this effect:

 public class Test { public static bool operator true(Test x) { return true; } public static bool operator false(Test x) { return false; } } void Main() { Test test = new test(); if (test = null) { Console.WriteLine("!"); } } 

Not so many times that overloading these operators makes sense, especially since .Net 2.0 introduced generics (it had more value for types like SqlBoolean , which allowed us to indicate true , false or null in the way that we will now use bool? ).

So this case is pretty marginal in C #.

The other seems to be if there is an implicit conversion to bool or to a type that, in turn, implements the true and false operators:

 void Main() { Test test = new Test(); if (test = null) { Console.WriteLine("!"); } } public class Test { public static implicit operator bool(Test x) { return true; } } 

Implicit operators should be avoided for several reasons, but this is slightly more likely than the first example, although it still does not come close to the general.

Another one if == overloaded asymmetrically:

 public class Test { public static bool operator == (Test x, Test y) { return ReferenceEquals(x, null); } public static bool operator !=(Test x, Test y) { return !(x == y); } } void Main() { Test test = new Test(); if (test == null) { Console.WriteLine("This won't print."); } if (null == test) { Console.WriteLine("This will print."); } } 

But since asymmetric == always an error, it depends on the fact that an error in the statement has any effect. This probably happens a little more often than in the first case, but it should be fixed when it happens, so it’s even less relevant.

Thus, although this may have an effect on C #, cases are rare and are mainly based on the fact that someone did something they shouldn't.

So this is basically a matter of style. People who set null first tend to choose it from languages ​​where it matters more.

+3
source

The classic reason in C-style languages ​​is to protect against the incorrect entry of equality == as destination = . You cannot assign a constant - that is, the following is illegal and therefore the compiler will catch the error:

 if (false = condition) { ... } 

whereas this is completely legal (but probably not what the author intended:

 if (condition = false) { ... } 

Note. This problem is limited in C # (compared to C vanilla), because if require bool (as mentioned in the comments below), so the only case that actually causes problems is your bool type.

+7
source

Regardless of how (null == condition) compiled, using an example null value as the first (left) operand is intuitive. Instead, the first operand should offer what you intend to evaluate.

0
source

Source: https://habr.com/ru/post/1237941/


All Articles