tags:

views:

248

answers:

5

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?

+22  A: 

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
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
It happened to me in the past. Consider yourself lucky.
Ori Pessach
@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
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
@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
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 ==.
GMan
@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
@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
@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
If you can get in the habit of reversing the order, you can get in the habit of checking your typing.
GMan
+7  A: 

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 http://stackoverflow.com/questions/148298/how-to-check-for-equals-0-i-or-i-0

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

Ori Pessach
+3  A: 

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
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
+1 for bloodpressure
GMan
+1  A: 

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
+1  A: 

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.

tristopia
In GCC that option is -Wparentheses
Adisak