Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Omitting Braces: Not Just A Matter Of Style

DZone's Guide to

Omitting Braces: Not Just A Matter Of Style

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

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.

Are you using Bitbucket to accomplish your company's mission? Share your company’s mission with #Forthecode for a chance to be featured on our homepage, our social media channels, or win a free t-shirt!

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

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

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}