11

Is there a difference in the order of the comparison operator?

#define CONST_VALUE 5

int variable;

...

if ( variable == CONST_VALUE )   // Method 1
...

OR

if ( CONST_VALUE == variable )   // Method 2
...

Is this simply a matter of preference or is there a compelling reason for a particular comparison order?

semaj
  • 1,555
  • 1
  • 12
  • 25
  • see http://stackoverflow.com/questions/283205/what-was-the-most-dangerous-programming-mistake-you-have-made-in-c/283212#283212 – Daniel Kreiseder Oct 26 '09 at 19:45
  • Relevant: [https://en.wikipedia.org/wiki/Yoda_conditions](https://en.wikipedia.org/wiki/Yoda_conditions) – Boann Nov 26 '13 at 19:12

5 Answers5

31

The reason some people use method 2 is because you'll get a compiler error if you mistype a = in place of the ==.

However, you'll have people (like me) who will still use method 1 because they find it more readable and if there is an error, it will be detected during testing (or, in some cases, static analysis of the code).

Thomas Owens
  • 114,398
  • 98
  • 311
  • 431
  • 11
    Tell me, have you ever, ever made this mistake anywhere where writing it this way would have caught it? I write it "method 1" because it's more readable, and I'm pretty sure I've *never* written = instead of == in 25 years of writing C and C-like languages. – Paul Tomblin Oct 26 '09 at 18:48
  • 5
    It happened to me in the past. Consider yourself lucky. – Ori Pessach Oct 26 '09 at 18:50
  • 3
    @Paul Tomblin: On at least three occasions over the years I've fixed long-standing bugs from having an assignment in an if (or while) statement where a comparison was intended. That said, I should also point out that some compilers (gcc in particular) can/will give a warning if it contains *only* an assignment (no comparison), unless contained in an extra set of parentheses. Putting the cosntant first is guaranteed to work with all compilers though. – Jerry Coffin Oct 26 '09 at 18:54
  • 2
    Paul: No, I don't write it this way simply because I'm confident that if I were to make a =/== mistake, I would find it during static analysis or testing. However, the first sentence IS the argument that people who write it the other way make. – Thomas Owens Oct 26 '09 at 18:55
  • 2
    @Paul: I have made the mistake once or twice before, but it's often easy to catch. And the compiler warning is reasoning is silly because if you remember to use `3==var` then you'll remember to check for typos. – DisgruntledGoat Oct 26 '09 at 19:11
  • Goat pretty much sums it up I think. If you're gonna remember to do the constant, it's just as equally difficult (read: not at all) to do ==. – GManNickG Oct 26 '09 at 19:26
  • 2
    @Goat, GMan: not for me. I write the constant lvalue form by habit, and thought it is rare, the compiler has caught me at it from time to time. – dmckee --- ex-moderator kitten Oct 26 '09 at 20:04
  • @Paul - I make this mistake more often than I'd like to admit; each time I curse my poor typing skills and the fact that C++ doesn't behave like C# in this situation. However, I hate the way method 2 reads and I still usually use method 1, even though every once in a while I get bit in the ass. And that doesn't count the times where, like Jerry Coffin, I come across a bug related to this issue that wasn't even typed in by me. – Michael Burr Oct 26 '09 at 20:13
  • 1
    @GMan: Doing it the `const==var` way isn't something you have to remember to do, once you learned the habit. You just do it. Also, it isn't something you can do wrong by accident, as the `=`/`==` distinction. – sbi Oct 26 '09 at 21:47
  • If you can get in the habit of reversing the order, you can get in the habit of checking your typing. – GManNickG Oct 27 '09 at 22:39
  • @DisgruntledGoat Agreed! And the point is that if one is able to remember to write constant at LHS then he must also be able to remember to write `==` correctly instead of `=`... – Sam Nov 27 '13 at 07:09
10

The only difference is that ( CONST_VALUE == variable ) makes the common typo ( CONST_VALUE = variable ) impossible to compile.

By comparison, if ( variable = CONST_VALUE ) will result in the compiler thinking you meant to assign CONST_VALUE to 'variable'.

The =/== confusion is a pretty common source of bugs in C, which is why people are trying to work around the issue with coding conventions.

Of course, this won't save you if you're comparing two variables.

And the question seems to be a duplicate of How to check for equals? (0 == i) or (i == 0)

And here's some more information: http://cwe.mitre.org/data/definitions/481.html

Community
  • 1
  • 1
Ori Pessach
  • 6,777
  • 6
  • 36
  • 51
3

As others mentioned, CONST_VALUE == variable avoids the = typo.

I still do "variable == CONST_VALUE", because I think its more readable and when I see something like:

if(false == somevariable)

my bloodpressure goes up.

whatsisname
  • 5,872
  • 2
  • 20
  • 27
  • 3
    I do use `if (CONST_VALUE == variable)`, but if I see `if(false == somevariable)`, my blood pressure suffers, too. (That's because this ought to be `if(!somevariable)` of course.) – sbi Oct 26 '09 at 18:49
2

The first variant

if (variable == CONST_VALUE) 

is better, because it is more readable. It follows the convention (also used in mathematics) that the value that changes most comes first.

The second variant

if (CONST_VALUE == variable)

is used by some people to prevent a mixup of equality checking with the assignment

if (CONST_VALUE = variable)

There are better ways to achieve that, for example enabling and taking heed of compiler warnings.

starblue
  • 55,348
  • 14
  • 97
  • 151
1

Others already pointed out the reason. = / == confusion. I prefer the first version because it follows the thought process more closely. Some compiler alleviate the confusion of = and == by giving a warning when it encounters something like

if(a=b)

in this case if you really wanted to do the assignation you're forced to write

if((a=b)) 

which I would write then as

if( (a=b) != 0) 

to avoid the confusion.

This said, we had in our code 1 case where we had a =/== confusion and writing it the other way round wouldn't not have aided as it was a comparison between vars.

Patrick Schlüter
  • 11,394
  • 1
  • 43
  • 48