Layer Misuse: Respecting the Direction but Not the Purpose

The dependency graph is spotless. Every import points downward, the linter is quiet, and still the codebase is hard to change. The cause is not where the arrows go. It is that one of the layers has taken on work that belongs to a different one.

Direction is necessary, not sufficient

The book calls this layer misuse. The imports are all legal, yet the code is asking a layer to do something its specification rules out. Each layer in LVD has a job description. Tools hold focused business operations, Resources insulate you from the outside world, and the API layer translates between a protocol and your business logic. Misuse happens when a layer ignores its own job description, and it is invisible to any tool that only inspects imports.

Three examples show the pattern, one each from Tools, Resources and the API layer.

The tool that does everything

The God Tool is a single tool that handles several unrelated operations and picks between them with a parameter. Imagine a restaurant system with manageReservation(action, params), containing branches for create, move, cancel and refund a deposit. It looks tidy from the outside, since there is one entry point, but the four operations have different preconditions and different failure modes.

Testing means enumerating every action. Adding a fifth action makes an already crowded function worse. A bug fix in the deposit path can break reservation creation because they share a function body. The cure is one tool per operation, with the layer above (the API layer or an agent) doing the dispatching.

What makes this teachable is that the book gives it a countable test. A tool is flagged when it shows at least two out of four listed indicators, and one of them is a cap on cyclomatic complexity per function. The indicators are a way to make "this tool feels too big" into a conversation with evidence.

The resource that leaks its vendor

Resources exist to absorb external volatility. A leaky one does the opposite and passes it upward:

// L3 Resource: the vendor type escapes
async function lookupAddress(q: string): Promise<MapsSdk.GeocodeResponse> {
  return mapsClient.geocode({ query: q });
}

// Better: map it to your own type, defined in Schema
async function lookupAddress(q: string): Promise<Coordinates> {
  const r = await mapsClient.geocode({ query: q });
  return { lat: r.geometry.lat, lng: r.geometry.lng };
}

In the first version every tool that calls lookupAddress now depends on the vendor's type definitions. Switching providers means editing every consumer, even though each only needed two numbers. In the second, the mapping happens inside the Resource, tools see a domain type from the Schema layer, and a provider change touches one file.

The handler that thinks

An API handler (L5) should parse the request, invoke the right tool and format the response. When it also checks stock, applies pricing rules or enforces business policy, three problems follow. The logic cannot be tested without building fake HTTP requests and responses. It cannot be reused when a CLI command, a scheduled job or an MCP tool needs the same behaviour. And the rules end up at the wrong volatility. Business logic changes at roughly the pace of Tools (ν 0.8), but the API layer sits at 0.85, so the rules are filed in the wrong place and tangled with protocol concerns. Moving them into a Tool leaves a handler of about three lines.

The takeaway

If dependency violations are about arrows, layer misuse is about job descriptions. The book ranks it as serious but fixable incrementally, in the course of normal work, which differs from the zero-tolerance stance on import direction. Keep going with how to organise tools, the patterns that keep vendors at arm's length, and how to catch all of this automatically.

What the book covers

  • The God Tool detection criteria and the decomposition into focused tools
  • The corrected Resource that maps a vendor SDK type to a Schema type
  • The corrected API handler reduced to parse, invoke and respond
  • Where layer misuse sits in the severity ranking and how to schedule fixes