Why wouldn't you use brackets?

Published 2011-08-23 on Farid Zakaria's Blog

Brackets and Braces

I wonder why some programmers are so averse to using curly braces and brackets in all situations to explicitly state intended behavior. Without explicit curly braces, you can run into the following scenarios. Here are two examples which didn't pop up right away during a quick code review but were found later and could have caused some havoc.


//Scenario #1
int value = isVersion2()?1:0 & canHardwareSupportFeature();

//Scenario #2
if ( condition ) {
//code
} else

if (condition 2) {
}

For Scenario #1, aside from the fact that the conditional operator is not needed, the problem was the priority of the conditional operator compared to that of the bit-AND operator. It might not stand out immediately depending on the spacing because you might read the operators priority differently.Scenario #2 has the problem of secretly hiding the second if condition under the else. Whether or not it is the intended functionality, it's just plain confusing.

I like the idea of -Wparentheses being turned on...

Quick Aside

Has anyone had any experience using Lint or adding it to a large existing project? I remember having used it at a previous co-op and although a lot of the code had to include pre-processor directives in order to tell Lint that these are not actual errors, I thought the overall quality of the software improved due to it's use.