1

Most of us write conditionals like:

if (resultIndex == 0)

...but occaisionally I come across someone writing them like:

if (0 == resultIndex)

...and interestingly those people have been authors and seemingly pretty hot coders.

So why do some people choose the 'backwards' style? Is there some history behind it? Readabililty?


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

Community
  • 1
  • 1
MrGreggles
  • 6,113
  • 9
  • 42
  • 48
  • 1
    Duplicate of http://stackoverflow.com/questions/271561/why-does-one-often-see-null-variable-instead-of-variable-null-in-c – Binary Worrier May 21 '09 at 10:44
  • Also duplicate of http://stackoverflow.com/questions/797162/is-there-any-difference-between-ifa5-or-if5a-in-c – ChrisF May 21 '09 at 10:51

5 Answers5

7

It is a legacy from C, where a common bug would be to write

if (x = 0) {...}

If you taught yourself to write these tests the other way around, then the compiler would complain when you made the == typo, instead of silently adding a bug.

Rik Heywood
  • 13,816
  • 9
  • 61
  • 81
6

This has been asked numerous times before though I can't seem to find the Dup.

Essentially, this style is a hangover from C where a common mistake for

if (c == 5) //Comparison

was

if (c = 5) //Assignment

In the latter case, the compile rwould not complain so people wrote it like so to reduce the likelyhood of this happening

if (5 == c) 
Eoin Campbell
  • 43,500
  • 17
  • 101
  • 157
2

Because (in C, where most of those programmers probably learned; I can't remember if this holds in C#) if you leave off a = character in the 'regular' style, you'll overwrite the value you are trying to compare (and you'll be checking the boolean value of the assignment). If you do it in the 'backwards' style, you'll get a compiler error.

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
  • It doesn't hold in C#. You get a compiler error because x = 0 isn't a conditional expession it's a statement. – Mark Pim May 21 '09 at 10:42
  • Mark Pim: You're right of course, but I'd like to add that the problem is not that it's a statement - even a statement is treated as a conditional expression if it can be evaluated to a boolean. So "if (a = true)" is a totally valid construct (evaluates to true), but of course nobody would use that, you would use "if (a)". – Tamas Czinege May 21 '09 at 10:48
1

In C#, integer values are not implicitly casted to boolean, so if (a = 0) yields a compiler error and does not compile. Hence, this practice is outdated and totally unnecesary in C#.

Tamas Czinege
  • 118,853
  • 40
  • 150
  • 176
0

The main reason behind this I believe is to prevent mistakes such as:

if (variable = 0)

Where you assign in the condition instead of comparing. The reason this would fail the other way around is because you have a constant on your left side of the expression.

So if you were to mistakenly write:

if (0 = variable)

You would get a compilation error from trying to assign something to a constant.

In my opinion this has more in regard to programming style, but still might be a good tip for new programmers. But once you're a little more experienced, errors such as this don't happen and if they do you should be able to track them down pretty easily.

Idan K
  • 20,443
  • 10
  • 63
  • 83