replace-constructor-with-factory-function

star 0

Apply Replace Constructor with Factory Function when you see Primitive Obsession, Speculative Generality. Construction goes through a named factory the agent can extend with validation, polymorphism, or caching as one location.

wallacedrew By wallacedrew schedule Updated 5/30/2026

name: replace-constructor-with-factory-function description: Apply Replace Constructor with Factory Function when you see Primitive Obsession, Speculative Generality. Construction goes through a named factory the agent can extend with validation, polymorphism, or caching as one location.

Apply: 32 — Replace Constructor with Factory Function

Announce first: name the smell you see and that you're applying Replace Constructor with Factory Function before any edit. The user reads the announcement as your contract.

Or decline first: if you don't apply Replace Constructor with Factory Function, name the decline type — catalog miss, taste call, cost-benefit, constraint-blocked, or insufficient context — so the user knows which counterargument applies.

Symptom: The agent finds constructors used directly where construction needs validation, subclass selection, or caching — none of which constructors can express.

Goal: Construction goes through a named factory the agent can extend with validation, polymorphism, or caching as one location.

// Avoid:
const employee = new Employee(name, 'engineer', salary);

// Prefer:
function createEngineer(name, salary) {
  return new Employee(name, 'engineer', salary);
}
const employee = createEngineer(name, salary);

Pressure: The agent's reasoning about construction must consider pre-construction logic scattered at every call site; constructors hide the capability for the patterns the code actually needs.

Tradeoff: The factory hides the actual class from callers; the agent must ensure the factory's name still expresses the produced shape clearly or call sites become opaque.

Relief: The agent extends construction in one place; consumers don't depend on which concrete class they're getting.

Trap: Wrapping every constructor in a factory adds an indirection layer the agent must navigate without buying any new construction capability.

Removes smells: Primitive Obsession, Speculative Generality

Install via CLI
npx skills add https://github.com/wallacedrew/ritl --skill replace-constructor-with-factory-function
Repository Details
star Stars 0
call_split Forks 0
navigation Branch main
article Path SKILL.md
More from Creator