Video in progress
In a design review, what criteria do you use to answer "do we need a pattern here?"
I start not from the pattern's name but from the axis of change: in which direction and how often the code changes, who changes it, and what a mistake costs. If the pattern makes that change cheaper, I add it; if not, the simple code stays and gets refactored later.
📖 Answer
In reviews I usually see two kinds of mistakes. One is a factory, strategy or plugin system written up front "because we might need it tomorrow", which no second implementation ever uses. The other is a switch copy-pasted a third time that nobody dares to pull into an abstraction.
The questions I ask:
-
What change are we protecting against? "A new payment provider is added every quarter" is a clear axis, and a strategy fits. "Maybe a different database someday" is no axis, and no abstraction is needed.
-
How often does it change? Code that changes once every two years is cheaper to rewrite at that point. The cost of an abstraction is paid by everyone every day, in reading and in debugging.
-
Who changes it? If another team has to extend it, you need a clear extension point. Inside your own team, plain code and good tests are enough.
-
What does a mistake cost? Where money, security or data loss is involved, I choose a stricter structure: state machine transitions, idempotent commands, a design that makes the wrong state impossible.
-
Does the language already give this? In Java 21 many classic patterns got lighter: strategy is a lambda, visitor is
sealed+ a pattern matchingswitch, and builder is, in many cases, a record with named factories. The form from a 2005 book may now be unnecessary.
When a decision is made, I write it into an ADR as one paragraph: what was chosen, for which axis of change, and when we'll revisit it. If a year later that axis has never moved, removing the abstraction is a perfectly normal decision.
The "abstract on the third repetition" rule is a good starting point. But it isn't a mechanical rule: if three similar pieces of code change for three different reasons, merging them costs more than separating them later.
Sign up to mark studied questions and save your progress.