48

Replace Function with Command


Goal

A function with rich internal state becomes an object whose methods can share that state — easier to extract, name, and test in pieces.

Before the refactoring
function score(c) {
  // fifty lines using ten locals
}
After the refactoring
class Scorer {
  constructor(c) { /* fields */ }
  execute()      { return this.compose(); }
  // named private steps
}
Savings

Long sequences become labeled steps; tests target each step on the command; subclasses or strategies can vary parts of the algorithm.

Note

Promoting a function to a command adds ceremony (constructor, method calls). Only worth it when the function genuinely needs its own intermediate state or multiple entry points.