views:

1033

answers:

4

Exact duplicate:

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

I have seen senior developers using syntaxes mentioned in the title.

Is there a need for specifying a constant first in .NET? (as opposed to in C/C++ world)

+20  A: 

No, there's no need for this, because the problem it tries to avoid - namely the typo of:

if (variable = 0)

wouldn't compile in C# anyway. The conditions in if statements have to be Boolean. There's still a risk of making one of these mistakes:

if (something = true)
if (something = false)

if something is a Boolean variable, but the better way to fix this is to avoid the constant:

if (something)
if (!something)

If you have developers bringing over idioms like this from other languages without thinking about whether they're appropriate in C#, you should keep an eye for them doing more of the same. If you try to write C# as if it's C++ (or any other language, pretty much - with the possible exception of VB.NET) you'll end up writing non-idiomatic C# code.

EDIT: As cletus noted, there is another potential area for concern:

bool a = false, b = true; 
if (a = b) { } // No warnings

So the error can still occur - but then we're outside the realm of comparing with a constant anyway :) I'd say this crops up incredibly rarely, and isn't worth too much time spent worrying about it.

Jon Skeet
So the (0==variable) syntax might be a remnant habit in C/C++?
Sung Meister
No "might be" about it. it is.
cletus
@Jon Skeet: Your second 50K of rep is going to be from answering dupes of the questions that got you your first 50K :P
Jon B
It's worth noting that: bool a = false, b = true; if (a = b) { } generates NO warnings.
cletus
@Jon Skeet: Thank you for the update comment.
Sung Meister
@cletus: that's something I should keep in mind. Thank you for pointing that out.
Sung Meister
+5  A: 

I suspect this is a leftover habit. In C:

 if (0 = variable)

will throw a compiler error whereas

 if (variable = 0)

will not.

Dana
+7  A: 

There is no need for this syntax in the modern world. It's a habit that many of us got into when our C compiler wouldn't warn us that we were about to launch the missiles.

if(status = RED_ALERT)
{
    launchMissiles();
}
Bill the Lizard
+1 for that disturbing scenario!
Michael Meadows
+1 Quite extreme case...
Sung Meister
A: 

It's not needed for C#. But It's still a good idea.

Programming is all about habit and you never know when you'll find yourself writing some javascript or updating an old program you thought was long gone.

Joel Coehoorn
Wouldn't you say it affects readability a bit, though? When we read the code out loud: We're not checking if zero equals a variable. We already know only zero can only equal zero. We want to check if the variable equals zero.
Michael Meadows
I don't know: I think it's pretty clear either way.
Joel Coehoorn
I've heard the readability reasoning many times, but I've never considered it an issue either. The == operator is symmetric (unless you override it!), so x == 0 should read just the same as 0 == x. I know from past arguments that I'm in the minority. :)
Bill the Lizard
For some reason i found (null == obj) to be quite unnatural when I "speak" that part of code in my mind... Usually one would say "when an object is null" not "null is object"..
Sung Meister
Programming *shouldn't* be about habit. You should absolutely be aware of what language you're using - use the appropriate idioms for the language.
Jon Skeet