0

Possible Duplicate:
Why does one often see “null != variable” instead of “variable != null” in C#?

Is there any difference between checking for null in the following ways:

object x;
// more code to work on x
if (null == x)
   return;

and

object x;    
// more code to work on x
if (x == null)
   return;

I think its just a style preference and there is nothing wrong (code logic or performance) with it but wanted to check. I think the later is easier to read but my colleague insists on writing it the first way. It drives me nuts. Thanks.

Community
  • 1
  • 1
nbushnell
  • 1,724
  • 1
  • 14
  • 14
  • null is the "reference" it make no sense to put it in first place. How would you say? "I play soccer = Messi" or "Messi plays soccer = me". It's the same case, Messi is the reference and you're comparing with it. – Claudio Redi May 05 '11 at 22:55

5 Answers5

5

I think its just a style preference

It is, it comes from the C/C++ world where apparently this error is very common:

if (x = null)

So instead of a comparison it's an assignment which introduces a potentially subtle bug. That's why they use

if (null = x)

which throws a compiler error.

In C#, both are illegal, so

if (x == null)

seems to be the common form.

Michael Stum
  • 177,530
  • 117
  • 400
  • 535
1

No, there is no difference. The compiler will stop you from doing if(x = null) too (variable assignment in a comparison operation), so there is no benefit from doing if (null == x)

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
  • '(variable assignment in a comparison operation)' - not quite, it fails to compile because it doesn't represent a `bool` result. `bool a=false; bool b=true; if(a = b) { ... }` will compile just fine. – Andras Zoltan May 05 '11 at 22:55
  • The assignment in comparison is only a problem in languages with dynamic types, where most scalars and even complex types can evaluate to booleans thus will compile properly. – BoltClock May 05 '11 at 22:59
0

I prefer the second example, because as you said, it's easier to read. They will perform the same.

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
0

Because of what Michael Stum said, I use

if(null == x) {}

in all my code. Besides, it makes more sense if the comparison variable is a long reference that sticks off the page to the right and there is a scroll bar to move over to see what you are comparing too.

The other reason is it easier to read to see the value you are comparing for, aka:

if("the answer" == someVariable)

because "the answer" is what you are looking for, the variable name can be confusing and isn't what you are looking for.

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
0

I like the first, but then I've been good friends with the C language for a long, long time. Depending on the value of x, the first can be much more readable. If 'x' is a long, horrible, ugly expression, it's far easier to grok the if statement if the short, simple, constant against which the expression is being tested is on the left side of the comparison operator. This is especially true if the expression is long enough to wrap across several lines — you don't have to mentally unwind the expression to grok that the whole thing is a test for nullity.

if ( null == (/*horribly long and convoluted expression*/) )

is much easier to comprehend than its converse

if ( (/*horribly long and convoluted expression*/) == null )
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135