AlgoViz

SOLID in one page

medium 45 min

Five 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

LetterClassPackageService
SOne actorReleased together, used togetherOne team, one ability
ONew classNew plugin packageNew consumer of an event, producer unchanged
LKeeps the contractNew version accepts old callsNew deployment understands old messages
ISmall interfaceNo need to pull a mail library to get a date helperAn endpoint per client need, not one giant answer
DPort owned by the rulesCore imports no frameworkSchema owned by the business, not by the broker
The L row is the one that stops outages: old and new run side by side during each deployment, so each must accept the other's data. See API versioning.

Packages of the alert program

No arrow leaves the core. Two adapters do not know each other.
Python
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)
D as a build step: a list of allowed arrows, checked against the imports.
Edit and run it in your browser

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

TimeProducerConsumerResult
10:00v1 sends severity: criticalv1Fine
10:05v2 sends severity: CRITICALv1 compares with criticalAlerts are dropped. The new version gives less than it promised
10:06half v1, half v2v1Some alerts arrive. The worst kind of bug
10:20back to v1v1Fine, 15 minutes lost
Betterv2 adds severity_code and keeps severityv1 ignores the new fieldBoth work, then consumers move, then the old field goes
Adding an optional field is like a weaker precondition. Changing the meaning of a field is a broken postcondition.

Change a contract that others use

Bring it into a big code base

  1. 1

    Draw the arrows that exist

    From imports, not from memory.

  2. 2

    Choose one border

    The one with the most incidents, for example payments.

  3. 3

    Forbid new wrong arrows

    The existing ones go on a list that may only shrink.

  4. 4

    Give it an owner

    A border without a team grows over.

  5. 5

    Measure the aim

    Files touched per change, or teams per release. Not the number of interfaces.