Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
Metadata
ID:java-code-style/control-statement-braces
Language: Java
Severity: Notice
Category: Code Style
Description
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 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