Developers organize files and code by separating concerns into predictable layers: a shallow top-level folder structure, one responsibility per file, modules grouped by feature, version control as the record of truth, and automation (formatters, linters, path aliases) that enforces the conventions so they survive contact with a growing team. The key condition is consistency within a repository, not adherence to one universal layout—a small script and a monorepo need different shapes, but both should let a new contributor find any file by guessing once or twice.

Most codebases don't rot because of bad algorithms; they rot because a folder named utils swallowed everything, a 3,000-line file became too risky to touch, and nobody wrote down where new things go. You can prevent that. The steps below start with prerequisites, move through reversible decisions first, and end with the checks that tell you the system is actually working.

Prerequisites Before You Reorganize Anything

Reorganizing on top of unknown state is how people lose work. Before moving a single file, confirm four things.

First, your project must be in version control, with a clean working tree (git status shows no uncommitted changes). If it isn't tracked yet, commit everything and push to a remote before you restructure—your commit history is the undo button.

Second, run your test suite and record the result. You need a baseline so that any post-move failure is attributable to the restructure, not pre-existing bugs.

Third, check how imports and build tools reference paths. A file move breaks relative imports; a build config, tsconfig.json path map, or Dockerfile COPY line can silently reference a folder you're about to rename.

Fourth, tell your team (or note it for yourself) the scope and timing. Large moves in a shared branch create merge conflicts; short-lived branches merged quickly are far safer than a week-long reorganization.

Success check before starting: git status is clean, tests pass, and you can describe in one sentence where each top-level folder goes. If you can't, you're not ready to move files.

Start With a Shallow Top-Level Structure

The most durable convention in modern projects is a small number of top-level directories, each with an obvious meaning. For an application, that often looks like src/ for source code, tests/ or colocated test files, scripts/ for reusable automation, docs/ for design notes and runbooks, config/ for environment templates (not secrets), and the repository root for tooling files like package.json, pyproject.toml, Makefile, or .gitignore.

Two rules do most of the work. First, keep depth shallow: aim for three or four levels below the root. Deep nesting like src/features/billing/invoice/components/table/parts/ slows navigation and multiplies import churn. Second, make folder names nouns or clear domain terms, not vague buckets—billing and auth tell you something, misc and helpers tell you less than the file names inside them.

A trick I recommend: instead of creating a utils folder, create a folder named after the thing the code does. date-formatting or http age better than common, because the next person doesn't have to open the folder to guess its contents.

Expect a short adjustment period where imports break. That's the reversible cost of the change—fix imports, run tests, commit as a single "move files only" commit with no logic changes, so the diff is reviewable at a glance.

One File, One Responsibility, and Keep Files Short

File organization inside folders matters as much as the folder tree. The principle is cohesion: things that change together live together, and a file should have one reason to change. In practice that means a file named invoice-total.ts should compute invoice totals and nothing else—not also parse CSV, not also hit the network.

Length is a signal, not a rule. A well-factored 400-line file can be fine; a 200-line file that mixes parsing, validation, and rendering is not. Watch for three symptoms that a file needs splitting: the header comment lists unrelated jobs, the imports pull from three unrelated domains, or a test for one behavior drags in setup for another. Split along those seams.

Naming does heavy lifting here. Use consistent casing per language and ecosystem (snake_case.py, kebab-case.ts, PascalCase.java for types), and make the name describe the export: parse-invoice.ts exports parse functions; InvoiceRepository is a class. A reader scanning a directory should be able to tell how many concepts live there.

The success check is simple: pick a random file and ask a colleague what it does from the filename alone. If they guess wrong twice, rename it.

Group Code by Feature or by Layer—Pick One and Document It

Two broad organizing schemes dominate application code, and both work if you choose deliberately.

Layer-based (technical) organization groups by role: controllers/, services/, models/, repositories/. It's familiar, works well for small projects, and mirrors how many frameworks scaffold things. Its weakness appears as the app grows: adding one feature touches four distant folders, and a pull request that should be local sprawls.

Feature-based (domain) organization groups by capability: billing/, auth/, notifications/, each containing its own handlers, logic, and tests. Changes stay local, ownership is obvious, and deleting a feature means deleting a folder. Its weakness is shared code—you'll need a deliberate shared/ or platform/ area for cross-cutting concerns, plus a rule that prevents shared from becoming a dumping ground.

A third pattern, the monolith's conventional "src plus packages," suits libraries and monorepos: each package owns its files, with a shared build layer above. That's a packaging decision, not a code-organization one, but it needs its own entry in your repo's documentation.

Whichever you pick, write it down in a short docs/architecture.md or a CONTRIBUTING section: "New feature goes in src/features//; shared helpers go in src/shared/." Conventions that live only in people's heads disappear when people leave.

Scheme Best fit Main trade-off Watch for
By layer (controllers, services, models) Small apps, framework-heavy stacks A single feature spans many folders Sprawl as features multiply
By feature (billing, auth, notifications) Larger apps, teams owning domains Shared code needs a home and rules shared/ becoming a junk drawer
By package (monorepo modules) Libraries, multi-app repositories Build and dependency overhead Unused packages and version drift

The table isn't a verdict; it's a criteria comparison. The question to ask is: when a change request arrives, how many folders does it touch, and can a stranger predict where to look?

Separate Source, Tests, Docs, and Generated Files

Four categories of files need different treatment, and mixing them is a common source of confusion.

