Java Reference
In-Depth Information
}
}
}
private boolean condition() {/* ... */}
// No other method references count
// but several other methods reference MAX_COUNT
}
The reusability of the method is reduced because if the method were copied to another
class, then the count variable would also need to be redefined in the new context. Fur-
thermore, the analyzability of the counter method would be reduced, as whole program
data flow analysis would be necessary to determine possible values for count.
Compliant Solution
In this compliant solution, the count field is declared local to the counter() method:
Click here to view code image
public class Foo {
private static final int MAX_COUNT = 10;
public void counter() {
int count = 0;
while (condition()) {
/* ... */
if (count++ > MAX_COUNT) {
return;
}
}
}
private boolean condition() {/* ... */}
// No other method references count
// but several other methods reference MAX_COUNT
}
Applicability
Detecting local variables that aredeclared inalargerscopethanisrequired bythecodeas
written is straightforward and can eliminate the possibility of false positives.
Search WWH ::




Custom Search