...
Code Block | ||
---|---|---|
| ||
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 | ||
---|---|---|
| ||
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.
...