Too many control variables in for loop

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: java-best-practices/forloop-variable-count

Language: Java

Severity: Warning

Category: Best Practices

Description

The presence of multiple control variables in a for loop can obscure the range of values over which the loop iterates and make your code brittle and difficult to debug.

To follow this rule, regular for loops should have only one control variable.

Arguments

  • max-variables: Maximum number of control variables. Default: 1.

Non-Compliant Code Examples

public class Main {
   public void foo() {
      for (int i = 0, j = 0; i < 10; i++, j += 2) { // more than one control variable
         foo();
      }
   }
}

Compliant Code Examples

public class Main {
   public void foo() {
      for (int i = 0; i < 10; i++) {
         foo();
      }
   }
}
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 Analysis