...
This noncompliant code example shows a variable that is declared outside the for
loop. This reduces reusability because the value of the loop index i
will have changed after the for
statement. Consider, for instance, the case when this code snippet is copied and pasted with the intent to use a different index j
. If the index variable change is were omitted, the new loop would then attempt to iterate over index i
. Unexpected behavior can follow because i
remains in scope.
...
Using a larger scope than what is necessary results in less reliable code.
...