Component Granularity
Metadata
- Status: Accepted & Implemented
- Date: 2025-12-20
- Type: Architecture Decision Record (ADR)
The "Too Many Files" Concern
It is common to worry when a project expands from 5 large files to 30+ smaller ones. It can feel like the code is "scattered." However, in modern frontend architecture (especially React), file count is a poor proxy for complexity.
1. The Cognitive Load Argument
-
Centralized (Big Files):
- Pro: You can use
Ctrl+Fto find everything in one place. - Con: To change a button color, you scroll past 500 lines of API logic, state management, and type definitions. You must "load" the entire component into your brain to ensure you don't break unrelated parts.
- Limit: Once a file hits ~300-400 lines, it becomes a "God Component," difficult to test and maintain.
- Pro: You can use
-
Decoupled (Many Files):
- Pro: When working on
DailyTotals, you only see code related to daily totals. You don't see the clipboard logic or the settings modal. - Con: You need to navigate the file tree more often.
- Solution: Good IDE navigation (Go to Definition) makes file hopping instant, negating the "scattered" feeling.
- Pro: When working on
2. The Separation of Concerns
We refactored Dashboard.tsx because it was violating the Single Responsibility Principle. It was doing three things:
- View: Rendering HTML/CSS.
- State: Managing complex objects (clipboard, layout preferences).
- Data: Fetching from APIs.
By splitting it:
useMeals.tshandles Data.useLayoutPreference.tshandles State.Dashboard.tsxhandles View (Composition).MealItem.tsxhandles View (Atomic).
3. When to Split (Rule of Thumb)
Do not split just for the sake of it. Split when:
- Reusability: A piece of UI (like
ProgressBar) is used in multiple places. - Complexity: A component has too many
useStateoruseEffecthooks (e.g., extractinguseMealStaging). - Frequency of Change: If you constantly edit the Logic but rarely touch the UI, they should probably be separate files.
4. Project Metrics (Post-Refactor)
- Total Files: ~32 source files.
- Largest File:
Dashboard.tsx(~300 lines). This is still on the larger side but acceptable as a "Page" controller. - Average File: ~85 lines. This is the "Sweet Spot" – easy to read in a single screen without scrolling.
Conclusion
32 files for a production-grade React application is actually very lean. "Enterprise" apps often have thousands. The current structure strikes a balance: enough structure to be scalable, but flat enough to remain navigable.