Over a million developers have joined DZone.

Omitting Braces: Not Just A Matter Of Style

· Java Zone

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

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.

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}