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 braceswhile(true)x++;// for loop - no bracesfor(inti=0;i<42;i++)x++;// if only - no bracesif(true)x++;// if/else - no bracesif(true)x++;elsex--;// do/while - no bracesdoi++;while(true);// case - no braces - allowed by defaultswitch(i){case(i<42):return"foo";default:return"bar";}}}
publicclassFoo{intx=0;publicvoidbar(){// while loop - with braceswhile(true){x++;}// for loop - with bracesfor(inti=0;i<42;i++){x++;}// if only - with bracesif(true){x++;}// if/else - with bracesif(true){x++;}else{x--;}// do/while - with bracesdo{i++;}while(true);// case - with bracesswitch(i){case(i<42){return"foo";}default{return"bar"}}}}
Seamless integrations. Try Datadog Code Security
Datadog Code Security
Try this rule and analyze your code with Datadog Code Security
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 Security scans to your CI pipelines