Over a million developers have joined DZone.

Omitting Braces: Not Just A Matter Of Style

· Java Zone

Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience, brought to you in partnership with JetBrains

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.

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.

Topics:

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

{{ parent.tldr }}

{{ parent.urlSource.name }}