As for the original question, I agree with the others; two relevant principles here:
- Minimizing Variable Scope
Recall that programming is about communication with the maintainer of the code, more than about communication with the compiler.
In order to help the maintainer as much as possible, we should make sure variables and other identifiers have as small a scope as possible.
The scope of a variable is the physical program text in which it is visible. The smaller to scope of each variable, the fewer the live variables at any particular point in the code.
- Reduce Scope of Variable
// well, OK, this one is a refactoring used to follow the aforementioned principle :-)
You have a local variable declared in a scope that is larger than where it is used
Reduce the scope of the variable so that it is only visible in the scope where it is used