Sub topic 1
Why Structure Matters as a Codebase Grows
Prerequisites
This chapter assumes you can:
- Write React function components and use props and hooks such as
useStateanduseEffect. - Read basic TypeScript or modern JavaScript module syntax (
importandexport). - Run a project built with a bundler such as Vite or webpack.
- Recall a small or medium React application you have built or worked on.
No new tools are needed here. This chapter explains the problem that the rest of the tutorial solves, so you can judge which later techniques your project needs.
What "structure" means
Structure is how you divide code into files, folders, and modules, and which of those pieces may depend on which others. It has two parts:
- Layout is where files live and what they are called.
- Boundaries are the rules about what one part of the code may import from another.
Most teams think about layout first. Boundaries do more for maintainability, because they limit how far a change can spread. A tidy folder tree with no boundaries still turns into a tangle.
Why small projects don't need it
A new React app works with almost any structure. A few components sit in one folder, one developer knows where everything is, and a change touches two or three files. The cost of a poor layout is low because the whole codebase fits in one person's head.
That stops being true as the project grows. Problems usually appear in this order:
- People can't find the file they need.
- A change in one place breaks something unrelated.
- New teammates take weeks to become productive.
- Two people edit the same files and hit merge conflicts.
- The app gets slow to build, test, and load.
Structure is the tool that keeps those costs from growing faster than the codebase.
The symptoms of an unstructured codebase
Slow navigation
Suppose a bug report says "the cart total is wrong". In a flat layout you search through folders organized by file type: components/, hooks/, utils/, api/. The code for one feature is scattered across all of them. You open five files in five folders to follow a single behavior.
Hidden coupling
In a large codebase, any file can import any other file. Over time, components reach into each other's internals. A checkout component imports a helper from the product list's utils folder, and a settings page imports a private hook from the cart. Each import looks harmless. Together they mean you can no longer change the cart without checking every file that touched it.
The test of coupling is simple: if you change file A, how many other files must you read to be sure nothing broke? In a coupled codebase the answer is "all of them."
Circular dependencies
When module A imports B and B imports A, you have a circular dependency. Depending on the bundler and the order of evaluation, you may get undefined imports at runtime, or errors that appear only in production builds. Cycles are rare in small projects and common in large ones without boundaries.
Unclear ownership
If every folder belongs to everyone, nobody feels responsible for it. Pull requests touch many areas, reviewers can't tell who should approve them, and merge conflicts become routine.
Growing bundles
If every module can import every other module, the bundler can rarely split the code cleanly. The user downloads code for pages they never visit. Chapter 14 covers route-based splitting, which depends on good module boundaries.
Two sources of complexity
It helps to separate two kinds of complexity, because structure addresses only one.
| Kind | Example | Does structure help? |
|---|---|---|
| Essential | Tax rules for twelve countries | No. The domain is complex. |
| Accidental | Finding where the tax code lives, or what else breaks if you edit it | Yes. This is what structure fixes. |
Structure won't make a hard domain simple. It keeps the accidental cost of working in the code low, so developers can spend their effort on the essential problem.
What a good structure gives you
A good structure aims at four properties.
- Findability. Given a feature or a bug, you can guess where the code is.
- Locality of change. A typical change touches one area. Related files sit together, so you edit and review them together.
- Explicit dependencies. What each area exposes and what it uses is visible and checkable, not an accident of what happens to be importable.
- Deletability. You can remove a feature by deleting its folder and fixing a small, known number of import errors.
Deletability is a useful practical test. If removing a feature means hunting through the whole project, the feature is not really a unit.
A concrete before and after
Here is a flat layout, organized by file type, that many projects start with:
src/
components/
CartList.tsx
OrderTable.tsx
Button.tsx
hooks/
useCart.ts
useOrders.ts
api/
cart.ts
orders.ts
utils/
formatDate.ts
cartTotal.tsThe cart is in four folders. Nothing says OrderTable.tsx can't import cartTotal.ts, and nothing says Button.tsx, a generic component, can't import useCart.ts.
Here is the same code grouped by feature:
src/
features/
cart/
components/CartList.tsx
hooks/useCart.ts
api.ts
cartTotal.ts
orders/
components/OrderTable.tsx
hooks/useOrders.ts
api.ts
shared/
ui/Button.tsx
lib/formatDate.tsEverything about the cart is under features/cart. The layout alone doesn't prevent bad imports, though. You also need rules, such as "features may import from shared but not from each other's internals". The following chapters build that out.
The next diagram shows the dependency direction that a structured codebase aims for.
Dependencies point one way: the app depends on features, and features depend on shared code. Shared code knows nothing about features. Features do not import from each other's internals. When arrows only point down, you can read, test, and change a lower layer without knowing what sits above it.
Seeing coupling in code
This example shows the kind of import that structure rules prevent. The first import reaches into another feature's private file.
// src/features/orders/components/OrderTable.tsx
// Fragile: depends on a private file inside the cart feature.
import { cartTotal } from "../../cart/cartTotal";
// Better: depends on a name the cart feature chose to expose.
import { cartTotal } from "@/features/cart";If the cart team renames cartTotal.ts or moves the logic, the first import breaks. The second keeps working as long as the cart's public entry point still exports cartTotal. Chapters 6 and 12 cover public APIs, aliases, and the lint rules that enforce this.
Common mistakes when adding structure
- Copying a famous layout. A layout built for a 200-person team may slow down a team of three. Choose structure that fits your current problems.
- Reorganizing without rules. Moving files into feature folders without restricting imports changes the look but not the coupling.
- Over-sharing. Moving code to
shared"in case someone needs it" creates a dumping ground that everything depends on. Move code there when a second real use appears. - Big-bang rewrites. Moving every file in one pull request blocks the team and hides bugs. Chapter 15 shows an incremental route.
- Structure as a goal. The aim is faster, safer changes. If a rule makes ordinary work harder, revisit it.
How to judge your own codebase
Ask these questions about a project you work on:
- Can a new developer find the code for a named feature in under a minute?
- How many folders does a typical feature change touch?
- Can you list what a feature exposes to the rest of the app?
- Does any
sharedcode import from a feature? - Could you delete one feature in an hour?
Several "no" or "I don't know" answers point to the areas worth restructuring first.
Exercise
A teammate moves all files into features/ folders but allows any file to import any other file by relative path. Which of the four properties (findability, locality of change, explicit dependencies, deletability) have improved, and which have not?
Show answer
Findability and locality of change improve, because related files now sit together. Explicit dependencies and deletability do not improve, because other features can still import any internal file. Removing a feature would still break imports across the project. Import rules (public APIs and lint checks) are needed for those two.
Exercise
In your own project, pick one feature and list every folder that holds code for it. Then name one file from a different feature that imports from it. What would break if you renamed that imported file?
Show answer
Answers vary. A good answer lists the feature's files by folder, such as components, hooks, and API code, and identifies a cross-feature import. The rename would break that import. The number of such imports is a rough measure of coupling, and each one is a candidate for a public API entry point.