Refactorings for variable scope widening (Widen Scope)
The scope of a variable declaration determines its visibility to the rest of a program. Scopes can be member-level, class-level and nested, where an inner scope may re-declare the meaning of a variable from an outer scope.
When declared inside a member, the scope of the variable is the entire member after the variable declaration, including all nested code blocks. This means that the variable is available to use within the member but when control passes to another member, the variable is unavailable.
A variable can also be declared within a code block. In this case, a variable will only be visible to the code within the block and not available outside of this code block. Code blocks can be nested. For example, a loop within a method within a class provides three levels of nested code blocks and therefore, three levels of nested scope. When a variable is declared within one of these nested scopes, it is visible only to the current scope and any scopes nested within it. This means that a variable declared within a loop is not visible outside of that loop, whereas a variable declared outside of the loop is also visible within the loop.
A wider scoped variable could be declared at the class-level, so that it can be used by any member within the class. In fact, a variable’s scope is always the full extent of the code block it is declared within.
When you want to widen the scope of the variable, you can perform the Widen Scope refactoring to do this automatically. This refactoring moves the variable declaration outside the current scope block. If a variable is initialized with some value, the refactoring automatically determines whether to move the initialization value with the declaration or leave it at the source position:
After it is executed, a navigation marker is dropped at the starting position, so you can navigate back by pressing Esc:
There is an option for this refactoring which specifies whether to increase the scope by one level, or always promote a variable to the maximum visibility (e.g. top of a method). The option is available in the Editor | Refactoring | Widen Scope options page in the CodeRush Options Dialog.
There are two additional modifications of the Widen Scope refactoring:
- Widen Scope (promote to field) – converts a local variable to a field variable. In other words, this refactoring moves a variable to the top class-level scope:
The field variable will be declared with the name corresponding to your Identifiers options and all references to it will be linked together:
- Widen Scope (promote constant) – moves a local constant to a class-level scope and replaces all similar constant values with the reference to a constant:
Once applied, you will be able to replace all similar expressions from the class to the reference on the newly created constant:
When all expressions are replaced, they are linked together as well:
This refactoring is useful when you want to introduce a class-level constant for all similar expressions of the current class.—– Products: Refactor! Pro, CodeRush Xpress Versions: 11.2 and up VS IDEs: any Updated: Jan/24/2012 ID: R050