Techniquecore
Split phase
Also called split into phases.
Split code that does two different things in sequence into two functions, joined by an intermediate data structure, so that each phase can be read, tested and changed alone.
- Family
- refactoring
- Intent
- Separate the code that prepares the data from the code that uses it, with a data structure between them.
Purpose
Some code does two things one after the other. It reads an input and then computes with it, or it works out what to do and then does it. When the two parts are mixed in one function, a reader must keep both in mind at once, a change to the input format touches the computation, and neither part can be tested alone.
Split phase separates the two into sequential functions. The first phase produces an intermediate data structure; the second phase takes only that structure. The catalogue card shows a line of order code that splits a string, looks up a price and multiplies, in one expression. After the refactoring, a function parseOrder returns a record with productID and quantity, and a function price takes the record and the price list.1
A compiler is a well-known case of the idea. Wikipedia says that a compiler is likely to perform "some or all of the following operations, often called phases". In its example, the scanner "composes a sequence of tokens", and the tokens are then "transformed by the parser into a syntax tree, which is then treated by the remaining compiler phases".2 The book uses this example to explain the refactoring.3 The phases are separate, so each can change alone, and the structure between them is a contract that both sides can test.
Steps
- Run the tests and see them pass.
- Extract the code of the second phase into its own function.3
- Test.
- Add an intermediate data structure as one more parameter of that function. It starts empty.
- Test.
- Look at each other parameter of the second-phase function. If the first phase computes it, move it into the intermediate structure, and test after each.
- Extract the code of the first phase into a function that returns the intermediate structure.3
- Test. The caller now reads as two calls: one that builds the structure, one that uses it.
Example
A payroll function reads lines of text and computes pay with overtime, in one loop. After the refactoring, a parse phase makes records and a pay phase computes from them.
import assert from "node:assert/strict";
const input = "ana,38,2500\nbo,45,2000\ncy,40,3000";
// Before: parsing and paying mixed in one loop. Rates are in cents per hour.
function payrollBefore(text) {
const out = {};
for (const line of text.split("\n")) {
const [name, h, r] = line.split(",");
const hours = Number(h);
const rate = Number(r);
const overtime = Math.max(0, hours - 40);
out[name] = (hours - overtime) * rate + overtime * rate * 1.5;
}
return out;
}
// After, phase 1: text in, records out. It knows the format and nothing about pay.
function parseTimesheets(text) {
return text.split("\n").map((line) => {
const [name, hours, rate] = line.split(",");
return { name, hours: Number(hours), rateCents: Number(rate) };
});
}
// After, phase 2: records in, pay out. It knows the rules and nothing about text.
function computePay(sheets) {
const out = {};
for (const { name, hours, rateCents } of sheets) {
const overtime = Math.max(0, hours - 40);
out[name] = (hours - overtime) * rateCents + overtime * rateCents * 1.5;
}
return out;
}
const payroll = (text) => computePay(parseTimesheets(text));
assert.deepEqual(payroll(input), payrollBefore(input));
assert.deepEqual(payroll(input), { ana: 95000, bo: 95000, cy: 120000 });
// Each phase can now be tested alone, and the input format can change
// without touching the pay rules.
assert.deepEqual(computePay([{ name: "dee", hours: 42, rateCents: 1000 }]), { dee: 43000 });
const fromJson = JSON.parse('[{"name":"ed","hours":10,"rateCents":1000}]');
assert.deepEqual(computePay(fromJson), { ed: 10000 });
console.log("split phase: tests pass");
split phase: tests pass
After the split, a second input format, JSON here, needs only a second phase-one function. The pay rules are tested with records, with no text to parse.
| Language | What the example shows |
|---|---|
| JavaScript | a parse phase and a pay phase, and a second input format, as above |
| Python | the same split, with a dataclass as the intermediate structure |
| Go | the same split, where the parse phase returns errors and the pay phase cannot fail |
from dataclasses import dataclass
INPUT = "ana,38,2500\nbo,45,2000\ncy,40,3000"
@dataclass(frozen=True)
class Timesheet: # the intermediate structure between the phases
name: str
hours: int
rate_cents: int
# Phase 1: text in, records out.
def parse_timesheets(text):
sheets = []
for line in text.splitlines():
name, hours, rate = line.split(",")
sheets.append(Timesheet(name, int(hours), int(rate)))
return sheets
# Phase 2: records in, pay out. Integer cents; overtime is paid at 1.5 times.
def compute_pay(sheets):
pay = {}
for s in sheets:
overtime = max(0, s.hours - 40)
pay[s.name] = (s.hours - overtime) * s.rate_cents + overtime * s.rate_cents * 3 // 2
return pay
result = compute_pay(parse_timesheets(INPUT))
assert result == {"ana": 95000, "bo": 95000, "cy": 120000}
assert compute_pay([Timesheet("dee", 42, 1000)]) == {"dee": 43000}
print(result)
print("split phase: tests pass")
{'ana': 95000, 'bo': 95000, 'cy': 120000}
split phase: tests pass
package main
import (
"fmt"
"strconv"
"strings"
)
// Timesheet is the intermediate structure between the phases.
type Timesheet struct {
Name string
Hours int
RateCents int
}
// Phase 1 knows the format, and it is the only phase that can fail.
func parseTimesheets(text string) ([]Timesheet, error) {
var out []Timesheet
for i, line := range strings.Split(text, "\n") {
f := strings.Split(line, ",")
if len(f) != 3 {
return nil, fmt.Errorf("line %d: want 3 fields, got %d", i+1, len(f))
}
h, err1 := strconv.Atoi(f[1])
r, err2 := strconv.Atoi(f[2])
if err1 != nil || err2 != nil {
return nil, fmt.Errorf("line %d: bad number", i+1)
}
out = append(out, Timesheet{f[0], h, r})
}
return out, nil
}
// Phase 2 knows the rules. Its input is already valid, so it returns no error.
func computePay(sheets []Timesheet) map[string]int {
pay := map[string]int{}
for _, s := range sheets {
overtime := max(0, s.Hours-40)
pay[s.Name] = (s.Hours-overtime)*s.RateCents + overtime*s.RateCents*3/2
}
return pay
}
func main() {
sheets, err := parseTimesheets("ana,38,2500\nbo,45,2000\ncy,40,3000")
if err != nil {
panic(err)
}
pay := computePay(sheets)
fmt.Println(pay["ana"], pay["bo"], pay["cy"])
_, err = parseTimesheets("ana,38")
fmt.Println(err)
fmt.Println("split phase: tests pass")
}
95000 95000 120000
line 1: want 3 fields, got 2
split phase: tests pass
When to use
- One function handles two concerns in sequence, such as reading and computing, or deciding and acting.
- Changes come from two directions. A change to the input format and a change to the rules both touch the same function. This is Divergent Change, and the book names Split Phase as a fix for it.3
- A part cannot be tested alone. If you must build text to test a rule, the rule is mixed with the parsing.
- After a transform. Combine Functions into Transform often makes the first phase: the enriched record is the intermediate structure.
Pitfalls
A leaky structure. If the second phase still reads the raw input, or the first phase computes results of the second, the split is only in name. Each parameter of the second phase should come from the intermediate structure or from data that the first phase never touches.
Errors in the wrong phase. Validation of the input belongs in the first phase. If the second phase finds a malformed value, the first phase let it through.
Too many phases. A split for a function of ten lines that never changes adds a type and a function and gains little. The refactoring pays when the phases change for different reasons.
Cost of the intermediate structure. The first phase builds the whole structure before the second phase starts. For a large input, this can use much more memory than the mixed loop. A stream or an iterator between the phases keeps the separation without holding all the data.
Tools that do it
This refactoring is a sequence of steps, not one action of a tool. Each Extract Function step can use the extract action of the IDE, and Introduce Parameter Object can make the intermediate structure. The choice of where one phase ends and the other starts is the programmer's.
References
- Martin Fowler, Catalog of Refactorings, Split Phase, the entry
- Wikipedia, Compiler, the list of compiler operations, and the example of the scanner and the parser
- Martin Fowler, Refactoring, Improving the Design of Existing Code, second edition, Addison-Wesley, 2018, Chapter 6, A First Set of Refactorings, Split Phase, motivation and mechanics; Chapter 3, Bad Smells in Code, Divergent Change
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag 4WBAB8 Edit this page History Report an error Markdown