Parameterize Function

Removes smells
Compare
Symptom
Human

Two or more near-identical functions that differ only in literal values — `tenPercentRaise(person)` and `fivePercentRaise(person)` differ only in 1.10 vs 1.05.

Agent

The agent finds near-identical functions differing only in literal values; consistency depends on every variant staying in sync.

Goal
Human

Two near-identical functions that differ only in literal values combine into one with a parameter.

Agent

One canonical function with a parameter; the agent reasons about one body and verifies parameter values at call sites.

Before the refactoring

function tenPercentRaise(person) { person.salary *= 1.10; }
function fivePercentRaise(person) { person.salary *= 1.05; }

After the refactoring

function raise(person, factor) { person.salary *= 1 + factor; }
Example source: Illustrative example written for this site, not a quotation from any source.
Pressure
Human

Bug fixes must land in every variant; new variants are new functions; the shared shape isn't expressed anywhere.

Agent

Bug fixes must land in every variant; the agent must find and update each consistently or risk drift.

Tradeoff
Human

If the variations are conceptually different operations, one parameterized function will accumulate flags and special cases — keep them separate then.

Agent

If the variations encode conceptually different operations, the parameterized function grows flags and special cases the agent must thread through — worse than the original duplication.

Relief
Human

One canonical implementation; new variations are new parameter values, not new functions.

Agent

One canonical implementation the agent reasons about; new variations are new parameter values, not new code paths.

Trap
Human

Parameterizing functions whose variations are conceptually different operations — the parameterized function accumulates flags and special cases that overwhelm the original simplicity.

Agent

Parameterizing conceptually different operations accumulates flag-driven branches the agent must reason about — the function becomes a switch statement in disguise.