Replace Command with Function

Compare
Symptom
Human

A command class whose execute() method does everything in one shot — fields and methods exist but no real sub-step structure justifies the class.

Agent

A command class whose execute() does the work in one shot with no sub-step decomposition; callers go through construct-then-call ceremony for what could be one function.

Goal
Human

A command object whose execute() does everything in one shot collapses back to a plain function.

Agent

The command collapses to a plain function; the agent's call sites become direct invocations.

Before the refactoring

class ChargeCalculator {
constructor(c, o) { this.c = c; this.o = o; }
execute() { return this.c.base + this.o.tax; }
}
new ChargeCalculator(c, o).execute();

After the refactoring

function charge(c, o) { return c.base + o.tax; }
charge(c, o);
Example source: Illustrative example written for this site, not a quotation from any source.
Pressure
Human

Callers go through build-then-execute ceremony for what could be one function call; the class adds file count and indirection without internal decomposition benefit.

Agent

Every caller pays the construct-then-call hop; the agent reasoning about behavior loads a class to find a single function.

Tradeoff
Human

If the command holds genuinely useful intermediate state, flattening to a function regrows the temps it eliminated — confirm there's no real reuse first.

Agent

If the command held genuinely useful intermediate state, collapsing regrows the temps it eliminated; the agent verifying the collapse must check whether any internal decomposition is load-bearing.

Relief
Human

Fewer files, fewer constructors, less indirection — the caller sees one function instead of build-then-execute.

Agent

Fewer files; shorter call stacks; the agent's plan-and-execute loop touches the function directly without the construct-then-call hop.

Trap
Human

Collapsing a command that holds genuinely useful intermediate state — flattening it forces the agent to regrow the temps the command eliminated and re-thread them through helper params.

Agent

Collapsing commands that genuinely decomposed into named sub-steps regrows the temps they eliminated and the function-with-many-locals smell returns.