Sub topic 1
Why Group Code by Feature, Not by File Type
Prerequisites
This chapter assumes you can read TypeScript and have built Angular components with templates and dependency injection. It also assumes you have defined routes with the Angular Router and used the Angular CLI and npm. No code from this chapter needs to run. The later chapters build on the ideas introduced here.
The problem with type-based folders
Many Angular projects start with folders named after kinds of files:
src/app/
components/
order-list.component.ts
order-detail.component.ts
customer-form.component.ts
services/
orders.service.ts
customers.service.ts
models/
order.model.ts
customer.model.tsThis layout is easy to explain, and it works for a small app. It becomes a burden as the app grows. A change to orders touches files in three or more folders. Each folder collects dozens of unrelated files, so you scroll past customer code to find order code. Nothing says which files belong together, so any component can import any service. Over time the imports form a tangle that nobody planned.
What feature folders change
A feature is a part of the product that a user or business would name, such as orders or customers. A feature folder keeps everything for that part in one place:
src/app/
orders/
data-access/
orders.store.ts
order.model.ts
feature/
order-list.component.ts
order-detail.component.ts
orders.routes.ts
index.ts
customers/
...
shared/
core/The file types are still separated inside the feature. The top level of the app is organized by what the app does, not by what the files are.
Why this scales
Grouping by feature gives you several concrete benefits:
- Locality of change. A typical task, such as adding a field to an order, stays inside one folder. Reviews are smaller and merge conflicts are rarer.
- Clear ownership. A team or a person can own
orders/. Type-based folders are shared by everyone, so nobody owns them. - Lazy loading. A feature that is a single unit can be loaded by the router only when the user navigates to it. A feature spread across type folders cannot be split off cleanly.
- Controlled dependencies. When a feature has edges, you can decide what it exposes and what it hides. Later chapters cover barrels, path aliases and lint rules that enforce these edges.
- Easy removal. Deleting a retired feature means deleting one folder and one route entry.
Trade-offs and common errors
Feature folders are not free. You must decide where code belongs, and some code does not fit one feature. The common mistakes are these:
- Moving the mess down one level. Renaming
components/toorders/components/without defining what the feature exposes gives you the same tangle in smaller pieces. - Putting everything in shared. When two features need the same code, it is tempting to drop it in
shared/. That folder then becomes a second type-based layout. Later chapters cover what belongs there. - Creating features that are too small. A feature per component produces many tiny folders with heavy cross-imports. A feature should match a user-facing capability.
- Importing across feature internals. If
customersimportsorders/feature/order-list.component, the folders are only decoration. The boundary has to be enforced, not just drawn.
Exercise
Exercise
Your app has components/, services/ and models/ folders. A teammate must add a "discount" field to orders. Name two problems this layout causes for that task, and say how a feature folder would help.
Show answer
The change touches files in several folders (the model, the service and the components), so the developer must find and edit related files scattered across the tree. Nothing prevents unrelated code, such as customer components, from importing the order service, so it is hard to know what else the change affects. With an orders/ feature folder, the model, store and components sit together, and other features import only what the folder exposes, so the change stays local and its impact is easier to see.