SOLID in one page
medium 45 minFive principles for classes that can be changed without fear: single responsibility, open/closed, Liskov substitution, interface segregation and dependency inversion.
Watch lesson5:16
Modules and services
The same five questions one floor higher, with a test for the arrows.
The point: most damage in big programs is done between packages. The five questions work there too, and a build can check them.
Translate
From class to package to service
| Letter | Class | Package | Service |
|---|---|---|---|
| S | One actor | Released together, used together | One team, one ability |
| O | New class | New plugin package | New consumer of an event, producer unchanged |
| L | Keeps the contract | New version accepts old calls | New deployment understands old messages |
| I | Small interface | No need to pull a mail library to get a date helper | An endpoint per client need, not one giant answer |
| D | Port owned by the rules | Core imports no framework | Schema owned by the business, not by the broker |
Packages of the alert program
import astFILES = { "core.service": "from core.ports import Sender", "core.ports": "from dataclasses import dataclass", "adapters.email": "import smtplib\nfrom core.ports import Sender", "adapters.sms": "from core.ports import Sender\nfrom adapters.email import clean", "main": "from core.service import AlertService\nfrom adapters.email import EmailSender",}ALLOWED = {"core": {"core"}, "adapters": {"core"}, "main": {"core", "adapters"}}OURS = set(ALLOWED)def imports(src): for n in ast.walk(ast.parse(src)): if isinstance(n, ast.ImportFrom): yield n.module if isinstance(n, ast.Import): yield from (a.name for a in n.names)for file, src in FILES.items(): here = file.split(".")[0] for target in imports(src): there = target.split(".")[0] if there in OURS and there not in ALLOWED[here]: print("FORBIDDEN", file, "->", target) if here == "core" and there in {"smtplib", "requests", "sqlite3"}: print("CORE KNOWS A DETAIL", file, "->", target)It prints one line: the SMS adapter imports a helper from the email adapter. Small, and it means that nobody can delete email any more. The helper moves to a place that both may know, or is written twice: two lines are cheaper than an arrow.
Failure walk
Liskov between two versions
| Time | Producer | Consumer | Result |
|---|---|---|---|
| 10:00 | v1 sends severity: critical | v1 | Fine |
| 10:05 | v2 sends severity: CRITICAL | v1 compares with critical | Alerts are dropped. The new version gives less than it promised |
| 10:06 | half v1, half v2 | v1 | Some alerts arrive. The worst kind of bug |
| 10:20 | back to v1 | v1 | Fine, 15 minutes lost |
| Better | v2 adds severity_code and keeps severity | v1 ignores the new field | Both work, then consumers move, then the old field goes |
Change a contract that others use
Bring it into a big code base
- 1
Draw the arrows that exist
From imports, not from memory.
- 2
Choose one border
The one with the most incidents, for example payments.
- 3
Forbid new wrong arrows
The existing ones go on a list that may only shrink.
- 4
Give it an owner
A border without a team grows over.
- 5
Measure the aim
Files touched per change, or teams per release. Not the number of interfaces.