Versions Compared

Key

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

Scope minimization helps to capture common programming errors, improves code readability by tying together the declaration and actual use of a variable and eases maintainability since because unused variables are easily caught and removed.

...

This noncompliant example shows a variable that is declared outside the for loop. This can harm reusability since as the loop index i will change after the for statement. Consider for instance, if the case when this code snippet is copy pasted with the intent of using a different index j but the statement mistakenly still iterates over index i. Since As i is still in scope, this will lead to a rather obtuse outcome.

...

Additionally, methods should be designed for only one operation if possible. This simplicity avoids variables from existing in overlapping scopes and prevents errors.

Risk Assessment

Using a larger scope than necessary results in less reliable code.

...