Section 6.3 Explanatory Variables and the Introduce Variable refactoring
A somewhat related refactoring is the introduce-variable refactoring, the first in many introduce- refactorings we will examine:
The Introduce Variable refactoring entails the creation of a local variable to represent a specific computation, and reusing the variable in other places where the same computation is performed.
The goal of this refactoring is to take some part of a calculation and turn it into a variable. The main reason for doing so is to give that calculation a name. This improves the readability of the code.
Subsection 6.3.1 The Introduce Variable refactoring
As an example, consider the following code. Here
parts
is an array that contains elements of an OrderPart
class, which have pricePerItem
field and a quantity
field. We are using a loop to find the total price of all the items together.int total = 0;
for (int i = 0; i < parts.length; i++) {
total += parts[i].pricePerItem * parts[i].quantity;
}
A simple enough piece of code. We could do it nicer with a
foreach
loop but for now we will use it to demonstrate the Introduce Variable refactoring.In our example we will introduce two new variables. The first will be for
parts[i]
, the i
-th element of the array. We will replace it with a local variable part
like so:int total = 0;
for (int i = 0; i < parts.length; i++) {
OrderPart part = parts[i];
total += part.pricePerItem * part.quantity;
}
We wrote more characters. But I would argue it is perhaps a bit clearer what we are doing. But we can go further, that multiplication deserves its own name:
int total = 0;
for (int i = 0; i < parts.length; i++) {
OrderPart part = parts[i];
int partPrice = part.pricePerItem * part.quantity;
total += partPrice;
}
We make it as clear as day that the multiplication was done to compute the part price! Overkill? Perhaps, but this example is just to illustrate the idea. And that idea is this:
Introduce explanatory variables to clarify complex calculations. Use the Introduce Variable refactoring to achieve that.
I should point out that we will return to this example shortly, when we discuss other refactorings and the "Tell, don’t ask" principle. But for now, let’s wrap up with the steps for an "introduce variable" refactoring.
Subsection 6.3.2 Mechanics of Introduce Variable refactoring
- Copy the part of the computation that you would like to turn into a variable.
- Somewhere before the first occurrence of that computation, declare a new variable and paste in the copied computation to set the variable’s value. You will need to carefully choose a good name for this variable, to clarify the computation.
- In a statically typed system with a smart enough IDE, you can ask the IDE to identify the type of this variable from the calculation. How you do that depends on the system.
- Find all occurrences of this calculation within the current scope, and replace each of them with the variable.
- In some cases you may have to decide if you indeed want to replace all occurrences, or only one.