The Folder-as-Layer Principle

A new engineer opens the repository and wants to know where the refund logic lives. In many codebases the honest answer is "search for it". LVD's folder-as-layer principle aims for a different answer: you can tell what a file is, what it may import and who may import it, from nothing more than its path.

The principle in one sentence

The directory structure should directly mirror the layer architecture. Each layer gets a folder at the top of the source tree, named for the layer and listed in order, so that the picture you draw on a whiteboard and the picture your editor shows are the same picture.

Here is how that might look for an imaginary ticketing service, with a plausible feature folder or two inside each layer:

src/
  schema/events/          event and ticket types
  utils/pricing/          pure fee and discount functions
  resources/payments/     payment-provider client
  tools/refunds/          refund one ticket
  api/rest/               HTTP endpoints
  mcp/                    tool descriptors for agents
  skills/                 usage notes for agents
  agents/                 support assistant
  ui/features/checkout/   checkout screens

Nothing about this layout is clever, and that is deliberate. The cleverness lives in the rule that connects the folders.

What you gain

The book lists four benefits of the arrangement:

  • Immediate visibility. The architecture is visible in the file tree, with no diagram needed.
  • Editor support. Folder structure already drives navigation and search, so the layers come along for free.
  • A clear build order. The folders line up with the direction of dependencies.
  • Easy tooling. Rules can be written in terms of paths.

The last benefit is the one with the most leverage. If the layer of a file is determined by where it sits, a checker never has to infer what a file is for. It reads the import statements, looks at the folder each one points into, and applies the rule for that pair of folders. The book shows this with import rules for lvd-lint that give each folder an allow list and a deny list. The Schema folder may import from no other layer, Utils may import only from Schema, and so on up the stack.

The UI is the one layer that gets a second level of prescribed structure inside its folder, because a frontend is a substantial codebase with its own gradient from slow-moving design-system parts to weekly-changing screens. The UI also gets three rules of its own beyond the plain layer ordering: a boundary rule that enforces Axiom A6, an ordering rule inside the UI folder, and a rule that keeps features from importing one another.

Discoverability and predictability

The second set of benefits is about the daily experience of working in the code. Discoverability means you know where to look. Need a type, look in schema/. Need a pure calculation, look in utils/. Need data access, resources/. Need business logic, tools/.

Predictability is the mirror image: you know where new code should go before you write it. A new kind of product extends the schema folder. A new fee calculation is added to the pricing utilities. A new payment provider goes into the resources folder. A reviewer does not have to ask "why is this here?", because the answer is in the path.

The book also argues that this speeds up onboarding, since a newcomer can learn the layer model first and then navigate by folder, making a first contribution guided by the structure. It reports some measurements for this, which are not repeated here, because they are best read in the context of how the book measured them.

Getting there from an existing codebase

You do not need a big-bang rewrite. The migration path the book describes has four steps:

  1. Create the folder structure.
  2. Move components over gradually, starting with Schema because it has the fewest dependencies, then Utils, which depends only on Schema, then Resources, Tools and API. Use version control's move command so that history follows the files.
  3. Update the imports, ideally with automated refactoring tools, adjusting path aliases in your compiler configuration, and verify with lvd-lint.
  4. Keep verifying continuously by adding the linter to your pipeline, so regressions are caught and improvement can be measured over time.

The order matters. Starting from the bottom means each file you move depends only on layers that have already been moved, or on its own, so the new structure stays consistent at each step.

The takeaway

Putting the architecture in the file system makes it cheap to see, cheap to enforce and cheap to teach. It is the same move as Löwy's project-to-project rule, applied to folders: make the structure physical so people do not have to remember it. For the enforcement tool see lvd-lint, and for the planning side see the nine-step migration roadmap. The layers themselves are introduced in the nine layers explained.

What the book covers

  • The full directory layout for a nine-layer project and how tests and package files fit around it
  • Import rules for the layers expressed as allow and deny lists for lvd-lint
  • The three UI-specific lint rules and the prescribed folder structure inside the UI layer
  • A four-step migration path with commands for moving an existing codebase