Loops

Apply refactorings
Compare
Symptom
Human

Imperative for/while loops obscuring what the loop is producing — filter, map, reduce mixed together by hand.

Agent

Imperative for/while loops where filter, map, and reduce concerns are mixed by hand; the agent cannot tell what the loop is producing without mentally executing it.

Goal
Human

The transformation reads as a sequence of named operations: filter, map, reduce.

Agent

The transformation reads as a sequence of named operations; the agent recognizes the shape (filter, map, reduce) without simulating the loop.

Smellier version

const seniors = [];
for (const u of users) {
if (u.age >= 65) seniors.push(u.name);
}

Fresher version

const seniors = users
.filter(u => u.age >= 65)
.map(u => u.name);
Example source: Illustrative example written for this site, not a quotation from any source.
Pressure
Human

Reader must mentally execute the loop to learn the result; off-by-one errors and accumulator bugs hide in the body.

Agent

The agent must mentally execute the loop to learn its result; off-by-one and accumulator bugs hide in the body and only surface at test time.

Tradeoff
Human

Pipelines (filter/map/reduce) introduce per-element function-call overhead and force the reader to track intermediate collection shapes; in tight loops the overhead is real.

Agent

Pipeline form adds per-element call overhead and forces the agent to track intermediate collection types through the chain; for hot paths the runtime cost matters.

Relief
Human

Intent reads in domain language; bugs concentrate on individual steps that are independently testable.

Agent

Each pipeline stage carries a typed input and output; the agent verifies one stage at a time against its signature instead of simulating the full loop body to predict the result.

Trap
Human

Forcing every loop into a pipeline — including loops with early-exit, accumulator-with-side-effects, or genuinely sequential dependencies — produces .reduce() bodies that are harder to read than the original.

Agent

Forcing every loop into a pipeline, including ones with early-exit, side-effecting accumulators, or sequential dependencies, produces .reduce() bodies whose accumulator state and per-iteration side effects the agent has to simulate at every read; the simulation cost exceeds what the original loop's straight-line control flow required.