The Proximity Principle
Less jumping, more shipping.
The Core Principles
Colocate
Put related code as close as possible to where it's used.
Inline First
Don't extract until code is reused. Keep it inline by default.
Lowest Common Ancestor
When sharing code, place it at the nearest common parent directory.
Prefer Fewer Files
One file with related code beats many files requiring imports.
Don't Abstract Prematurely
If code is only used once, keep it inline. Abstraction adds complexity.
Exception: React State Isolation
Extract a child component when it has its own state — even if only used once. This prevents the parent from re-rendering when the child's state changes.
Prefer Fewer Files
Splitting code across many files increases cognitive load. Keep related code together.
Extract Only When Reused
Only move code to a separate file when it's needed in multiple files.
Lowest Common Ancestor
When you do share code, place it at the nearest common parent.
Tip: Avoid generic directory names like features/. Use specific names that describe what the directory contains: pages/, routes/, controllers/, etc.
Exception: Global Code
For something that we can expect to be used globally (e.g. a button component) or used almost everywhere, it's better to put them in a "type" directory (e.g. components/, schemas/).
Follow Framework Rules
File-based routers (Next.js, TanStack Router) often turn every file into a route. Co-location requires understanding how to "hide" files.
File Suffixes
When organizing by type at scale, use suffixes for discoverability.
Quick Reference
Code used once → Keep it inline
Code is too long → Extract to function, keep in file
Code reused in same file → Extract to function, keep in file
Code reused across files → Extract to lowest common ancestor
Code used globally / almost everywhere → Put in a type directory (e.g. components/, schemas/)
React: Child has own state → Extract to component (prevents parent re-render)
Why This Matters
- 🧠Reduces cognitive load — Less jumping between files
- 🚀Faster shipping — Less time spent managing file structure
- 🧹Easier cleanup — Delete a file and everything goes with it