1. ## Comparison Question

Why is it fundamentally better to use the syntax:

Code:
`if(42 == answer)`
as opposed to:

Code:
`if(answer == 42)`

2. It isn't.
http://c-faq.com/style/revtest.html

Get a compiler that warns you about using = in an if statement and move on with your life.

This is an ancient trick that has only limited usefulness.

For example, if you have
if ( var1 = var2 )
then you're FORCED to learn how to use == properly, and no amount of rearranging the deck chairs on the titanic will save you.

3. Originally Posted by Salem
It isn't.
Question 17.4

Get a compiler that warns you about using = in an if statement and move on with your life.

This is an ancient trick that has only limited usefulness.

For example, if you have
if ( var1 = var2 )
then you're FORCED to learn how to use == properly, and no amount of rearranging the deck chairs on the titanic will save you.
QFT!

Don't put the constant first!
If you're conciously going to think about doing that every time then you may as well just conciously think about using the right operator every time instead.

4. I hate when people at work do this. It just looks annoying.

5. Originally Posted by Salem
It isn't.
http://c-faq.com/style/revtest.html

Get a compiler that warns you about using = in an if statement and move on with your life.

This is an ancient trick that has only limited usefulness.

For example, if you have
if ( var1 = var2 )
then you're FORCED to learn how to use == properly, and no amount of rearranging the deck chairs on the titanic will save you.
Or we can get even quirkier!

Modified code from other help topic:
Code:
```#include <stdio.h>
#define equals ==
void main(void){
int one, two, three;
printf("Enter 3 ints seperated by spaces:");
scanf("%d %d %d", &one, &two, &three);
((one equals two) && (two equals three)) ? printf("Equal\n") : printf("Not Equal\n");
}```
I wonder how many programmers just shuddered in fear.

Oh, then later you can undefine "equals" and issue a vim command line ":%s/equals/==/g" and move on with our lives like nothing happened!

Or we can avoid all of this and just remember "==" != "=" phew...

6. Originally Posted by tenchu
Or we can get even quirkier!

Or we can avoid all of this and just remember "==" != "=" phew...

That is funny.

7. > #define equals ==
Guess what, people think that sucks as well.
Question 10.2

8. "equals" as macro name is a rather bad choice, perhaps "EQUALS" would be better? Anyway, this solves the problem of accidental assignment much better than reverse comparisons.

9. Originally Posted by Salem
> #define equals ==
Guess what, people think that sucks as well.
Question 10.2

Yes, my post was mostly meant as a joke but it does solve the problem. I think it makes more sense than the FAQ you linked and more than than switching the order of the comparison! At the end of the day the best thing you can do is just remember how to code properly and not make the mistake. I wouldn't rely on the compiler or any other convention to help prevent it. I hardly ever forget an equals sign when I compare, but I am much more likely to forget a semicolon when I shouldn't. :P

10. I've actually seen several cases on this forum recently where people incorrectly had == instead of =.
e.g.
Code:
`found == true;`
Again, a good compiler comes to the rescue, noting that there is a statement that has no effect.

11. Originally Posted by iMalc
I've actually seen several cases on this forum recently where people incorrectly had == instead of =.
e.g.
Code:
`found == true;`
Again, a good compiler comes to the rescue, noting that there is a statement that has no effect.
I'm just remembering, with a shudder, some code from a colleague that had provided an operator==() for a class that had side effects - actually reading from a database. Apart from the fact he deliberately broke a "like int" guideline when overloading operators, he had sprinkled a few comparisons that had (it appeared) no effect into code that used the class. When those lines were removed by someone else, or turned into assignments, the program exhibited some strange bugs.