8

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)

Community
  • 1
  • 1
dance2die
  • 35,807
  • 39
  • 131
  • 194
  • Duplicate of http://stackoverflow.com/questions/271561/why-does-one-often-see-null-variable-instead-of-variable-null-in-c ? – Burkhard Mar 17 '09 at 19:30
  • 1
    Ah, gosh, why did the other question use "!=" not "=="... i wasn't able to find that post... again... – dance2die Mar 17 '09 at 19:31
  • @Burkhard: Thanks Burkhard for the 2nd time. – dance2die Mar 17 '09 at 19:33
  • We could really do with the FAQ idea being implemented: http://stackoverflow.uservoice.com/pages/general/suggestions/138261-allow-a-per-tag-home-faq-page As SO grows, it's getting harder to find the duplicates even if you know they're there. – Jon Skeet Mar 17 '09 at 19:33
  • Yes. this is the 2nd time that I had to post a dupe while I could have just looked up the original answer. Voting on User voice – dance2die Mar 17 '09 at 19:36
  • @Jon -- it's easier if you use Google and site:stackoverflow.com, though I would agree on the need for more FAQs. – tvanfosson Mar 17 '09 at 19:37
  • Sung Meister, i don't know any programmer personally who does it in C++. there is still the case of if(var = var) which would still be valid. best put on compiler warnings and write the way it most pleases you. – Johannes Schaub - litb Mar 17 '09 at 19:37
  • (0==var) syntax seems so unnatural to read in my opinion. I think I should probably try to switch them around (if you use Resharper, you can switch them around using "Ctrl+Shift+Alt+ LEFT or RIGHT arrow keys" – dance2die Mar 17 '09 at 19:40
  • @tvanfosson: I kind of started searching for SO posts through google... – dance2die Mar 17 '09 at 19:41
  • and even if you write 0==var, there are still enough sources of bugs. you could write >= as > or != or whatever and no compiler warns you. i mostly regard it as a way for programmers to "show off" what they know about that assigning to 0 isn't possible. but i don't see real use in it. – Johannes Schaub - litb Mar 17 '09 at 19:43
  • @Sung Meister: Yes, I usually use Google to find old posts. When I'm trying to find my own posts, the adverts get in my way though :( Trouble is, a question like this is hard to find - it could say anything. – Jon Skeet Mar 17 '09 at 20:17

4 Answers4

21

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
10

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
  • 398,270
  • 210
  • 566
  • 880
5

I suspect this is a leftover habit. In C:

 if (0 = variable)

will throw a compiler error whereas

 if (variable = 0)

will not.

Dana
  • 32,083
  • 17
  • 62
  • 73
0

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
  • 399,467
  • 113
  • 570
  • 794
  • 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 Mar 17 '09 at 19:41
  • I don't know: I think it's pretty clear either way. – Joel Coehoorn Mar 17 '09 at 19:44
  • 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 Mar 17 '09 at 19:49
  • 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".. – dance2die Mar 17 '09 at 19:50
  • 2
    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 Mar 17 '09 at 20:26