Marco.org

I’m : a programmer, writer, podcaster, geek, and coffee enthusiast.

C/C++/Objective-C programmers:

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.