Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This noncompliant code example shows a variable count that is declared outside the counter method, although the variable is not used outside the counter method. This reduces the reusability of the method because count variable would need to be redefined in the new context.

...

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 */
}

Risk Assessment

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

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

DCL14-J

low

unlikely

medium

P2

L3

...

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

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this guideline on the CERT website.

Bibliography

Wiki Markup
\[[Bloch 2001|AA. Bibliography#Bloch 01]\] Item 29, Minimize the scope of local variables
\[[JLS 2005|AA. Bibliography#JLS 05]\] [Section§ 14.4.2|http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.4.2], "Scope of Local Variable Declarations"

...