Move Statements into Function

Removes smells
Compare
Symptom
Human

The same setup (logging, validation, metric tick) or follow-up code appears around every call to a function — repeated boilerplate at every call site.

Agent

The agent finds the same setup or follow-up code around every call to a function; consistency depends on every caller remembering the pattern.

Goal
Human

Setup or follow-up that happens around every call to a function moves inside the function, so the caller's contract shrinks.

Agent

The function owns its setup and follow-up; the agent verifies behavior at the function definition instead of auditing every call site.

Before the refactoring

log('start fetch');
const data = fetch(url);
log('start fetch');
const data2 = fetch(url2);

After the refactoring

function fetchLogged(url) {
log('start fetch');
return fetch(url);
}
Example source: Illustrative example written for this site, not a quotation from any source.
Pressure
Human

Every caller must remember the boilerplate; missing it produces inconsistent behavior the function doesn't enforce; updates to the boilerplate touch every caller.

Agent

Every caller is a chance to miss the boilerplate or misorder it; the agent verifying consistency must check every site individually.

Tradeoff
Human

If the moved statements aren't always wanted, the function grows a flag argument — verify every caller really needs the moved behavior.

Agent

If some callers genuinely don't want the moved behavior, the function grows a flag argument and the agent must reason about which mode each caller wants.

Relief
Human

One fewer thing to remember at the call site; consistency is enforced by the function's definition, not by convention.

Agent

The agent reasons about the function's full contract from its definition; consistency is enforced by the function, not by convention.

Trap
Human

Moving statements into a function that some callers don't actually want — the function grows a flag argument to make the moved behavior conditional, which is a worse smell.

Agent

Moving statements into a function some callers don't want adds a flag argument the agent must thread through every call site — substitutes one boilerplate for another.