Architecture Verification: Turning a Diagram into a Test

A diagram in a wiki is a promise that nobody checks. It is drawn when the system is young, remembered when it is convenient, and contradicted by the code within a quarter. The practical payoff of the graph theory in Chapter 3 is that the promise can be turned into a test that fails the build, which is the only kind of promise that survives a busy year.

A two-step check

The book's verification routine takes two inputs, the dependency graph extracted from the code and a volatility value for every component, and applies two checks in a deliberate order.

  1. Cycles first. If any exist, the check fails immediately and reports them. There is no point reasoning about ordering in a graph that has loops.
  2. Then every edge. For each dependency, compare the volatility of the component doing the depending with that of the component depended on. If the first is lower, that edge is a violation, and the report lists it together with the two values.

If neither check finds anything, the architecture passes. The structure is simple enough to write down in a few lines. Here is a test in Python that treats the layer order as the ranking, with the loading helpers left to your own tooling:

RANK = {"schema": 1, "utils": 2, "resources": 3, "tools": 4, "api": 5}

def upward_edges(edges, layer_of):
    return [(a, b) for a, b in edges
            if RANK[layer_of[a]] < RANK[layer_of[b]]]

def test_no_upward_imports():
    edges = load_import_edges("src")
    layers = load_layer_map("layers.yml")
    assert upward_edges(edges, layers) == []

That is the idea in miniature. In practice you would use a purpose-built tool, and for LVD that is lvd-lint, covered in its own article. The point is how little logic the check needs once the graph exists, and that the failure message names the exact edge that broke the rule.

When the check fails: three ways out

The chapter also offers guidance for the commonest failure, a cycle among components. It names three refactorings, each of which dissolves a different kind of loop.

  • Extract the common dependency. If the members of the loop all lean on some shared piece, move that piece to a lower layer where everyone can use it and none of them depends on the others.
  • Invert the dependency. If two components need each other, introduce an interface that both depend on. The loop becomes a pair of arrows pointing at a shared contract.
  • Merge the components. If two things are so tightly coupled that they cannot be separated, admit that they were one component all along.

It is worth asking, in order, whether the shared thing can move down, whether a contract can sit between the two, and only then whether the parts should merge. Merging is the final resort because it gives back the modularity you were trying to build.

Letting the numbers suggest the layers

A third application runs the other way. Given components with measured volatilities and a target number of layers, the book's layer-assignment procedure sorts the components by volatility, slices the sorted list into the desired number of groups, and then refines the boundaries so that they respect natural clusters in the data. Cutting into even slices is only a starting point. If your measurements show three clear bunches, the boundaries should fall in the gaps between them, not through the middle.

That refinement step matters. It acknowledges that a layer is a cluster of things that behave alike, not an arbitrary bucket.

What the chapter adds up to

The chapter closes with a compact summary: dependency graphs model component relationships formally, acyclic graphs can be put in a topological order, cycles can be found in linear time, volatility metrics quantify change rates, and volatility ordering keeps an architecture stable. Together they enable automated verification, objective quality metrics, systematic refactoring guidance and layer assignment. The book lists three key theorems behind it: that an acyclic graph has a topological ordering, that cycle detection is linear, and the conditions for a volatility-compatible ordering.

The takeaway

An architecture you can verify is one that stays the architecture you drew. Extract the graph, check for loops, check every edge, and report failures by name. Then wire the check into your pipeline, as described in putting LVD checks into CI/CD. For how the numbers behind the edge check are measured, see measuring volatility and ordering components.

What the book covers

  • The full verification algorithm with its reporting of violations
  • The layer-assignment algorithm for slicing components into layers by volatility
  • Worked before-and-after forms of the three cycle-breaking refactorings
  • The chapter summary and its list of key theorems