Versions Compared

Key

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

...

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

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

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

Compliant Solution

In this compliant solution, the count field is declared local to the counter method.

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 a larger scope than is necessary results in less reliable code.

...