Replace Parameter with Query

Removes smells
Compare
Symptom
Human

A function that takes a parameter the caller computed from data already available to the function — `discounted(order, basePrice, level)` where the function can compute basePrice and level itself.

Agent

A function takes parameters its callers computed from data the function already has access to; the agent verifying any call must reproduce the caller's computation.

Goal
Human

When a function can compute its own answer from already-available state, callers don't have to pre-compute it.

Agent

The function computes its own answer; the agent calls it without pre-computing the inputs.

Before the refactoring

const basePrice = order.qty * order.itemPrice;
const level = discountLevel(order);
const final = discounted(order, basePrice, level);

After the refactoring

const final = discounted(order); // computes basePrice and level itself
Example source: Illustrative example written for this site, not a quotation from any source.
Pressure
Human

Every caller pays the homework cost; signatures inflate with values the function can derive; the duplication of derivation logic scatters.

Agent

Every caller pays the homework cost; the duplication of derivation logic scatters and the agent must keep callers in sync with the function's expectations.

Tradeoff
Human

If the query has side effects or is expensive, passing the value is genuinely better — only replace when the query is pure and cheap.

Agent

If the query is expensive or has side effects, replacing the parameter multiplies cost or introduces hidden coupling the agent must reason about.

Relief
Human

Signatures shrink; consumers stop doing the function's homework.

Agent

Signatures shrink; the agent calls the function directly without reproducing caller-side derivations.

Trap
Human

Replacing parameters with queries when the query is expensive or has side effects — multiplies cost and introduces hidden coupling.

Agent

Replacing parameters with queries that are expensive or have side effects multiplies cost or introduces hidden coupling the agent reads as 'just a parameter swap'.