AlgoViz

DRY

easy 40 min

Don'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

InputPathResult
abc@name abc, domain empty, parts ['']Refused in all three callers
Noor@Hall.Example Trim, split, lower case the domainNoor@hall.example
100.00 on 30 June 2026Row 1: 800 points, 800 cents of tax108.00
100.00 on 1 July 2026Row 2 has started: 850 points108.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")
The rule has many tests. Each caller has one or two. Add a new caller and you add one test, not twenty.
Edit and run it in your browser

Guard

Search for what should be gone

  1. 1

    Search the number

    Look for 0.08, 1.08, 8 % and 800. Knowledge hides in other forms.

  2. 2

    Search the idea

    Look for every "@" in the code. A rewrite has other text and the same purpose.

  3. 3

    Search outside the code

    Web form, database check, help page, mail templates. They may hold the rule as well.

  4. 4

    Close the door

    Accept EmailAddress and not str in the methods behind the border. The next copy will not fit.