Refactoring Toward Object Design
In this pack:
sizing,
notation,
rule strictness.
Overview
Working code can take a better shape without changing what the clinic
sees. This skill separates the two kinds of change: one step alters
structure, the next alters behaviour, never both at once. It pairs every
move with a rule of this pack and a run of the tests, so each step can
be judged and undone on its own. It also says where to stop, at the rule
the code now keeps, at the pattern whose force has gone, or before code
that no change needs.
When to use
- A method, class or module has become hard to read while it must keep
working through the change.
- A refactoring branch has run for days and no longer merges cleanly.
- A change is about to land in code whose shape makes it touch many
places.
- A pattern's force has appeared or gone, and the code around it must
follow.
- A step needs a name before anyone takes it.
When not to use
- The code has no tests and must change now: pinning what it does before
any step is in
working with legacy code.
- The change adds or alters behaviour, such as a new clinic rule: the
skill that owns that rule shapes it, and any refactoring is a separate
step before or after.
- The question is which antipattern to treat first: that is
recognising antipatterns.
- The question is whether a pattern pays its way at all: that is
choosing a pattern.
Core rules
Rule. Change structure and behaviour in separate steps. A
refactoring step changes nothing a test or a caller can observe. A
change in behaviour is a step of its own, made before or after the
refactoring, never inside the same step.
Rule. Refactor only under tests that pass before the first step
and after every step, and keep what those tests check the same. A
step may rename or move the messages a test sends, never what the test
expects. A test that must change what it expects for a refactoring was
checking the inside, which
testing object-oriented code
rule 1 forbids; change that test first, in a step of its own.
Default. Take the smallest named move that leaves the code
working, and run the tests after each move. Examples are renaming,
extracting a method, moving a method, extracting a class, inlining a
method or a class, and replacing a primitive with a value.
The exception is a move the editor's own refactoring tool makes and
checks, such as a rename across the whole code base: it is one step
however many lines it touches.
Rule. Aim each move at a rule of this pack, and stop when the code
keeps it. A method that uses another object's data more than its own
moves to that object
(limiting what an object knows
rule 5). A primitive that carries a domain rule becomes a value
(modeling values and entities
rule 5). The same switch on a type in more than one place becomes one
class per case
(replacing conditionals with polymorphism
rule 1). A chain of messages through other objects becomes one message
to the nearest object
(limiting what an object knows
rule 2). A class changed for two kinds of request splits in two
(assigning responsibilities
rules 7 and 8). A subclass that refuses its parent's messages stops
extending the parent, or the parent's contract widens
(honouring substitutability
rule 7). A smell that
recognising antipatterns
names, such as a god class, is sorted there first.
Rule. Move toward a pattern only while its force is present, in
moves that each keep the tests passing, and stop as soon as the force
is resolved. Choosing a pattern
rule 2 asks for that evidence. Move away from a pattern the same way
once its force has gone, as choosing a pattern, rule 6, asks.
Default. Refactor the code a change is about to touch before making
the change. First reshape it until the change fits in one place,
with the tests passing; then make the change as a step of its own.
The exception is code no change needs, which
recognising antipatterns
rule 5 leaves alone.
Rule. When a step breaks a test, undo the step and take a smaller
one. Never fix forward on top of a failing test, because each
further step hides which move broke it.
Pattern
Before, TransportDesk asks a TransportRequest for its data and
decides which fare applies. After, the request answers its own fare and
the desk delegates.
Distance and Fare are values taken as given. FareTable is taken to
answer each fare from the clinic's price list and to change nothing. Both
fences store nothing.
Before:
interface FareTable
+ standardFare(distance: Distance): Fare
+ wheelchairFare(distance: Distance): Fare
class TransportRequest
- distance: Distance
- wheelchair: Boolean
+ new(distance: Distance, wheelchair: Boolean)
+ distance(): Distance
return distance
+ needsWheelchair(): Boolean
return wheelchair
class TransportDesk
- fares: FareTable
+ new(fares: FareTable)
+ fareFor(request: TransportRequest): Fare
if request.needsWheelchair() then
return fares.wheelchairFare(request.distance())
else
return fares.standardFare(request.distance())
TransportDesk.fareFor uses the request's data more than its own
(limiting what an object knows
rule 5).
After:
interface FareTable
+ standardFare(distance: Distance): Fare
+ wheelchairFare(distance: Distance): Fare
class TransportRequest
- distance: Distance
- wheelchair: Boolean
+ new(distance: Distance, wheelchair: Boolean)
+ fare(fares: FareTable): Fare
if wheelchair then
return fares.wheelchairFare(distance)
else
return fares.standardFare(distance)
class TransportDesk
- fares: FareTable
+ new(fares: FareTable)
+ fareFor(request: TransportRequest): Fare
return request.fare(fares)
The moves between the fences, each followed by a run of the tests, are:
move the body of fareFor to a new method TransportRequest.fare(fares)
and let fareFor send it; then remove distance() and
needsWheelchair() once no caller sends them. Whether TransportDesk
keeps fareFor is a later step, since a class that only forwards is
limiting what an object knows, rule 3's question. Each version answers
one fare for each fareFor and sends one message to fares for it.
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 commit that renames, moves and changes behaviour at once.
- A refactoring branch whose tests were edited to pass.
- A long run of edits between two runs of the tests.
- A pattern introduced with one case behind it.
- A failing test left while further moves continue.
- A structure change with no test covering the code it moves.
Common mistakes
- Rewriting a module in one step and calling it a refactoring.
- Fixing a bug found along the way inside a refactoring step.
- Changing what a test expects so that a move passes.
- Moving toward a pattern beyond the point where its force is resolved.
- Refactoring code that no planned change touches.
Related skills
Further reading
- Smells and moves
- Toward and away from a pattern
- A worked example of a fat controller
1---2name: oo-refactoring-toward-object-design3description: Use when restructuring working code without changing what it does, when a refactoring grew too big to finish, or when moving toward a pattern.4license: MIT5metadata:6 source: https://skillmd.com/skills/yutna/oo-refactoring-toward-object-design7 version: 0.21.08 pack: object-oriented-design-skills9---1011# Refactoring Toward Object Design1213**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## Overview1920Working code can take a better shape without changing what the clinic21sees. This skill separates the two kinds of change: one step alters22structure, the next alters behaviour, never both at once. It pairs every23move with a rule of this pack and a run of the tests, so each step can24be judged and undone on its own. It also says where to stop, at the rule25the code now keeps, at the pattern whose force has gone, or before code26that no change needs.2728## When to use2930- A method, class or module has become hard to read while it must keep31 working through the change.32- A refactoring branch has run for days and no longer merges cleanly.33- A change is about to land in code whose shape makes it touch many34 places.35- A pattern's force has appeared or gone, and the code around it must36 follow.37- A step needs a name before anyone takes it.3839## When not to use4041- The code has no tests and must change now: pinning what it does before42 any step is in43 [working with legacy code](../oo-working-with-legacy-code/SKILL.md).44- The change adds or alters behaviour, such as a new clinic rule: the45 skill that owns that rule shapes it, and any refactoring is a separate46 step before or after.47- The question is which antipattern to treat first: that is48 [recognising antipatterns](../oo-recognising-antipatterns/SKILL.md).49- The question is whether a pattern pays its way at all: that is50 [choosing a pattern](../oo-choosing-a-pattern/SKILL.md).5152## Core rules53541. Rule. **Change structure and behaviour in separate steps.** A55 refactoring step changes nothing a test or a caller can observe. A56 change in behaviour is a step of its own, made before or after the57 refactoring, never inside the same step.58592. Rule. **Refactor only under tests that pass before the first step60 and after every step, and keep what those tests check the same.** A61 step may rename or move the messages a test sends, never what the test62 expects. A test that must change what it expects for a refactoring was63 checking the inside, which64 [testing object-oriented code](../oo-testing-object-oriented-code/SKILL.md)65 rule 1 forbids; change that test first, in a step of its own.66673. Default. **Take the smallest named move that leaves the code68 working, and run the tests after each move.** Examples are renaming,69 extracting a method, moving a method, extracting a class, inlining a70 method or a class, and replacing a primitive with a value.7172 The exception is a move the editor's own refactoring tool makes and73 checks, such as a rename across the whole code base: it is one step74 however many lines it touches.75764. Rule. **Aim each move at a rule of this pack, and stop when the code77 keeps it.** A method that uses another object's data more than its own78 moves to that object79 ([limiting what an object knows](../oo-limiting-what-an-object-knows/SKILL.md)80 rule 5). A primitive that carries a domain rule becomes a value81 ([modeling values and entities](../oo-modeling-values-and-entities/SKILL.md)82 rule 5). The same switch on a type in more than one place becomes one83 class per case84 ([replacing conditionals with polymorphism](../oo-replacing-conditionals-with-polymorphism/SKILL.md)85 rule 1). A chain of messages through other objects becomes one message86 to the nearest object87 ([limiting what an object knows](../oo-limiting-what-an-object-knows/SKILL.md)88 rule 2). A class changed for two kinds of request splits in two89 ([assigning responsibilities](../oo-assigning-responsibilities/SKILL.md)90 rules 7 and 8). A subclass that refuses its parent's messages stops91 extending the parent, or the parent's contract widens92 ([honouring substitutability](../oo-honouring-substitutability/SKILL.md)93 rule 7). A smell that94 [recognising antipatterns](../oo-recognising-antipatterns/SKILL.md)95 names, such as a god class, is sorted there first.96975. Rule. **Move toward a pattern only while its force is present, in98 moves that each keep the tests passing, and stop as soon as the force99 is resolved.** [Choosing a pattern](../oo-choosing-a-pattern/SKILL.md)100 rule 2 asks for that evidence. Move away from a pattern the same way101 once its force has gone, as choosing a pattern, rule 6, asks.1021036. Default. **Refactor the code a change is about to touch before making104 the change.** First reshape it until the change fits in one place,105 with the tests passing; then make the change as a step of its own.106107 The exception is code no change needs, which108 [recognising antipatterns](../oo-recognising-antipatterns/SKILL.md)109 rule 5 leaves alone.1101117. Rule. **When a step breaks a test, undo the step and take a smaller112 one.** Never fix forward on top of a failing test, because each113 further step hides which move broke it.114115## Pattern116117Before, `TransportDesk` asks a `TransportRequest` for its data and118decides which fare applies. After, the request answers its own fare and119the desk delegates.120121`Distance` and `Fare` are values taken as given. `FareTable` is taken to122answer each fare from the clinic's price list and to change nothing. Both123fences store nothing.124125Before:126127```text128interface FareTable129 + standardFare(distance: Distance): Fare130 + wheelchairFare(distance: Distance): Fare131132class TransportRequest133 - distance: Distance134 - wheelchair: Boolean135 + new(distance: Distance, wheelchair: Boolean)136 + distance(): Distance137 return distance138 + needsWheelchair(): Boolean139 return wheelchair140141class TransportDesk142 - fares: FareTable143 + new(fares: FareTable)144 + fareFor(request: TransportRequest): Fare145 if request.needsWheelchair() then146 return fares.wheelchairFare(request.distance())147 else148 return fares.standardFare(request.distance())149```150151`TransportDesk.fareFor` uses the request's data more than its own152([limiting what an object knows](../oo-limiting-what-an-object-knows/SKILL.md)153rule 5).154155After:156157```text158interface FareTable159 + standardFare(distance: Distance): Fare160 + wheelchairFare(distance: Distance): Fare161162class TransportRequest163 - distance: Distance164 - wheelchair: Boolean165 + new(distance: Distance, wheelchair: Boolean)166 + fare(fares: FareTable): Fare167 if wheelchair then168 return fares.wheelchairFare(distance)169 else170 return fares.standardFare(distance)171172class TransportDesk173 - fares: FareTable174 + new(fares: FareTable)175 + fareFor(request: TransportRequest): Fare176 return request.fare(fares)177```178179The moves between the fences, each followed by a run of the tests, are:180move the body of `fareFor` to a new method `TransportRequest.fare(fares)`181and let `fareFor` send it; then remove `distance()` and182`needsWheelchair()` once no caller sends them. Whether `TransportDesk`183keeps `fareFor` is a later step, since a class that only forwards is184limiting what an object knows, rule 3's question. Each version answers185one fare for each `fareFor` and sends one message to `fares` for it.186187## Red flags188189Places to look, not verdicts. A red flag becomes a finding only with190evidence: a measurement, a reproducible bug, or a failing test.191192- A commit that renames, moves and changes behaviour at once.193- A refactoring branch whose tests were edited to pass.194- A long run of edits between two runs of the tests.195- A pattern introduced with one case behind it.196- A failing test left while further moves continue.197- A structure change with no test covering the code it moves.198199## Common mistakes200201- Rewriting a module in one step and calling it a refactoring.202- Fixing a bug found along the way inside a refactoring step.203- Changing what a test expects so that a move passes.204- Moving toward a pattern beyond the point where its force is resolved.205- Refactoring code that no planned change touches.206207## Related skills208209- [oo-design](../oo-design/SKILL.md) sizes a task and defines the rule210 labels.211- [oo-testing-object-oriented-code](../oo-testing-object-oriented-code/SKILL.md)212 keeps tests on what a caller can see, so a refactoring leaves them213 unchanged.214- [oo-recognising-antipatterns](../oo-recognising-antipatterns/SKILL.md)215 names the smell behind a move and orders the fixes.216- [oo-choosing-a-pattern](../oo-choosing-a-pattern/SKILL.md) asks217 whether a pattern's force is present, and when it has gone.218- [oo-limiting-what-an-object-knows](../oo-limiting-what-an-object-knows/SKILL.md)219 moves a method to the object whose data it uses, and removes a class220 that only forwards.221- [oo-modeling-values-and-entities](../oo-modeling-values-and-entities/SKILL.md)222 decides when a primitive becomes a value.223- [oo-replacing-conditionals-with-polymorphism](../oo-replacing-conditionals-with-polymorphism/SKILL.md)224 gives each case of a repeated switch its own class.225- [oo-assigning-responsibilities](../oo-assigning-responsibilities/SKILL.md)226 splits a class that changes for two kinds of request.227- [oo-honouring-substitutability](../oo-honouring-substitutability/SKILL.md)228 decides when a subclass stops extending its parent.229- [oo-working-with-legacy-code](../oo-working-with-legacy-code/SKILL.md)230 pins what untested code does before the first refactoring step.231232## Further reading233234- [Smells and moves](references/smells-and-moves.md)235- [Toward and away from a pattern](references/toward-and-away-from-a-pattern.md)236- [A worked example of a fat controller](references/worked-example-a-fat-controller.md)