Recognising Antipatterns
In this pack:
sizing,
notation,
rule strictness.
Overview
A review often comes back with a list of smells: a class that does too
much, code nobody dares delete, an object every test must reset. A
smell is a place to look, not a verdict; the evidence in the code or its
history decides whether it is an antipattern. This skill names each
antipattern from that evidence, sends it to the rule it breaks in the
skill that owns that rule, and fixes the one whose code changes most
first, one at a time behind passing tests.
When to use
- A review names an antipattern, such as a god class, lava flow or
poltergeist, and asks what to do about it.
- A list of code smells must be sorted into the rules each one breaks.
- A team cannot agree which of several smells to fix first.
- A module is hard to change and nobody has said which rule it breaks.
- Code nobody understands is kept in the module and nobody knows whether
it can go.
When not to use
- The antipattern is already named and the question is how to fix it:
the skill that owns the broken rule answers, such as assigning
responsibilities for a class that does too much.
- A smell has no evidence behind it yet, such as a long class that
nobody changes: leave it and look again when a change reaches it.
- The question is the steps of a safe refactoring: that is
refactoring toward object design.
- The question is whether one pattern pays its way: that is choosing a
pattern.
Core rules
Rule. Name an antipattern only with evidence from the code or its
history. A class that most change requests touch, a single instance
that every test must reset, or code that no run has reached in a year
is evidence. A smell is a place to look, as every red flag in this
pack is; the evidence makes it a finding.
Rule. Treat an antipattern by the rule it breaks, in the skill that
owns that rule, rather than by a fix of its own. A god class goes to
assigning responsibilities, rules 7 and 8; an anemic model to deciding
object or data, rule 1; a service locator to inverting dependencies,
rule 5; a single instance reached from anywhere to inverting
dependencies, rules 2 and 3; a dependency cycle to organising modules,
rule 4; a pattern with no force to choosing a pattern, rules 2 and 6;
shotgun surgery to diagnosing coupling, rule 1, and then hiding
implementation, rule 5; a class that only forwards to limiting what an
object knows, rule 3.
Rule. Remove lava flow once evidence shows it does not run. Lava
flow is code that nobody understands, nobody calls on purpose and
nobody dares delete. Delete it, since version control keeps it. Where
it does run, cover what it does with a test before changing it. Code
kept in case it is needed is read by every newcomer and changed in
every refactoring.
Default. Inline a poltergeist: a short-lived class with no state of
its own that is created only to start another object's work and then
dropped. The caller sends that message itself.
The exception is the use-case object that a user interface or an API
calls first, which assigning responsibilities, rule 5, asks for even
when it only delegates.
Judgement. Fix first the antipattern in the code that changes most
often and costs most to change, and leave alone a stable one that no
change needs. What tips the balance is the change history: a god
class that no request touches costs nothing until one does.
Rule. Change one antipattern at a time, in steps that each leave the
tests passing. The steps of a safe refactoring are in
refactoring toward object design.
Pattern
Before, CloseVisit creates an InvoiceStarter for one message.
InvoiceStarter holds no state of its own, lives for one message and is
dropped: a poltergeist. After, CloseVisit sends its invoicing port the
message itself, and InvoiceStarter is gone.
Before:
class InvoiceStarter
- invoicing: Invoicing
+ new(invoicing: Invoicing)
+ start(visit: VisitId): void
invoicing.issueFor(visit)
return
class CloseVisit
- invoicing: Invoicing
+ new(invoicing: Invoicing)
+ run(visit: VisitId): void
starter := new InvoiceStarter(invoicing)
starter.start(visit)
return
After:
class CloseVisit
- invoicing: Invoicing
+ new(invoicing: Invoicing)
+ run(visit: VisitId): void
invoicing.issueFor(visit)
return
Invoicing is taken to be a port whose issueFor issues one invoice for
the visit and raises nothing in this fence. VisitId is the visit's
identity. What issueFor stores, under which identity, where it stores
it and in which transaction are left out of both fences, as is closing
the visit itself. Each version issues one invoice for each run: the
Before sends start once, which sends issueFor once, and the After
sends issueFor once.
Red flags
Places to look, not verdicts. A red flag becomes a finding only with
evidence: a measurement, a reproducible bug, or a failing test.
- A class that most change requests touch.
- An object that looks up its collaborators in a global registry.
- A single instance reached from anywhere and reset in test setup.
- Code that no run reaches and no test covers.
- A class created only to send one message and then dropped.
- One business change that edits many modules.
Common mistakes
- Calling code an antipattern from its size alone.
- Rewriting a stable antipattern that no change needs.
- Fixing several antipatterns in one change.
- Deleting code because nobody remembers it, with no evidence that it
does not run.
- Replacing a single global instance with a service locator.
Related skills
Further reading
- Antipatterns and their rules
- Gathering evidence
1---2name: oo-recognising-antipatterns3description: Use when a review names an antipattern such as a god class, lava flow or poltergeist, or when sorting code smells into the rules they break.4license: MIT5metadata:6 source: https://skillmd.com/skills/yutna/oo-recognising-antipatterns7 version: 0.21.08 pack: object-oriented-design-skills9---1011# Recognising Antipatterns1213**In this pack:**14[sizing](../oo-design/SKILL.md#how-much-design-a-task-warrants),15[notation](../oo-design/references/notation.md),16[rule strictness](../oo-design/SKILL.md#how-strictly-a-rule-is-meant).1718## Overview1920A review often comes back with a list of smells: a class that does too21much, code nobody dares delete, an object every test must reset. A22smell is a place to look, not a verdict; the evidence in the code or its23history decides whether it is an antipattern. This skill names each24antipattern from that evidence, sends it to the rule it breaks in the25skill that owns that rule, and fixes the one whose code changes most26first, one at a time behind passing tests.2728## When to use2930- A review names an antipattern, such as a god class, lava flow or31 poltergeist, and asks what to do about it.32- A list of code smells must be sorted into the rules each one breaks.33- A team cannot agree which of several smells to fix first.34- A module is hard to change and nobody has said which rule it breaks.35- Code nobody understands is kept in the module and nobody knows whether36 it can go.3738## When not to use3940- The antipattern is already named and the question is how to fix it:41 the skill that owns the broken rule answers, such as assigning42 responsibilities for a class that does too much.43- A smell has no evidence behind it yet, such as a long class that44 nobody changes: leave it and look again when a change reaches it.45- The question is the steps of a safe refactoring: that is46 [refactoring toward object design](../oo-refactoring-toward-object-design/SKILL.md).47- The question is whether one pattern pays its way: that is choosing a48 pattern.4950## Core rules51521. Rule. **Name an antipattern only with evidence from the code or its53 history.** A class that most change requests touch, a single instance54 that every test must reset, or code that no run has reached in a year55 is evidence. A smell is a place to look, as every red flag in this56 pack is; the evidence makes it a finding.57582. Rule. **Treat an antipattern by the rule it breaks, in the skill that59 owns that rule, rather than by a fix of its own.** A god class goes to60 assigning responsibilities, rules 7 and 8; an anemic model to deciding61 object or data, rule 1; a service locator to inverting dependencies,62 rule 5; a single instance reached from anywhere to inverting63 dependencies, rules 2 and 3; a dependency cycle to organising modules,64 rule 4; a pattern with no force to choosing a pattern, rules 2 and 6;65 shotgun surgery to diagnosing coupling, rule 1, and then hiding66 implementation, rule 5; a class that only forwards to limiting what an67 object knows, rule 3.68693. Rule. **Remove lava flow once evidence shows it does not run.** Lava70 flow is code that nobody understands, nobody calls on purpose and71 nobody dares delete. Delete it, since version control keeps it. Where72 it does run, cover what it does with a test before changing it. Code73 kept in case it is needed is read by every newcomer and changed in74 every refactoring.75764. Default. **Inline a poltergeist: a short-lived class with no state of77 its own that is created only to start another object's work and then78 dropped.** The caller sends that message itself.7980 The exception is the use-case object that a user interface or an API81 calls first, which assigning responsibilities, rule 5, asks for even82 when it only delegates.83845. Judgement. **Fix first the antipattern in the code that changes most85 often and costs most to change, and leave alone a stable one that no86 change needs.** What tips the balance is the change history: a god87 class that no request touches costs nothing until one does.88896. Rule. **Change one antipattern at a time, in steps that each leave the90 tests passing.** The steps of a safe refactoring are in91 [refactoring toward object design](../oo-refactoring-toward-object-design/SKILL.md).9293## Pattern9495Before, `CloseVisit` creates an `InvoiceStarter` for one message.96`InvoiceStarter` holds no state of its own, lives for one message and is97dropped: a poltergeist. After, `CloseVisit` sends its invoicing port the98message itself, and `InvoiceStarter` is gone.99100Before:101102```text103class InvoiceStarter104 - invoicing: Invoicing105 + new(invoicing: Invoicing)106 + start(visit: VisitId): void107 invoicing.issueFor(visit)108 return109110class CloseVisit111 - invoicing: Invoicing112 + new(invoicing: Invoicing)113 + run(visit: VisitId): void114 starter := new InvoiceStarter(invoicing)115 starter.start(visit)116 return117```118119After:120121```text122class CloseVisit123 - invoicing: Invoicing124 + new(invoicing: Invoicing)125 + run(visit: VisitId): void126 invoicing.issueFor(visit)127 return128```129130`Invoicing` is taken to be a port whose `issueFor` issues one invoice for131the visit and raises nothing in this fence. `VisitId` is the visit's132identity. What `issueFor` stores, under which identity, where it stores133it and in which transaction are left out of both fences, as is closing134the visit itself. Each version issues one invoice for each `run`: the135Before sends `start` once, which sends `issueFor` once, and the After136sends `issueFor` once.137138## Red flags139140Places to look, not verdicts. A red flag becomes a finding only with141evidence: a measurement, a reproducible bug, or a failing test.142143- A class that most change requests touch.144- An object that looks up its collaborators in a global registry.145- A single instance reached from anywhere and reset in test setup.146- Code that no run reaches and no test covers.147- A class created only to send one message and then dropped.148- One business change that edits many modules.149150## Common mistakes151152- Calling code an antipattern from its size alone.153- Rewriting a stable antipattern that no change needs.154- Fixing several antipatterns in one change.155- Deleting code because nobody remembers it, with no evidence that it156 does not run.157- Replacing a single global instance with a service locator.158159## Related skills160161- [oo-design](../oo-design/SKILL.md) sizes a task and defines the rule162 labels.163- [oo-assigning-responsibilities](../oo-assigning-responsibilities/SKILL.md)164 splits a god class and gives a use-case object its job.165- [oo-deciding-object-or-data](../oo-deciding-object-or-data/SKILL.md)166 decides whether an anemic model has a rule with no home.167- [oo-inverting-dependencies](../oo-inverting-dependencies/SKILL.md)168 keeps a service locator and a single global instance out of domain169 code.170- [oo-organising-modules](../oo-organising-modules/SKILL.md) breaks a171 dependency cycle between modules.172- [oo-choosing-a-pattern](../oo-choosing-a-pattern/SKILL.md) asks whether173 a pattern still has a force.174- [oo-diagnosing-coupling](../oo-diagnosing-coupling/SKILL.md) names the175 tie behind shotgun surgery.176- [oo-hiding-implementation](../oo-hiding-implementation/SKILL.md) keeps177 one decision in one module once the tie is named.178- [oo-limiting-what-an-object-knows](../oo-limiting-what-an-object-knows/SKILL.md)179 removes a middle man that only forwards.180- [oo-refactoring-toward-object-design](../oo-refactoring-toward-object-design/SKILL.md)181 takes each change in small steps that keep the tests passing.182183## Further reading184185- [Antipatterns and their rules](references/antipatterns-and-their-rules.md)186- [Gathering evidence](references/gathering-evidence.md)