Split Temporary Variable
- 2 minutes to read
In This Article
Introduces a new local variable instead of multiple assignments to the same variable.
#Purpose
Sometimes a local variable can take on multiple personalities, playing different roles through the life of a method. This makes code harder to read, because the role of that variable depends on its position in the code. This refactoring cleans up the mess, creating a new local variable for the next assignment.
#Availability
Available from the context menu or via shortcuts:
- when the edit cursor, or caret is on a local variable name within an assignment statement provided that this is not the first assignment to this local.
Note
Sometimes this refactoring may become available on locals that are assigned to multiple times yet play a single consistent role throughout the code block (e.
#Notes
- Split Temporary Variable declares a new local variable above the assignment statement where it was invoked and replaces the assignment statement with an assignment to the new local. All further occurrences of the initial local variable within the method are replaced with the newly declared variable.
- After splitting, Rename is automatically available for the newly declared variable so you can provide a meaningful name for it.
#Options
- You can specify the prefix and suffix that are automatically used to create a default name for the newly declared variable.
#Example
int a;
a = 15;
Method1(a);
│a = 20;
return Method2(a);
Dim a As Integer
a = 15
Method1(a)
│a = 20
Return Method2(a)
Result:
int a;
a = 15;
Method1(a);
int splitA = 20;
return Method2(splitA);
Dim a As Integer
a = 15
Method1(a)
Dim splitA As Integer = 20
Return Method2(splitA)
#Screenshot
See Also