ez.spikesource.com — August 2006

The biggest programming trap

The biggest programming trap that has causes a lot of griefs in my live is the following: Typing: if ($some_variable = "some_value) { //Do something } while trying to express if ($some_variable == "some
The biggest programming trap that has causes a lot of griefs in my live is the following:

Typing:
if ($some_variable = "some_value") {
    //Do something

}

while trying to express
if ($some_variable == "some_value) {

    //Do something

}


That elusive bug is sintactically correct, and it does not create any errors for most language runtimes.
Furthermore, it produces the expected behavior half the time.

Just now I wasted more than 3 hours for that.
Three years ago, I lost the ACM programming contest for the same bug.

Comments

'A visitor' left this comment on 31 Aug 06
Yes... but will you do it AGAIN?

Mistakes like this are part of the gauntlet of programming... But after a while we train the programmer (and then don't bother with training the language) to not make them. I haven't made that mistake in years (though I did! several times!), but use the if(a = b){...} assignment/test on purpose almost daily.

'Ezra Nugroho' left this comment on 31 Aug 06
arrenlex,

Thanks for catching the typo. It's been a very long day.

'Ezra Nugroho' left this comment on 31 Aug 06
Slimscsi,

Thanks for the comment. This time rhs and lhs were both variables. The fact that I remembered only two experiences of falling into this trap made me reasonably glad. I know that I've done it several times, but not a lot.

'A visitor' left this comment on 30 Aug 06
I don't think that's your only problem. No end quote for the value. The ONE bug that always gets you, eh? Ah, the irony.
'A visitor' left this comment on 30 Aug 06
this is why you ALWAYS put the constant on the left: "if ( 4 == x )" not "if ( x == 4 )"

because "if ( 4 = x )" would be cought at compile time

if both arguments are variables, then you just need to be carefull