Replace Temp with Query

Referenced by patterns
Symptom

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.

Goal

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; }
Example source: Illustrative example written for this site, not a quotation from any source.
Pressure

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.

Tradeoff

If the temp wraps an expensive calculation called many times, naive replacement multiplies cost; the agent verifying performance must measure or cache before substituting.

Relief

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.

Trap

Replacing temps that wrap expensive computations called many times multiplies runtime cost the agent's local tests may not catch.