Techniquecore
Rename variable
Also called rename local variable.
Give a variable a name that says what it holds, and change every reference in its scope, with care for names that are shadowed and for references that a tool cannot see.
- Family
- refactoring
- Intent
- Replace a variable name that does not say what the variable holds with one that does.
Purpose
A name is the first thing a reader sees of a variable, and often the only thing. If the name is a, the reader must find the assignment to know what a holds. If the name is area, the reader knows at the use. The catalogue card shows this case: let a = height * width; becomes let area = height * width;.1
Rename variable changes the name at the declaration and at each reference. A good name is often found only after the code is written and read, so a rename is a normal part of the work, not a sign that the first name was careless. The book treats it as one of the first fixes for Mysterious Name.2
How much care a rename needs depends on the scope. A variable in a one-line function is seen whole, and a short name can be enough. A field of a record that is stored, sent or used across many modules needs a careful name and a careful rename.2
Steps
For a variable with a small scope:
- Run the tests and see them pass.
- Change the name at the declaration.
- Change each reference in the scope. A tool that knows the language does this in one step.
- Test.
For a variable with a wide scope:
- If many parts of the program use it, first apply Encapsulate Variable. Then the rename is a rename of the functions and the variable behind them.2
- If the variable does not change after it is set, copy it to a variable with the name you want, move the references one at a time, testing after each, and delete the old name.2
- If the name is part of a published interface, such as a field in a stored file or an API, the rename is a change of format. Keep both names for a time, or leave the old one.
Example
A rename by search and replace changes text, not names. A rename that knows the scope changes only the references.
import assert from "node:assert/strict";
// Before: the reader must find the assignment to know what a is.
function roomBefore(height, width) {
const a = height * width;
return `room of ${a} square metres`;
}
// After: the name says what the value is.
function room(height, width) {
const area = height * width;
return `room of ${area} square metres`;
}
assert.equal(room(3, 4), roomBefore(3, 4));
// A rename by text, not by scope: count -> total also changes the string.
const source = 'const count = 2; console.log("count: " + count);';
const byText = source.replaceAll("count", "total");
assert.equal(byText, 'const total = 2; console.log("total: " + total);');
// The message that users see has changed; a rename by scope leaves strings alone.
const byScope = 'const total = 2; console.log("count: " + total);';
assert.notEqual(byText, byScope);
console.log("rename variable: tests pass");
rename variable: tests pass
The second part shows why the tools work on the syntax tree. A text replace cannot tell the variable count from the word "count" in a message, from a comment, or from another variable in another scope with the same name.
| Language | What the example shows |
|---|---|
| JavaScript | a rename, and a text replace that changes a string, as above |
| Python | a rename that a tool cannot finish, because a name is used as a string in getattr |
| Go | a rename that would capture an outer variable, which gopls refuses |
# Before and after a rename of a field.
class OrderBefore:
def __init__(self, q):
self.q = q # q: the number of items
class Order:
def __init__(self, quantity):
self.quantity = quantity
# Code that reaches the field by a string. A rename tool that works on
# the syntax tree does not see "q" here as a reference to the field.
def read_field(obj, name):
return getattr(obj, name)
assert read_field(OrderBefore(3), "q") == 3
try:
read_field(Order(3), "q") # the string was not renamed
raise AssertionError("expected the old name to fail")
except AttributeError as e:
print("missed by the rename:", e)
# The fix: change the strings by hand, and search for the old name as text.
assert read_field(Order(3), "quantity") == 3
print("rename variable: tests pass")
missed by the rename: 'Order' object has no attribute 'q'
rename variable: tests pass
package main
import "fmt"
// Before: the inner variable is y. The reference to x reaches the parameter.
func before(x int) int {
total := 0
for y := 0; y < 3; y++ {
total += x * y
}
return total
}
// A rename of y to x by text: the inner x shadows the parameter,
// and x * x is not the product that the code meant.
func renamedByText(x int) int {
total := 0
for x := 0; x < 3; x++ {
total += x * x
}
return total
}
// A rename by scope to a name that is free: the behaviour stays.
func renamedByScope(x int) int {
total := 0
for step := 0; step < 3; step++ {
total += x * step
}
return total
}
func main() {
fmt.Println(before(10), renamedByText(10), renamedByScope(10))
if before(10) != renamedByScope(10) || before(10) == renamedByText(10) {
panic("unexpected results")
}
fmt.Println("rename variable: tests pass")
}
30 5 30
rename variable: tests pass
When to use
- The name does not say what the variable holds, or says something that is no longer true.
- The name is the type, not the role.
listorstrsays less thanpendingOrdersorcustomerName. - After you learn more. A first name often comes from the first use of the value. When other uses appear, a better name may too.
- Before an extraction. A good name for a variable is often the name for the function that Extract Function makes from the code around it.
Pitfalls
Shadowing and capture. A rename can make two variables share a name, so that a reference that meant one now reaches the other. gopls checks for this: "changing a name may cause a symbol to become “shadowed”, so that some existing references are no longer in scope", and it reports an error.3
References that the tool cannot see. A name in a string, in a reflection call, in a template, in a configuration file or in a serialized record is not a reference in the syntax tree. The tool does not find it.
Wide renames. IntelliJ IDEA notes that renaming local variables "can be done easily inline since only the limited scope is affected", while renaming classes or public methods "could potentially impact a lot of files", and it shows a preview of the changes.4
A name that is worse. A long name is not always clearer. In a two-line loop, i is a known convention; currentIndexIntoTheArray makes the loop harder to read.
Tools that do it
| Tool | Languages | What it automates |
|---|---|---|
| IntelliJ IDEA | Java, Kotlin, and others | Rename, to "change names of symbols, files, directories, packages, modules and all the references to them throughout code"4 |
| gopls | Go | the LSP rename, with a check that "takes great care to detect situations in which renaming might introduce a compilation error"3 |
| VS Code | TypeScript, JavaScript | Rename with F2, "across your TypeScript project"5 |
Each of these tools renames by scope, not by text. None of them can see the references that are only strings.
References
- Martin Fowler, Catalog of Refactorings, Rename Variable, the entry
- Martin Fowler, Refactoring, Improving the Design of Existing Code, second edition, Addison-Wesley, 2018, Chapter 6, A First Set of Refactorings, Rename Variable, motivation and mechanics; Chapter 3, Bad Smells in Code, Mysterious Name
- gopls documentation, Code transformation features, Rename
- IntelliJ IDEA documentation, Rename refactorings, the introduction
- Visual Studio Code documentation, Refactoring TypeScript and JavaScript, Rename
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag C17WE0 Edit this page History Report an error Markdown