Extract Variable, Rename, and Change Function Declaration
What This Concept Is
Three refactor moves that treat naming as a structural concern, not a cosmetic one.
Extract Variable: pull a sub-expression out and give it a name. The name explains why the expression matters.
Rename Variable / Rename Field / Rename Function: change the name of an element everywhere it appears. When the element is published (used by external callers), do it via Change Function Declaration in two phases.
Change Function Declaration: change a function's name, its parameter list, or its return shape. The parallel-change version: add the new form, migrate callers, remove the old form.
Why It Matters Here
Naming is the primary way intent shows up in code. A useful name can reduce reconstruction effort; the benefit is contextual and no fixed time saving is established here. They are also the ones most often skipped because "it still works."
Change Function Declaration is the move that connects small-step refactoring to large-scale API evolution (see Cluster 5: parallel change).
Concrete Example
Extract Variable:
// Before
return order.quantity * order.itemPrice -
Math.max(0, order.quantity - 500) * order.itemPrice * 0.05 +
Math.min(order.quantity * order.itemPrice * 0.1, 100);
// After
const basePrice = order.quantity * order.itemPrice;
const quantityDiscount = Math.max(0, order.quantity - 500) * order.itemPrice * 0.05;
const shipping = Math.min(basePrice * 0.1, 100);
return basePrice - quantityDiscount + shipping;
These expression sketches assume ordinary stable numeric fields. Caching basePrice changes property-read counts; a getter or proxy makes those counts observable. Keep a trace when those objects belong to the supported domain.
Rename (wide-scope variable), using Encapsulate Variable first:
// Step 1: encapsulate
let tpHd = "untitled";
function title() { return tpHd; }
function setTitle(arg) { tpHd = arg; }
// Step 2: rename the storage; callers keep using title()/setTitle()
let _pageTitle = "untitled";
function title() { return _pageTitle; }
function setTitle(arg) { _pageTitle = arg; }
Change Function Declaration (parallel change):
// Existing, external callers
function circum(radius) { return 2 * Math.PI * radius; }
// Phase 1: add new name, keep the old one delegating
function circumference(radius) { return 2 * Math.PI * radius; }
function circum(radius) { return circumference(radius); }
// Phase 2: migrate callers (one commit per batch)
// Phase 3: remove circum only after supported callers migrate; keep it if compatibility requires it.
Common Confusion / Misconception
"Rename is trivial -- just find-and-replace." Even for a local, textual replacement can collide with shadowed names, strings or comments. For a field or function used across a codebase, you are doing a declaration change; treat it as such. Use the IDE's rename refactor, and if the symbol is published, use the two-step delegating version.
Extract Variable vs Extract Function: if the name has meaning only inside this function, use Extract Variable. If the name would be useful to other code in the module, use Extract Function instead (or Replace Temp with Query).
How To Use It
Check Yourself
- When is Extract Variable the wrong move and Extract Function the right one?
- Why is renaming a public API method not a one-step operation?
- A function takes
(x, y)that are always called withcoord.x, coord.y. Which of the three moves fixes this, and which concept in Cluster 4 follows?
Mini Drill or Application
Find a one-line expression of more than ~40 characters in your code. Apply Extract Variable. Now reread the function aloud. If the new names let someone unfamiliar follow the calculation in one pass, stop. If not, keep extracting or consider Extract Function.
Engineering Extension: Keep old consumers visible
A stored JSON key, reflection lookup or external package import may retain the old name after an IDE reports success. Record compatibility aliases and retirement criteria. A renamed exported parameter can affect callers that use named arguments in languages supporting them.
Use the behavior contracts, labs and practice feedback to test the claim.
Read This Only If Stuck
Locate this move through the Refactoring book index. The selective reading guide gives exact consulted local locators and primary documentation. Index links are not exact chapter links.