DRY
easy 40 minDon't repeat yourself: every piece of knowledge has one home, and everything else asks that home.
Watch lesson4:18
Verify
Trace values, and prove that no copy is left.
The point: test the rule once and fully. Test the callers only for what is theirs.
Trace
Four inputs by hand
| Input | Path | Result |
|---|---|---|
abc@ | name abc, domain empty, parts [''] | Refused in all three callers |
Noor@Hall.Example | Trim, split, lower case the domain | Noor@hall.example |
| 100.00 on 30 June 2026 | Row 1: 800 points, 800 cents of tax | 108.00 |
| 100.00 on 1 July 2026 | Row 2 has started: 850 points | 108.50 |
Python
def parse(raw): text = (raw or "").strip() name, at, domain = text.rpartition("@") parts = domain.split(".") if not (at and name) or " " in text or len(parts) < 2 or "" in parts: raise ValueError("email: expected name@domain") return name + "@" + domain.lower()good = {"a@b.c": "a@b.c", " Noor@Hall.Example ": "Noor@hall.example", "x@y@z.org": "x@y@z.org"}bad = [None, "", "abc", "abc@", "@hall.example", "a b@hall.example", "a@hall", "a@.example", "a@hall."]for raw, want in good.items(): assert parse(raw) == wantfor raw in bad: try: parse(raw) raise AssertionError("accepted " + repr(raw)) except ValueError: passprint(len(good) + len(bad), "cases, one place")Guard
Search for what should be gone
- 1
Search the number
Look for
0.08,1.08,8 %and800. Knowledge hides in other forms. - 2
Search the idea
Look for every
"@"in the code. A rewrite has other text and the same purpose. - 3
Search outside the code
Web form, database check, help page, mail templates. They may hold the rule as well.
- 4
Close the door
Accept
EmailAddressand notstrin the methods behind the border. The next copy will not fit.