DRY
easy 40 minDon't repeat yourself: every piece of knowledge has one home, and everything else asks that home.
Watch lesson4:18
Wrong abstraction
The helper with six flags, and the way back.
The point: a shared function that needs to know who is calling is not shared knowledge. Put it back, then look again.
Failure walk
How one helper grew
| Change | Edit | Effect |
|---|---|---|
| Invoice and receipt look alike | render(doc) is shared | Good so far |
| Receipt has no address | Flag with_address | One branch |
| Invoice needs a due date | Parameter due | Receipt passes None |
| Refund note is negative | Flag is_refund | The sign is flipped in two places |
| Receipt printer has 32 columns | Parameter width | Invoice code now wraps too |
| A fix for receipts | Edit in the shared wrap | Invoices lose a column. Nobody expected it |
Python
def render(lines, total, with_address=False, address=None, due=None, is_refund=False, width=None): out = [] if with_address and address: out.append(address) for name, cents in lines: c = -cents if is_refund else cents row = f"{name} {c / 100:.2f}" out.append(row[:width] if width else row) t = -total if is_refund else total out.append(f"TOTAL {t / 100:.2f}") if due and not is_refund: out.append("due " + due) return out# 1. What is truly shared? Only how money is written.def money(cents): return f"{cents / 100:.2f}"# 2. Three plain functions. Each contains only its own lines.def invoice(lines, total, address, due): return [address, *[f"{n} {money(c)}" for n, c in lines], "TOTAL " + money(total), "due " + due]def receipt(lines, total, width): return [*[f"{n} {money(c)}"[:width] for n, c in lines], "TOTAL " + money(total)]def refund(lines, total): return [*[f"{n} {money(-c)}" for n, c in lines], "TOTAL " + money(-total)]# 3. Pinned: the same answers as before for the three real uses.L = [("Hall, 2 hours", 4000), ("Tea", 350)]assert invoice(L, 4350, "Mill Lane 4", "1 July") == render(L, 4350, True, "Mill Lane 4", "1 July")assert receipt(L, 4350, 12) == render(L, 4350, width=12)assert refund(L, 4350) == render(L, 4350, is_refund=True)print("\n".join(receipt(L, 4350, 12)))Look at the pinned receipt: Hall, 2 hour has lost its price. The old helper did that too. The split did not fix it, and did not hide it. Now it can be fixed in receipt without fear for invoices.
A shared helper gets its fourth flag. What now?
BreaksAdd the flag
- How
- It is only one more line.
- Limit
- Paths double with each flag. Tests do not.
- Choose when
- Never for a flag that names a caller.
WorksPass the difference in
- How
- Callers hand in a function for the part that differs.
- Limit
- Good for one difference. With four, the helper becomes a puzzle.
- Choose when
- The frame is stable and one step varies.
StrongInline, trim, share again
- How
- Copy the helper into each caller, delete dead branches, then extract only what is still equal.
- Limit
- For an hour there is more code. Keep the pinned tests.
- Choose when
- Flags say who is calling.