Monolithic Files and Module Decomposition
Practice moving one module out of a file that does too many jobs, keeping its imports, state, and behavior the same.
Content date: October 15, 2026.
Plain English
A monolithic file mixes jobs that are hard to understand or change together. Length is a clue, not a verdict: a long data table is a different problem from a short file that handles passwords, draws the page, and writes to the database. Splitting helps when it produces a module (a file with one job and a short list of what it offers) that someone can understand on its own.
Use this when
Do this when one file forces unrelated changes or makes a behavior hard to test, ideally after "Maintainability and Refactoring AI-Generated Code." This is the hands-on extraction practice. It is not a rule that every file must stay under some number of lines.
If you are just starting
Mark each section of the file by its job and list what it depends on. Start with a pure helper (a function that only calculates from its inputs) or with static data that has an obvious interface. Keep the same export names so callers behave the same. Run your checks after the move, and only then try a second extraction.
When your project grows
Check which way imports point, how modules start up, how long state lives, and any side effects. Shared changeable state, or files that import each other in a loop, can change behavior even when functions look identical. Run the build, and check the line between server-only and browser code, so the move doesn't ship a secret to the browser or turn per-page state into one copy shared by the whole app.
Worked example
A club registration page file holds form drawing, a list of sample fee options, and the fee calculation. The developer first extracts the fee calculation, which is a pure function, and adds tests for it. The page keeps owning the form state: moving it into a shared module would make every open copy of the form share one set of values, so it stays put.
Practice
Extract one coherent module
- Make a disposable copy or branch of a project with one file that mixes several jobs.
- List its jobs, exports, changeable state, and side effects.
- Pick one extraction and write down its inputs and outputs.
- Move the code, update imports, and check for import loops and server-only code reaching the browser.
- Run your behavior checks and the build, then read the diff for accidental changes.
Deliverable: A list of the file's jobs, the new module's inputs and outputs, a small diff, and the behavior and build results.
Common mistakes to watch for
- A 600-line page becomes 40 tiny files nobody owns, and finding anything takes longer.
- Two new files import each other, and the app crashes on startup only in the production build.
- A counter moved to the top level of a file is now shared by every copy of the component.
- A helper that reads a secret key moves into a file the browser build also imports, and the key ships to every visitor.
Prompting
A weak prompt
Split every file above 200 lines into smaller files.
A better prompt
Inspect this file, which mixes several jobs: [file path]. List its exports, imports, changeable state, and side effects. Choose one extraction with a clear purpose and a short interface. Keep callers, startup behavior, and the server/browser boundary unchanged. Check the new imports for loops, run the tests and the build, and explain why the new module is easier to maintain.
It ties a split to one clear job and unchanged behavior, so you don't end up chasing a line-count target.
Check before you move on
- The new module has one clear job.
- Its exports and inputs are easy to list.
- State lives where it did before, or you moved it on purpose.
- You checked the imports for loops.
- The tests and the production build both pass.
You are done when
- You can understand the new module without reading the unrelated page code.
- Callers and what visitors see are unchanged.
Questions to think about
A file holds 800 lines of club catalog data and nothing else. Should you split it?
Not just for length. Split only if parts have different owners or change for different reasons. A long, simple data table can be fine.
Why can moving a variable to the top level of a file change behavior?
It changes when the variable is created and who shares it. Every caller may now see one shared copy, though the surrounding functions look the same.
Tests pass, but the production build fails after your extraction. Are you done?
No. A failing build points to an import, bundling, or server/browser boundary problem the tests missed. Fix it and rerun everything affected.