This product is not supported for your selected Datadog site. ().

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.

Non-Compliant Code Examples

public class Foo {     int x = 0;      public void bar() {         String message;         if (randomNumber < something)             message = "foo";         else             message = "bar";          if (randomNumber < something) {             message = "foo";         }         else             message = "bar";          if (randomNumber < something)             message = "foo";         else {             message = "bar";         }     } } 
public class Foo {     int x = 0;      public void bar() {         // while loop - no braces         while (true)             x++;          // for loop - no braces         for (int i = 0; i < 42; i++)             x++;          // if only - no braces         if (true)             x++;                  // if/else - no braces         if (true)             x++;          else              x--;                  // do/while - no braces         do             i++;         while (true);                  // case - no braces - allowed by default         switch(i) {             case (i < 42):                 return "foo";             default:                  return "bar";         }     } } 

Compliant Code Examples

public class Foo {     List list = new ArrayList();      public void bar() {         String message;         if(list.size() == 0) {             message = "empty";         } else if (list.size() == 1) {             message = "solo";         } else {             message = "multiple";         }     } } 
public class Foo {     int x = 0;      public void bar() {         // while loop - with braces         while (true) {             x++;         }          // for loop - with braces         for (int i = 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"             }         }     } } 
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Security