Anti-Patterns: Fail the Build Only on Certainties

The first upward import never looks like a disaster. It is one line, it makes a test pass, and the reviewer was in a hurry. A month later there are twelve of them, and nobody can say which came first. The book's chapter on detection argues that dependency direction deserves a different kind of enforcement from everything else.

One import is not "a bit untidy"

Most code smells degrade quality gradually. An upward dependency is different, because the model's promises rest on the dependency graph having no cycles. The book puts it bluntly: a single upward dependency can destroy that acyclic property.

Picture a pure formatting function in Utils that starts importing a repository from Resources because it needs one lookup. Utils can no longer be tested without a database. Resources already depends on Utils, so you now have a loop, and "which layer changes first?" has no answer. The reasoning that lets you predict how far a change will spread assumed that arrows only point one way. In practice the first symptom is rarely a clear failure. It is a circular-import error at start-up, or a unit test that suddenly needs a database.

That is why the book treats dependency violations as critical: they undermine the fundamental invariant, so tolerance is zero. It ranks the other families lower. Layer misuse is serious but can be fixed incrementally during normal work, and structural problems call for an architectural assessment and a dedicated refactoring or migration effort.

Let the machine decide only what it can be sure of

The ranking translates directly into how you configure enforcement. In the book's recommended setup for lvd-lint, only the two dependency rules, upward imports and imports between Tools, fail the build. The remaining rules produce warnings.

The reasoning behind that split, which I would add from general practice, is about trust. A rule that fails the build must be right every time, because a developer blocked by a false positive learns to route around the gate. Import direction is a fact you can read off the source. Whether a handler is "too big" or a file "belongs" in another layer is a judgement, and a judgement is better raised as a warning that a human can weigh.

Because import direction is so mechanical, even a crude checker earns its keep. This toy (not lvd-lint) fails when a lower layer imports a higher one:

import pathlib, re, sys

ORDER = ["schema", "utils", "resources", "tools", "api"]  # low to high
bad = []
for f in pathlib.Path("src").rglob("*.ts"):
    layer = f.parts[1]
    if layer not in ORDER:
        continue
    for m in re.finditer(r'from "@(\w+)/', f.read_text()):
        if m.group(1) in ORDER and ORDER.index(m.group(1)) > ORDER.index(layer):
            bad.append(f"{f}: {layer} imports {m.group(1)}")
print("\n".join(bad))
sys.exit(1 if bad else 0)

It reads only aliased imports, so a real tool would also resolve relative paths and re-exports. Even so, run it in CI and the twelfth import never happens, because the first one never merges. The cost of adding the check is an afternoon; the cost of removing a dozen tangled imports later is a refactoring project.

What the gate cannot see

Notice what a checker like this misses. It cannot tell whether a Utils function is quietly doing I/O under a different name, or whether a new file sits in the layer that matches how often it will change. Those need people, and the book pairs the tooling with a review checklist and with short written records of structural decisions. It also goes further than I have here, rating every anti-pattern by how it is detected, how severe it is and how costly it is to fix.

The takeaway

Spend your zero-tolerance budget on the few rules that protect the structure, and spend your humans on the judgements. The expensive failure is a gate so noisy that it gets switched off. To go further, see lvd-lint, putting LVD checks into CI/CD and detecting dependency cycles.

What the book covers

  • The full set of lvd-lint rules, each mapped to the anti-pattern it detects
  • A ten-item code review checklist for violations static tools cannot see
  • Architecture decision records and how they stop old mistakes returning
  • A consolidated table rating every anti-pattern by detection method, severity and fix effort
  • Exercises on spotting violations in unfamiliar code