𝗧𝗵𝗿𝗲𝗲 𝗧𝗶𝗲𝗿 𝗙𝗼𝗹𝗱𝗲𝗿 𝗦𝘁𝗿𝘂𝗰𝘁𝘂𝗿𝗲𝘀
Most codebases have a utils folder. It becomes a dumping ground. You find date formatters next to user permissions. This is not organization.
Use Three Tier Hoisting. Use three folders. Use one test to decide where code goes.
The Tiers:
- Generic: Code works in any repo. An array sort belongs here.
- Product: Code is for your product. It is used in many places. Database drivers live here.
- Local: Code is for one component. It stays next to it.
The Tests:
- Generic: Does the code work in any repo without changes?
- Product: Does the name use product words?
- Local: Does only one place use it?
The Rule of Three: Start local. Duplicate it twice. Move it up on the third use. This stops early abstraction.
The Import Flow:
- Domain imports from Product and Generic.
- Product imports from Generic.
- Generic imports from nothing.
Enforce this with ESLint. Build errors stop wrong imports. You stop arguing in code reviews.
New hires find files fast. The utils mess disappears.
Source: https://dev.to/adam_lewis_427616cbc93f0b/a-three-tier-folder-structure-for-long-lived-codebases-3a3l