# Apply Package By Feature

> Use when a codebase is organized into top-level technical folders (controllers/, services/, models/, utils/, components/, hooks/) and a single change touches ma

- Skill: `jeffreytse/apply-package-by-feature` (Agent Skill)
- Install (CLI): `npx get-skill add jeffreytse/apply-package-by-feature`
- Raw SKILL.md: https://api.skillmd.com/api/skills/jeffreytse/apply-package-by-feature/raw
- Safety review: pending
- Works with: any agent that reads SKILL.md (Claude Code, Claude.ai, Cursor, Codex, Windsurf, 60+ more)
- Category: Coding & Dev Tools
- Author: jeffreytse (https://skillmd.com/u/jeffreytse)
- Updated: 2026-12-03
- Page: https://skillmd.com/skills/jeffreytse/apply-package-by-feature

---

---
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

1. **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`).

2. **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.

3. **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.

4. **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.

5. **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.

6. **Colocate tests, fixtures, and feature-specific config** in the feature folder, so deleting a feature means deleting one folder.

7. **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.

8. **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.

