name: apply-package-by-feature
description: Use when a codebase is organized into top-level technical folders (controllers/, services/, models/, utils/, components/, hooks/) and a single change touches many distant folders, or when starting a new project and choosing how to group code at the top level.
source: 'Parnas, "On the Criteria To Be Used in Decomposing Systems into Modules", CACM 15(12), 1972; Martin, "Screaming Architecture", 2011; Brown, "The Missing Chapter" in Martin, "Clean Architecture", 2017; Angular, "Style Guide — Folders-by-feature structure"'
tags: [project-structure, package-by-feature, modularity, cohesion, developer, maintainability]
related: [apply-high-cohesion, apply-low-coupling, apply-module-boundary-enforcement, apply-domain-driven-design]
Apply Package by Feature
Group code by the business capability it serves (orders/, billing/, accounts/), not by technical layer (controllers/, services/, repositories/), so that each top-level folder holds everything one feature needs and changes stay local.
Why This Is Best Practice
Adopted by:
- Angular's official style guide ("folders-by-feature").
- The Django "apps" convention, Rails engines, and Shopify's componentized Rails monolith (Packwerk).
- NestJS modules and Spring Modulith.
- Simon Brown's "package by component" chapter in Clean Architecture.
Impact:
- Parnas (1972) showed that modules built around design decisions likely to change are easier to modify than modules built around processing steps.
- MacCormack, Rusnak & Baldwin (2006, Management Science 52(7)) found that more modular codebases have lower "propagation cost": a change ripples to fewer files.
- Accelerate (Forsgren, Humble & Kim, 2018) found that loosely coupled architecture, where teams change their part without coordinating with others, is among the strongest predictors of software delivery performance.
Why best:
- Features change together. Layers rarely do.
- Layer-first folders scatter a single feature across five places, hide what the system does ("screaming architecture"), and make every class public by default.
- Feature-first folders put related code side by side, let you hide internals, and map cleanly to team ownership.
Sources: Parnas (1972), CACM 15(12), 1053–1058. Martin, R.C., "Screaming Architecture" (blog.cleancoder.com, 2011). Brown, S., "The Missing Chapter", in Clean Architecture (2017). MacCormack, Rusnak & Baldwin (2006). Forsgren, Humble & Kim, Accelerate (2018).
Steps
List the capabilities. Write the nouns and verbs the business uses: orders, payments, catalog, users, notifications. These become top-level folders. Use domain language, not technical terms (see apply-domain-based-naming).
Create one folder per feature and put its layers inside it:
src/
orders/
orders.controller.ts
orders.service.ts
orders.repository.ts
order.entity.ts
orders.test.ts
index.ts # public API of the feature
billing/
...
shared/ # truly cross-cutting: logging, config, db client
Small features can be flat. Add sub-folders (api/, domain/, infra/) only when a feature grows past roughly 10–15 files.
Define a public surface for each feature. Export only what other features may use (an index.ts, a Go package's exported names, a Java package with package-private internals, Python's __all__). Everything else stays internal.
Keep shared/ small and generic. Code moves to shared/ only when two or more features use it and it has no feature-specific knowledge. A growing shared/ or utils/ is a sign that a missing feature or domain concept needs its own folder.
Make features talk through their public API or through events. Never reach into another feature's internals. If billing needs order data, call orders' exported service or subscribe to an OrderPlaced event.
Colocate tests, fixtures, and feature-specific config in the feature folder, so deleting a feature means deleting one folder.
Migrate an existing layered codebase incrementally. Pick the feature you're changing next, move its controller, service, and repository into a new feature folder, fix imports, and repeat. Don't stop the world for a big-bang move.
Enforce the boundaries automatically once two or more features exist (see apply-module-boundary-enforcement).
Rules
- The top level should tell a newcomer what the system does, not what framework it uses.
- One feature, one folder, one owner.
- Cross-feature access goes through the public API only.
shared/ holds no business rules.
- Prefer duplicating a tiny helper over coupling two features through it.
Common Mistakes
- Features named after layers.
api/, core/, and common/ as features just recreate layer-first structure.
- The dumping-ground
utils/ or shared/. It quietly becomes the real coupling hub.
- Circular dependencies between features.
orders imports billing and billing imports orders. Fix it by extracting the shared concept, inverting the dependency, or using an event.
- Splitting too finely. One folder per entity (
order-line/, order-status/) instead of per capability creates many tiny, tightly coupled modules.
Examples
Express API: src/controllers/, src/services/, src/models/ became src/orders/, src/catalog/, src/accounts/, each with route, service, repository, and tests. Adding "order cancellation" touched one folder instead of four.
React app: components/, hooks/, api/ became features/cart/, features/checkout/, features/search/, with shared/ui/ for design-system primitives (see design-frontend-project-structure).
When NOT to Use
- Tiny projects or scripts (a few files) where any structure is overhead.
- Libraries whose "features" are technical layers by nature (a database driver, a parser).
- Framework-mandated layouts where fighting the convention costs more than it saves. Follow the framework (see
apply-convention-over-configuration), and group by feature inside its folders where possible.
1---2name: apply-package-by-feature3description: Use when a codebase is organized into top-level technical folders (controllers/, services/, models/, utils/, components/, hooks/) and a single change touches ma4---5---6name: apply-package-by-feature7description: Use when a codebase is organized into top-level technical folders (controllers/, services/, models/, utils/, components/, hooks/) and a single change touches many distant folders, or when starting a new project and choosing how to group code at the top level.8source: 'Parnas, "On the Criteria To Be Used in Decomposing Systems into Modules", CACM 15(12), 1972; Martin, "Screaming Architecture", 2011; Brown, "The Missing Chapter" in Martin, "Clean Architecture", 2017; Angular, "Style Guide — Folders-by-feature structure"'9tags: [project-structure, package-by-feature, modularity, cohesion, developer, maintainability]10related: [apply-high-cohesion, apply-low-coupling, apply-module-boundary-enforcement, apply-domain-driven-design]11---1213# Apply Package by Feature1415Group code by the business capability it serves (`orders/`, `billing/`, `accounts/`), not by technical layer (`controllers/`, `services/`, `repositories/`), so that each top-level folder holds everything one feature needs and changes stay local.1617## Why This Is Best Practice1819**Adopted by:**20- Angular's official style guide ("folders-by-feature").21- The Django "apps" convention, Rails engines, and Shopify's componentized Rails monolith (Packwerk).22- NestJS modules and Spring Modulith.23- Simon Brown's "package by component" chapter in *Clean Architecture*.2425**Impact:**26- Parnas (1972) showed that modules built around *design decisions likely to change* are easier to modify than modules built around processing steps.27- MacCormack, Rusnak & Baldwin (2006, *Management Science* 52(7)) found that more modular codebases have lower "propagation cost": a change ripples to fewer files.28- Accelerate (Forsgren, Humble & Kim, 2018) found that loosely coupled architecture, where teams change their part without coordinating with others, is among the strongest predictors of software delivery performance.2930**Why best:**31- Features change together. Layers rarely do.32- Layer-first folders scatter a single feature across five places, hide what the system *does* ("screaming architecture"), and make every class public by default.33- Feature-first folders put related code side by side, let you hide internals, and map cleanly to team ownership.3435Sources: Parnas (1972), *CACM* 15(12), 1053–1058. Martin, R.C., "Screaming Architecture" (blog.cleancoder.com, 2011). Brown, S., "The Missing Chapter", in *Clean Architecture* (2017). MacCormack, Rusnak & Baldwin (2006). Forsgren, Humble & Kim, *Accelerate* (2018).3637## Steps38391. **List the capabilities.** Write the nouns and verbs the business uses: orders, payments, catalog, users, notifications. These become top-level folders. Use domain language, not technical terms (see `apply-domain-based-naming`).40412. **Create one folder per feature and put its layers inside it:**42 ```43 src/44 orders/45 orders.controller.ts46 orders.service.ts47 orders.repository.ts48 order.entity.ts49 orders.test.ts50 index.ts # public API of the feature51 billing/52 ...53 shared/ # truly cross-cutting: logging, config, db client54 ```55 Small features can be flat. Add sub-folders (`api/`, `domain/`, `infra/`) only when a feature grows past roughly 10–15 files.56573. **Define a public surface for each feature.** Export only what other features may use (an `index.ts`, a Go package's exported names, a Java package with package-private internals, Python's `__all__`). Everything else stays internal.58594. **Keep `shared/` small and generic.** Code moves to `shared/` only when two or more features use it *and* it has no feature-specific knowledge. A growing `shared/` or `utils/` is a sign that a missing feature or domain concept needs its own folder.60615. **Make features talk through their public API or through events.** Never reach into another feature's internals. If `billing` needs order data, call `orders`' exported service or subscribe to an `OrderPlaced` event.62636. **Colocate tests, fixtures, and feature-specific config** in the feature folder, so deleting a feature means deleting one folder.64657. **Migrate an existing layered codebase incrementally.** Pick the feature you're changing next, move its controller, service, and repository into a new feature folder, fix imports, and repeat. Don't stop the world for a big-bang move.66678. **Enforce the boundaries automatically** once two or more features exist (see `apply-module-boundary-enforcement`).6869## Rules7071- The top level should tell a newcomer what the system *does*, not what framework it uses.72- One feature, one folder, one owner.73- Cross-feature access goes through the public API only.74- `shared/` holds no business rules.75- Prefer duplicating a tiny helper over coupling two features through it.7677## Common Mistakes7879- **Features named after layers.** `api/`, `core/`, and `common/` as features just recreate layer-first structure.80- **The dumping-ground `utils/` or `shared/`.** It quietly becomes the real coupling hub.81- **Circular dependencies between features.** `orders` imports `billing` and `billing` imports `orders`. Fix it by extracting the shared concept, inverting the dependency, or using an event.82- **Splitting too finely.** One folder per entity (`order-line/`, `order-status/`) instead of per capability creates many tiny, tightly coupled modules.8384## Examples8586**Express API:** `src/controllers/`, `src/services/`, `src/models/` became `src/orders/`, `src/catalog/`, `src/accounts/`, each with route, service, repository, and tests. Adding "order cancellation" touched one folder instead of four.8788**React app:** `components/`, `hooks/`, `api/` became `features/cart/`, `features/checkout/`, `features/search/`, with `shared/ui/` for design-system primitives (see `design-frontend-project-structure`).8990## When NOT to Use9192- **Tiny projects or scripts** (a few files) where any structure is overhead.93- **Libraries whose "features" are technical layers by nature** (a database driver, a parser).94- **Framework-mandated layouts** where fighting the convention costs more than it saves. Follow the framework (see `apply-convention-over-configuration`), and group by feature *inside* its folders where possible.