Source code lives where your build expects it (often src/). Tests should either sit beside the code they test—invoice-total.test.ts next to invoice-total.ts—or in a mirrored tests/ tree. Colocation keeps tests discoverable when you open a file; a mirrored tree makes bulk test runs and fixtures easier to reason about. Pick one per repository, not per file.

Documentation belongs in docs/ or at the root (README.md for the front door, CONTRIBUTING.md for process, CHANGELOG.md for release notes). Keep design decisions in a docs/decisions/ folder with one note per significant choice; six months later, "why did we do this" gets answered from the record, not from memory.

Generated files are the ones to exclude from manual editing: build output (dist/, build/), dependency caches (node_modules/, .venv/), and compiled artifacts. Put them in .gitignore and make regeneration part of the documented workflow. If a generated file must be committed (some lockfiles, some schema snapshots), say so explicitly in the README—otherwise someone will "fix" it by hand.

A practical note for the shared-config problem: keep environment-specific values in .env.example committed to the repo, and the real .env untracked. That's the version-control equivalent of good file hygiene.

Put Version Control and a Root README at the Center

Git is both the safety net and the index for everything above. Two habits make it an organizing tool rather than just a backup.

First, commit in units that match the structure: pure moves in one commit, behavior changes in another, config changes separately. When history is legible, reorganizing later is safe because you can always see which change moved what.

Second, let the root README.md answer the four questions a newcomer actually has: what this repository is, how to install and run it, where the code lives (with a short folder map), and how to run tests. That folder map—five or six lines—is the single highest-value document in most projects. Update it when the top level changes.

Branch naming and commit message conventions (however lightweight) also reduce cognitive load: a feature/billing-refactor branch with commits like move billing module into feature folder tells a reviewer exactly what to expect before they open the diff.

Automate the Conventions So They Don't Erode

Rules enforced by people get remembered until a deadline; rules enforced by tooling survive.

Formatters (Prettier, Black, gofmt, rustfmt) remove style decisions from review entirely, and a pre-commit hook keeps unformatted code from entering history. Linters catch unused imports and misfiled concerns—an import reaching across the tree from features/ back into another feature's internals is a signal your boundaries need attention. Path aliases (@/features/billing) shorten imports and make a future move a config change instead of a mass edit.

Continuous integration is the check that the whole structure still works: it builds from a clean checkout, runs tests, and—usefully—can fail when generated files are committed stale. If your repository has a documented make setup && make test or equivalent, a new contributor's success is verifiable in one command, which is the real test of file organization at the team level.

The stop condition for this section: if your tooling disagrees with your documented structure repeatedly, the structure is probably wrong. Adjust the convention rather than disabling the check.

Worked Example: Restructuring a Growing Project

Say a project has drifted into this shape: a utils.py with 1,200 lines, models.py with 800 lines mixing data classes and database access, templates next to application code, and a test_all.py at the root. You have two weeks and a bug backlog.

Start by counting responsibilities, not lines. utils.py holds date parsing, string cleaning, and an HTTP retry helper—three domains, so three new modules under a shared/ area: date_parsing.py, text_cleaning.py, http_retry.py. models.py splits into models/ (data structures) and repositories/ (queries), with one file per entity. Tests split to mirror the modules they cover, and the root test_all.py becomes a thin runner that discovers the new tests.

Each split is its own commit, with imports updated and tests run after each. Expect to spend roughly half the effort on import rewiring and configuration paths—that ratio is normal, and it's why the "make moves in a fresh branch and merge quickly" rule matters. Success looks like this: git log shows a series of move-only commits, the full test suite goes green at each commit, and the root README's folder map now matches reality.

If a restructure of a heavily used repository feels too risky to do in one pass, the reversible first step is adding the README folder map and the formatter/linter config. Those change nothing about where code lives but make the next reorganization cheaper.

Common Failure Modes and How to Spot Them

Three patterns account for most organizational decay.

The junk-drawer folder: utils, common, helpers, or misc grows without limit because adding a file there requires no decision. Symptom: the folder's file count exceeds any feature folder's. Fix: rename it to the domain it serves and move strays to their proper homes.

The god file: one file that everything imports. Symptom: a change to it touches unrelated features and its diff review always looks like a pile. Fix: split by responsibility and watch for circular imports—if splitting creates a cycle, the two halves were probably one concept that should move together, or a shared abstraction is missing.

The duplicated structure: every folder gets its own tests/, docs/, and config/ subfolder, so the tree multiplies. Symptom: you're never sure whether a given file belongs at the top or under a feature. Fix: pick colocation or mirroring once, and write the rule in CONTRIBUTING.

These are diagnostic patterns, not moral judgments. A junk drawer that's five small, stable files may be fine; the failure mode is when the folder grows unexamined. Check folder sizes and ages periodically—large and recently touched is the combination worth investigating.

What Good Looks Like After the Change

You'll know the system works when new files have obvious homes and old files are easy to find. Concretely, a newcomer should be able to answer "where does a new feature go?" from the README in under a minute, imports should read like a map of the domain, tests should be runnable with one documented command, and generated files should never need hand editing.

Organization is maintenance, not a one-time event. Every few months, or whenever a folder exceeds a comfortable review size, spend an hour checking the structure against how the code actually changes. The revisions are usually small—renaming, splitting, moving—and that's the point: small adjustments stay reversible, and reversible changes keep the codebase livable.

If you remember one thing, make it the shallow tree plus the README map. Those two decisions cost little, survive team changes, and make every other organizing choice easier to make consistently.