DRY
easy 40 minDon't repeat yourself: every piece of knowledge has one home, and everything else asks that home.
Watch lesson4:18
Problem
Find the fact that lives in several places.
The point: before you extract anything, write down the fact and every place that knows it.
The village hall has a booking program. The task: *emails like abc@ are accepted, and the tax changes from 8 to 8.5 percent on 1 July 2026.* It sounds like two small edits.
Inventory
Who knows what
| Fact | Where it lives | In which form |
|---|---|---|
| A valid email | register, updateEmail, sendReset | Two copies and one rewrite |
| Tax of 8 percent | Order, subscription, service fee | 0.08 three times |
| How to round cents | The same three | Not at all: floats are printed with two digits |
| Password length 8 | register only | One home already |
| Voucher length 8 | Voucher | Another fact that is also 8 |
Python
def order_total(items): subtotal = sum(i["price"] for i in items) return subtotal + subtotal * 0.08def subscription_total(monthly, months): subtotal = monthly * months return subtotal + subtotal * 0.085 # changed here, not in the othersdef service_fee(hours, rate): subtotal = hours * rate return subtotal + subtotal * 0.08print(order_total([{"price": 100}]), subscription_total(50, 2), service_fee(4, 25))Three ways to spend 100, and two different totals. No test fails, because each function is tested against its own number.