Item 57: Minimize the scope of local variables
57. Minimize the scope of local variables
- The most effective technique for minimizing the scope of a local variable is to declare it just before it is first used.
- Local variables should basically be initialized at the time of declaration.
- In the loop statement, the for statement should be selected from the while statement.
- Because the for statement can define ad hoc variables to be used repeatedly, while the while statement cannot.
- The for statement is less likely to make mistakes due to copying. (Syntax is fairly fixed)
- Methods should be united smaller to minimize the scope of local variables.