A local variable assigned once from a computation, then referenced one or more times within the same function — `const basePrice = qty * itemPrice;` used multiple times in the body.
The agent finds a local variable assigned once from a computation and referenced multiple times; the temp's existence couples the rest of the function to the computation's locality.
A local variable assigned once from a computation becomes a function that returns that computation on demand.
Computations become named queries the agent can reference by name from anywhere; functions decompose without dragging the temp's lifetime.
Before the refactoring
function bill() {const basePrice = qty * itemPrice;if (basePrice > 1000) return basePrice * 0.95;return basePrice;}
After the refactoring
function bill() {if (basePrice() > 1000) return basePrice() * 0.95;return basePrice();}function basePrice() { return qty * itemPrice; }
The temp's lifetime constrains how the surrounding function can be split; Extract Function must drag the temp along; the named computation is locked inside one function.
The agent extracting parts of the function must thread the temp through every extracted helper; the named computation can't be reused outside the function.
If the temp wraps an expensive calculation called many times, naive replacement may multiply cost — measure or cache before deciding.
If the temp wraps an expensive calculation called many times, naive replacement multiplies cost; the agent verifying performance must measure or cache before substituting.
Extract Function becomes easier (the query has a name and stable scope); the temp's lifetime no longer constrains how the surrounding function is split.
The value is recomputed from its source at every read; the agent does not track a temp's binding across reads to predict staleness, and the query is callable from any site without the binding's scope constraint.
Replacing every temp with a query — including ones wrapping an expensive computation referenced many times — multiplies runtime cost without any structural gain.
Replacing temps that wrap expensive computations called many times multiplies runtime cost the agent's local tests may not catch.