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"}}}}
シームレスな統合。 Datadog Code Security をお試しください
Datadog Code Security
このルールを試し、Datadog Code Security でコードを解析する
このルールの使用方法
1
2
rulesets:- java-code-style # Rules to enforce Java code style.