Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Changed to Applicability and updated some references to Java 7

...

Code Block
bgColor#ccccff
public class Foo {
  static private final int MAX_COUNT;

  public void counter() {
    int count = 0;
    while (condition()) {
      /* ... */
      if (count++ > MAX_COUNT) return;
    }
  }

  /* No other method references count */
  /* but several other methods reference MAX_COUNT */
}

...

Applicability

Do not use a larger scope than necessary because it will result in less reliable code.

Guideline

Severity

Likelihood

Remediation Cost

Priority

Level

DCL55-JG

low

unlikely

medium

P2

L3

...

Detecting local variables that are declared in a larger scope than is required by the code as written is straightforward and can eliminate the possibility of false positives.

Detecting multiple for statements that use the same index variable is straightforward; it will produce false positives in the unusual case where this was intended by the programmer.

Related Guidelines

C Secure Coding Standard: DCL19-C. Minimize the scope of variables and functions C++ Secure Coding Standard: DCL07-CPP. Minimize the scope of variables and methods

Bibliography

[Bloch 2001]

Item 29, Minimize the scope of local variables

[JLS 20052011]

§14.4.2, "Scope of Local Variable Declarations"

 

DCL63-JG. Avoid cyclic dependencies between packages      01. Declarations and Initialization (DCL)      DCL51-JG. Do not shadow or obscure identifiers in subscopes

...