Product wants refunds. Fair enough, it's one feature. You open the repo and start adding files: a route in controllers/, the logic in services/, a query in repositories/, a new field in models/. That's four folders for one idea, and the rest of the team is editing every one of them this sprint too.
Nobody really decided this. It came from the first folder someone created on day one, probably copied from a framework tutorial. This post compares the three common ways to group code (by layer, by feature, by business domain), what each one costs as the codebase grows, and how to keep the boundaries real once several teams share the repo.
Folders decide where a change lands
It's tempting to treat folder layout as tidiness, something you argue about once and forget. In practice it decides three things you feel every week:
- How far one change spreads. If code that changes together lives in different folders, every feature becomes a trip across the tree, and every PR touches files other people are also editing.
- How fast people find things. A new teammate asks "where's the billing code?" One folder is an answer. "Some of it's in
services/, some inmodels/, and check utils too" is a scavenger hunt. - How safely you can delete. When a feature gets retired, can you remove it in one go, or does half of it stay behind because nobody's sure which helpers it owned?
A city map groups places by neighborhood. Nobody prints one with every bakery on page one and every school on page two, even though that's technically organized. Grouping files by type (all controllers here, all models there) is the bakery-page map. Grouping by feature is the neighborhood map, the one you can actually use to get somewhere.
Team shape pulls on this as well. In 1968 Melvin Conway wrote that organizations design systems that copy their own communication structure (How Do Committees Invent?; Martin Fowler has a readable modern summary). If your teams are split by product area, say a payments squad and an orders squad, folders split the same way give each squad a place it owns. Folders split by layer mean every squad edits every folder.
By layer: the tutorial default
Group files by technical role and a typical Node or Spring project looks like this:
src/
ββ controllers/
β ββ OrderController.ts
β ββ UserController.ts
β ββ BillingController.ts
ββ services/
β ββ OrderService.ts
β ββ UserService.ts
β ββ BillingService.ts
ββ repositories/
β ββ OrderRepository.ts
β ββ UserRepository.ts
β ββ BillingRepository.ts
ββ models/
ββ Order.ts
ββ User.ts
ββ Invoice.ts
Many framework starters and tutorials point you here, and it's a fair place to start. Everyone recognizes it on day one, and while the app has a handful of features each folder is small enough to scan.
It works well when:
- The app is small and has few features. Any structure reads fine at that size.
- The whole team works across the whole app, so nobody needs a part of the tree to call their own.
- You're building a prototype or an internal tool, and getting it running beats planning for the next two years.
It starts to hurt when:
- One feature is spread over every folder. Refunds is one thing in everyone's head and four folders in the repo.
- Deleting gets nervous. Which files in
services/belong to billing and which to orders? The folder name won't tell you, so dead code tends to stay. - Parallel work collides. Two people building unrelated features still edit the same folders and, sooner or later, the same service file.
Nobody can give you an exact feature count where this breaks, and you'll feel it long before you can measure it. The usual signs: services/ holds dozens of files, nobody owns the folder as a whole, and reviewers keep seeing files they've never opened before.
By feature: one change, one folder
Group files by the part of the product they serve. The layers don't go away. Controller, service and repository still exist, they live inside each feature folder:
src/
ββ orders/
β ββ OrderController.ts
β ββ OrderService.ts
β ββ OrderRepository.ts
β ββ Order.ts
ββ billing/
β ββ BillingController.ts
β ββ BillingService.ts
β ββ BillingRepository.ts
β ββ Invoice.ts
ββ auth/
β ββ AuthController.ts
β ββ AuthService.ts
β ββ User.ts
ββ shared/
ββ database.ts
ββ logger.ts
ββ Money.ts
billing/ and the other folders stay put.What you get:
- Changes stay local. Refunds means new files in
billing/and nothing else, so the PR is easy to review. - Deleting is honest. Remove the folder, and the compiler or your editor points at every import that still reaches into it.
- Ownership is obvious. Payments owns
billing/, identity ownsauth/, and a CODEOWNERS file can say so in one line each. - Onboarding gets narrower. Someone starting on orders reads
orders/first and leaves the rest for later.
What needs care:
- Shared code needs a home. Orders and billing both use
Money, so it can't live in either. It goes toshared/, and if nobody owns that folder it becomes the next junk drawer. - Features can still reach into each other. If
orders/OrderService.tsimports something deep insidebilling/, the folders look separate while the code is tangled. You'll want a rule that catches this, and there's one below.
What lights up when you ship refunds
The fastest way to see the difference is to count the folders one feature touches.
billing/ does and the rest of the code stays where it was.Those four lit boxes turn into real costs: a PR that's harder to review, more merge conflicts with teammates, and a git log where one feature's history is smeared across the whole tree. When the change sits in one folder, the review, the history and the rollback sit there too.
Screaming Architecture: the top level should say what the system does
In 2011 Robert C. Martin (Uncle Bob) wrote Screaming Architecture. Look at the blueprint of a house, he says, and it screams "house". Look at a library's and it screams "library". So what do your top-level folders scream? Health care, accounting, inventory? Or Rails, Spring, ASP.NET? His line that stuck with a lot of people: "Frameworks are tools to be used, not architectures to be conformed to."
Grouping by domain takes by-feature one step further. The folder names come from words the business already uses (Domain-Driven Design calls this the ubiquitous language), so a product manager can read them too:
// Says "online shop"
src/
ββ catalog/ // products and what we sell
ββ ordering/ // placing and tracking orders
ββ fulfillment/ // picking, packing, shipping
ββ payments/ // charges, refunds, invoices
ββ identity/ // accounts, login, permissions
// Says "some MVC framework"
src/
ββ controllers/ // HTTP plumbing
ββ models/ // ORM classes
ββ views/ // templates
ββ services/ // everything else
What goes inside each domain folder is up to the team. The top level is the part that matters: a new developer, or someone from the business side, should be able to read it and tell what the system does before they know how it's built.
Side by side
Here's how the three compare on questions that come up every week:
| Question | By layer | By feature | By domain (screaming) |
|---|---|---|---|
| Where's the billing code? | Spread across every layer folder | In billing/ |
In payments/, named the way the business says it |
| Folders one feature change touches | Usually every layer | Mostly one | Mostly one |
| Deleting a feature | Hunt for its files in each layer | Delete the folder, fix the imports the compiler flags | Same as by feature |
| Who owns what | Hard to say, every team edits every folder | One team per folder | One team per business area |
| How it usually goes wrong | Layer folders turn into junk drawers | shared/ grows with no owner |
Domain lines drawn too early, in the wrong place |
The mix many teams end up with
Martin Fowler lands in the same spot. Splitting code into presentation, domain and data is useful, he writes, but "it should only be applied at a relatively small granularity." As an app grows, "you should split your top level into domain oriented modules which are internally layered" (PresentationDomainDataLayering). In folder terms: features or domains on top, small layers inside each one.
Why this mix holds up:
- The top level tells you it's a shop with orders and billing before you see any framework.
- Each feature keeps its own code, so a billing change stays in
billing/. - Anyone used to MVC or Clean Architecture still finds controller, service and repository files where they expect them, one level down.
shared/holds what's genuinely cross-cutting: aMoneyvalue object, the logger, the database client. Everything else stays inside a feature.
Call it shared/, common/ or lib/, the name barely matters. Treat it like a small internal library with an owner. One easy test: if a file in shared/ keeps changing in the same PRs as one particular feature, it belongs inside that feature.
When folders aren't enough: let a tool hold the line
Folders are a convention, and anyone in a hurry can type import { applyDiscount } from '../../billing/internal/discounts'. Once a few teams share the repo, you want a machine to catch that. Start by giving each feature a single public entry point:
// src/billing/index.ts
export { createRefund } from './refunds/createRefund';
export type { Invoice } from './Invoice';
Then pick a tool that checks it:
- ESLint's
import/no-restricted-pathsrule blocks imports from one folder into another, with exceptions for specific files. If you already run ESLint, it's the smallest step. - Nx module boundary rules tag each project (for example
scope:ordersorscope:shared) and fail lint when a project depends on a tag it isn't allowed to. - NestJS modules encapsulate their providers: another module can only inject what a module explicitly exports.
- For Rails, Shopify built Packwerk, a static analysis tool that reports dependency and privacy violations between packages.
// .eslintrc.cjs
module.exports = {
plugins: ['import'],
rules: {
'import/no-restricted-paths': ['error', {
zones: [{
target: './src/orders',
from: './src/billing',
except: ['./index.ts'],
message: 'Import billing through src/billing/index.ts',
}],
}],
},
};
billing/index.ts. Anything deeper fails lint in CI.Shopify shows what a late move looks like. Their core Rails app, worked on by more than a thousand developers over more than a decade, was laid out like a typical Rails app: models, views, controllers. In early 2017 a small team started regrouping it by real-world concepts such as orders, shipping, inventory and billing. They listed about 6,000 Ruby classes, labeled each one with its component, and then moved every file in one big pull request generated by scripts (Deconstructing the Monolith). Their own advice is worth keeping: re-architect as late as you can, and treat features getting slower to ship as the sign that it's time.
You don't need a monorepo with one package per domain to get these benefits. Plenty of teams run a single package with feature folders and one boundary rule for years. Split into packages when cross-team coupling actually starts to hurt.
A rough guide by team size
Treat these as rules of thumb. The shape of your product matters more than headcount.
| Stage | Structure to reach for | Why | What to do now |
|---|---|---|---|
| Solo or pre-launch | By layer, or shallow feature folders | A few features and a lot of unknowns. Keep it simple. | The framework default is fine. Leave yourself a note to revisit when folders get crowded. |
| One team (roughly 5 to 20) | Feature folders plus a small shared/ |
Features keep arriving and people start working in parallel. | Move to feature folders while the move is still a short, boring PR. |
| Several teams (roughly 20 to 100) | Domains on top, layers inside, plus a boundary lint rule | Teams own areas, and accidental cross-team imports start costing real time. | Add CODEOWNERS and one boundary rule in CI before adding more structure. |
| Many teams (100+) | Domain packages in a monorepo, or separate services per bounded context | Teams need independent builds, releases and versioned interfaces. | Shopify kept one Rails app and checked package boundaries with Packwerk. Splitting into services is the other route, and it has its own bill. |
Starting by layer is rarely the problem. Staying there after it hurts usually is. When reviewers start asking "wait, what else does this service touch?", put the move on the calendar. That PR only gets bigger.
Try this week
- Pick your last three feature PRs and count how many top-level folders each one touched.
- Ask the newest person on the team to find the billing code without help, and watch where they look first.
- Open
shared/(orutils/) and move one file that only one feature uses back into that feature. - Give your busiest feature folder an
index.tsand add a lint rule that forces other features through it.
If you want to go a step further and organize each feature around individual requests, read Layered vs. Vertical Slice Architecture. And when one service stops being enough, the next stop is Monolith to Microservices.