C/C++/Objective-C programmers:
huge:
Tired of these bugs?
if (x = 5) { /* Oops, accidentally set x to 5 */ }Use GCC’s -Wparentheses option, which will generate warnings whenever you do this. (In Xcode, it’s in Project Info: GCC 4.0 - Warnings: Missing Braces and Parentheses.)
If you actually intend for the assignment to be the evaluated value in a conditional, just give it an extra set of parentheses:
if ( (x = 5) ) { /* That's better */ }The extra spaces are optional. That’s just the style I like for these constructs to make it very clear during code reading that you intended to do this.
Long ago I got in the habit of ALWAYS putting the constant on the LHS.
if (5 = x) will never compile, arguments or not!
That doesn’t help if they are both variables! e.g. (y=x)