Over a million developers have joined DZone.
Platinum Partner

Omitting Braces: Not Just A Matter Of Style

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

According to our analysis of more than 100 Java projects, one of the most violated rules from static analysis, is 'missing braces in If statement'. Although often considered to be merely a stylistic preference, we found that omitting braces is actually a good indicator of probable buggy code.

For example, when developers need to add to existing code and forget to add braces where the original statement only contained a single line of code, e.g.


//original code
if (condition)
doSomething();

//new code
if (condition)
doSomething();
doSomethingElse();

The call to 'doSomethingElse()' is only meant to execute if 'condition' is true. However, omitting the braces in the If statement means doSomethingElse() will execute regardless of t he value of 'condition', hence the bug.

It may seem obvious but after that large lunch on a Friday.....who knows ;-)

From our individual metrics studies we found the Halstead Program Volume was the best individual metric for finding files that might contain bugs.

This means that, if you know nothing else about the file, a high Program Volume metric means the file is more likely to contain a bug. Our ongoing research indicates that a Program Volume value of 4000 reflects a 50% likelihood that a bug is present in the code. A Value of 5790 increases the likelihood to 90%.

For more details on this and other metrics that are indicators of possibly buggy code read our technical paper.

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}