Omitting braces {} is valid in multiple statements, such as, for loops, if statements, and while loops. However, enforcing the use of control braces throughout your codebase will make the code more consistent and can make it easier to add statements in the future.
publicclassFoo{intx=0;publicvoidbar(){// while loop - no braces
while(true)x++;// for loop - no braces
for(inti=0;i<42;i++)x++;// if only - no braces
if(true)x++;// if/else - no braces
if(true)x++;elsex--;// do/while - no braces
doi++;while(true);// case - no braces - allowed by default
switch(i){case(i<42):return"foo";default:return"bar";}}}
publicclassFoo{intx=0;publicvoidbar(){// while loop - with braces
while(true){x++;}// for loop - with braces
for(inti=0;i<42;i++){x++;}// if only - with braces
if(true){x++;}// if/else - with braces
if(true){x++;}else{x--;}// do/while - with braces
do{i++;}while(true);// case - with braces
switch(i){case(i<42){return"foo";}default{return"bar"}}}}
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- java-code-style # Rules to enforce Java code style.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines