Split Temporary Variable
- 2 min to read
Introduces a new local variable instead of multiple assignments to the same variable.
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.
- 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.
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.g., accumulating a total or checking for errors). It is unnecessary to split the local when used in this manner.
- 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.
- You can specify the prefix and suffix that are automatically used to create a default name for the newly declared variable.
int a; a = 15; Method1(a); a = 20; return Method2(a);
Dim a As Integer a = 15 Method1(a) a = 20 Return Method2(a)
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)