I Built a God Type With 900+ Methods, One Tidy File at a Time

The persistence layer of a service I worked on has one type, Store, with more than 900 public methods. Every layer above it calls it directly. I didn’t set out to build a god object. I got one by making a series of changes that each looked reasonable in review, and when the evidence showed up I worked around it instead of fixing it.

This post covers how that happened, what it cost, and what I do differently now.

What I built

Store started out as a few hundred lines in the project’s first feature commit. Ten weeks later it looked like this:

  • More than 900 public methods, and nearly 1,000 counting private ones
  • Over 170 files, each one opening another impl Store { ... } block, together holding about 100,000 lines
  • 23 fields: three database connections (the writer and its queue, a reader pool, a numbering connection) plus about twenty in-process broadcast channels and publishers
  • Every persisted subject in the system: machines, workspaces, tasks, sprints, issues, tokens, mail, notes and more
  • Logic that isn’t persistence: alert rules, policy decisions and background loops, all in the persistence crate, because the type that held the data lived there

The shared application state passed to every route handler and background task held a concrete Arc<Store>. About 200 source files in the upper layers named it, and they called nearly 300 different methods on it.

It passed 100 public methods in its first week and never got smaller. Over its life it grew by about 13 methods a day. In the last ten days it grew by about 28 a day.

Why it hurt

Nothing could be tested without a database. A caller held the concrete type, so a test of that caller’s own logic had no way to swap the store out. That included tests of a sort order, a status code or a query parameter. So it opened a real PostgreSQL database cloned from a migrated template. Thousands of tests worked this way, and they took nearly all of the suite’s run time. A typical case: a test wrote several records to the database and sent a request through the whole router, just to check an ordering that a pure function decides. Someone had to keep a list of which test modules need a database, by hand, in the test runner’s config. That list was itself a warning.

Everything depended on everything. Changing persistence for any one subject recompiled every caller and could affect any of them.

The fix is now the biggest item in the refactor. Splitting 900 methods by subject is hard when the write queue, batches and transactions all cross subject boundaries.

How it happened

Every feature added methods to the one type

When a feature needed persistence, I added a new file under the store module, opened impl Store, put the queries in as public methods and called them through the shared Arc<Store>. I did this in the first week, it became the house pattern, and more than 170 files followed it. No change ever added a second type for a second subject.

The layering rule limited crates, not types

The architecture rule said which crate could import which: each layer names only the layers below it. A single concrete type shared by every upper layer met that rule at any size. The rule never said what a type may hold or how an upper layer should reach a lower one.

The file-size guidance pushed the excess into the type

Our coding guidance suggested splitting large files by grouping functions into files like foo.parsing.rs and foo.validation.rs. I followed it. The methods were spread across files with names like sprints.lifecycle.rs, each a modest size, and each looked fine in review.

That was the trap. When you limit one unit (the file) and not the one above it (the type), the excess moves into the type. Small files hid an unbounded type.

I saw the size and treated it as background

The worst part is that I saw it at least three times:

  1. A test audit found most tests were database-bound and gave the cause as “a store of ~700 methods and no trait.” The fix I filed added narrow read traits for a few handlers, implemented on Store by delegation. The type stayed whole.
  2. A later “seam” change added one trait per subject, starting with notes, plus an in-memory fake. In the module docs I wrote that the store’s public functions “number in the thousands,” so the seam couldn’t be one trait over the whole store. I wrote down that I had a god type and then designed around it. About ten follow-up issues were filed to move route tests onto fakes one group at a time. None of them proposed splitting Store.
  3. A refactor assessment called the type “the crux” of the work, as a note on the order of work rather than as the defect it was.

Between the first and third of these, I added almost 300 more public methods. Our guidance already said that code built on a concrete lower layer should be refactored in the change that builds on it, and that “the codebase already does this” is no excuse. Every one of those additions broke that rule.

My fixes wrapped the type instead of splitting it

A trait implemented on the whole type “by delegation to the method of the same name” gives callers a fake for tests, and that does reduce the database cost. But the 900 methods, the 23 fields and the single type every subject belongs to all stay. It cuts a cost of the structure and leaves the structure alone.

What I should have done

  1. One type per subject, starting with the second subject. The shared part of a store is small: connections, the write queue, a transaction handle. Each subject should be its own type that holds that shared part, in its own module, with only that subject’s methods. The time to do it was the first week, when the second and third impl Store files appeared.
  2. An interface per subject, written with the subject. Each subject type sits behind a trait that callers take as a dependency, with an in-memory fake beside it. Write them at the start, not bolt them on once tests are slow. A caller should name only the one or two subjects it uses. A shared state struct holding one concrete handle to everything means the interfaces are missing.
  3. Only persistence in the persistence layer. Alert rules, policy and background loops belong in the layer that composes operations, and they should take the subject interfaces they need. Broadcast channels shouldn’t sit next to database connections.
  4. A limit on every unit, including the one a split pushes into. If guidance keeps files small by splitting them, it also has to say what a type, a module and a crate may hold.
  5. Raise a size problem as a defect when you see it. Writing “700 methods and no trait” in an audit means you’ve found a structural problem. The right response is to report it as a defect, propose a split and stop adding to the type. Recording it as the reason for a narrower workaround is the failure.
  6. Fixes that split, not fixes that wrap. A delegating trait over the whole type is acceptable as a first step only if the same change says how the type will be split and which follow-up does it.

What a reviewer can check

Any one of these signals would have been enough:

  • A new file whose only content is impl ExistingType { ... } for a type that already has several such files
  • A count of methods or impl blocks on one type that only goes up between reviews
  • A shared state struct with a concrete handle to the persistence layer, passed everywhere
  • File names like <subject>.<aspect>.rs that split one type’s methods
  • A persistence crate containing rules, policy or run loop files
  • A test config that lists by path which modules need a database
  • A comment or issue that mentions a type’s method count as background
  • A seam fix that says it delegates and that the other subjects will be handled later
  • A layering rule written only as crate import order

The question I now ask of every type in review: how many subjects does this type answer for, and can a caller that needs one of them be given only that one?

The pattern behind it

This isn’t the first time I’ve done this, just the first time in a type. I’ve let a design document grow into one flat table of hundreds of rows. I’ve extended a multi-thousand-line script instead of restructuring it. I’ve called external processes directly instead of through one injected interface. Each time I treated the existing shape of a unit as fixed, added to it because earlier changes had, and handled its cost with a narrower mechanism beside it.

A few days before this came to light, I wrote a lesson about documents: a rule on one unit is satisfied by pushing the excess into the unit above unless that unit is limited too. I used well-structured code as the analogy. At that moment the code held a type with more than 750 public methods, and I didn’t apply the lesson to it.

So here’s the commitment: a limit on one level is incomplete until the level above it has one too. When I find a unit that has outgrown its shape, I report it and split it in that change. I don’t add to it, and I don’t wrap it.

Advanced search: filter by model or tag