2

I heard some programmers use if(1 == var) instead of if(var == 1) to avoid unintended assignment. Why or in what cases does it cause unintended assignment?

Vinícius
  • 15,498
  • 3
  • 29
  • 53
  • 11
    Because sometimes people accidentally write `if (var = 1)`. Personally I find (1 == var) incredibly annoying. You shouldn't be making those mistakes after writing a few programs and on the off chance you do they are usually easy to find with a debugger. – Duck Sep 22 '12 at 00:19
  • 1
    btw it's called a Yoda condition. – mvds Sep 22 '12 at 00:19
  • Compilers should warn about that. – chris Sep 22 '12 at 00:19
  • I saw it on here http://stackoverflow.com/questions/490420/favorite-clever-defensive-programming-best-practices/ , actually I don't care about this point stuff, this is my 3rd or 4th account on S.O because I always forget my password/login or something, I was just curious what the `assignment` thing was. I've been self studying for a long time and I learned that asking instead of making my own assumptions is always better, not to count how much I've learned just in S.O. You can -1 how much you want^^ it's no big deal, I'm just here for learning – Vinícius Sep 22 '12 at 00:49
  • 1
    Re "Personally I find (1 == var) incredibly annoying." That's far too nice. This is an absolutely ugly construct that is completely at odds with how people think. – David Hammen Sep 22 '12 at 01:30
  • 1
    Alf, perhaps it might be a good idea to assume good faith? – pmcs Sep 22 '12 at 01:42
  • 1
    [Asked and answered on Programmers](http://programmers.stackexchange.com/q/74086/8) –  Sep 22 '12 at 03:16

2 Answers2

13

The problem is if you mistype the statement:

if (var = 1)

vs

if (1 = var)

In the first case, the code after the if is executed unconditionally (with no more than a warning from the compiler, which isn't obliged to produce a warning for you — but the good ones do; if you aren't using a good compiler, get one!). In the second case, you get a syntax error at compile time, so the problem has to be fixed before the code can compile.

The problem isn't always as blatant:

if (var = 0)

never executes the code after the if, of course. Often though, you'll have:

if (var = function(arg1, arg2))

and it won't be clear whether you intended to assign or compare. You can make it clear to the compiler and code readers (humans) by writing:

if ((var = function(arg1, arg2)) != 0)

or

if (var == function(arg1, arg2))

I don't use the 'back-to-front' comparison technique. I dislike the inverted conditions because they almost invariably read 'wrong' to me. I'm not comparing 1 with my variable; I'm comparing my variable with 1. So, even though logically the == operator is commutative, I don't think commutatively and prefer that 'riskier' way. I have not found myself making the assignment vs equality mistake often enough for the issue to be a problem. The compiler warns me if I do make a mistake (and I pay attention to the warning and fix the code so that there isn't a problem).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Even when my compiler didn't warn me, it still didn't take long to find. When something's always executing, it's easy to notice. It's like forgetting a break in a case label. – chris Sep 22 '12 at 00:21
  • 1
    @chris you may not notice it when the `if` statement is not in the default code execution path. – ouah Sep 22 '12 at 00:24
  • @ouah, True, but when you test your program, you should be testing that area as well. – chris Sep 22 '12 at 00:29
  • @Jonathan, Good point, that edit with `var = 0` happens too. Finding code that doesn't run when it should is a bit more tricky sometimes. – chris Sep 22 '12 at 00:31
  • Well my compiler generates C2106 for `if( x = 0 )` but it doesn't if 2 vars are in question `if( y = x )` – Vinícius Sep 22 '12 at 00:54
  • GCC 4.7.1 gives a warning: `x.c: In function ‘main’:` and `x.c:7:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]`. This is for `if (x = y)`. – Jonathan Leffler Sep 22 '12 at 01:12
4

If you mistype it as

if (var = 1)

that would cause unintended(?) assignment. Decent compilers warn about that unless you include an extra set of parentheses

if ((var = 1))

to make your intention clear to the compiler.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • Annoying compilers warn about that unless you include an extra set of parentheses. – Pete Becker Sep 22 '12 at 00:37
  • Well, okay, every time I've gotten that warning so far, it was because I forgot the parentheses. But still, I'd rather the compiler warned me if some time I really didn't mean to assign. – Daniel Fischer Sep 22 '12 at 00:42