Detecting Dependency Cycles Before They Ship
Why dependency cycles hurt builds, tests and comprehension, and how a depth-first search or Tarjan's algorithm finds them in linear time.
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.
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.
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.
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.
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.
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.
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.
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.