Combine Functions into Transform

Compare
Symptom
Human

Multiple consumers compute the same derived values from the same source data, each independently re-deriving the result.

Agent

The agent encounters consumers each independently computing the same derived values from the same source; reasoning about consistency requires tracing every derivation.

Goal
Human

Multiple derived values from the same source come from one transform that produces an enriched record.

Agent

One transform produces the enriched record from the input; the agent reads one input-to-shape contract and consumers read named output fields without simulating the derivation.

Before the refactoring

function baseCharge(reading) { return reading.kwh * reading.tariff.baseRate; }
function taxableCharge(reading) { return reading.kwh * reading.tariff.taxRate; }
// every consumer recomputes:
const monthly = baseCharge(reading) + taxableCharge(reading);
const discount = baseCharge(reading) * 0.95;

After the refactoring

function enrich(reading) {
return {
...reading,
baseCharge: reading.kwh * reading.tariff.baseRate,
taxableCharge: reading.kwh * reading.tariff.taxRate,
};
}
const r = enrich(reading);
const monthly = r.baseCharge + r.taxableCharge;
const discount = r.baseCharge * 0.95;
Example source: Illustrative example written for this site, not a quotation from any source.
Pressure
Human

Derivations drift between consumers as they age; bugs hide where two consumers compute slightly different versions; performance suffers from redundant computation.

Agent

Each consumer re-derives the same values; the agent can't trust them to be consistent and must verify equivalence on every change.

Tradeoff
Human

Building a transform up-front when only one derivation exists is BDUF — wait for the second derivation before introducing the transform.

Agent

Building the transform when only one consumer exists creates an intermediate type the agent must learn before its second use justifies it.

Relief
Human

Derivations stay consistent (no two callers compute slightly different versions); cache invalidation becomes obvious.

Agent

Derivations are consistent by construction; the agent reads field accesses on the enriched record instead of computing across the codebase.

Trap
Human

Building a transform up-front when only one consumer derives anything — BDUF abstraction that adds an intermediate type without an active second user.

Agent

Pre-building transforms for hypothetical consumers creates intermediate shapes the agent must understand and maintain with no active reuse.