I ported the old code's structure into the new architecture instead of mapping the functionality onto it
I was porting a library that supports about a dozen programming languages to our layered architecture. It has an IO layer for effects, a service layer for orchestration and an init layer for wiring. My design kept the old code’s decomposition and gave each existing piece the layer that seemed closest. Over the next day the review findings kept pointing at problems with that shape, and I fixed each one inside the shape. Here is what went wrong and what I’m changing.
What I built
- Toolchain effects as services. Each language family’s toolchain logic became a “service”: write the project tree, run the compiler, read what it produced. That meant nine separate service interfaces, one per substrate. Below them sat one generic “run a process” IO and a files IO. This is where the old code had its boundary, so that’s where I put it.
- A per-language layer that did nothing. Above the substrate services I had one struct per language. Every method on it either forwarded to the substrate service or returned a constant.
- Then I doubled it. The architecture has a rule that each service implementation implements exactly one interface. Each per-language struct implemented two traits, and one was a supertrait of the other. My fix was to split every struct in two, which turned twelve forwarding structs into twenty-four. The design doc I landed said the two halves “share every IO and value the language reads”. In other words, I described two objects by how alike they were.
All this time, every caller reached a language through two interfaces that don’t depend on the language, with the same signatures for every language. No caller ever needed anything specific to one language.
Why it was wrong
- The effect boundary was in the wrong place. Driving a compiler is an effect on external state. Knowing how to drive one particular toolchain is what an IO implementation is for. I put that knowledge in services, so the only replay and test seam was down at the raw process call. That produced a very large set of recorded individual tool invocations, and no interface meant “the C++ toolchain answered this”.
- Language-specific logic ended up in three places: constants in the per-language structs, preparation logic in the substrate services, and state computed from the build recipe, which the middle layer read and passed down on every call. The middle layer existed only to join the other two.
- I wrote nine interfaces for one job. They all had the same six operations. They differed only in extra parameters, and each extra was either which language it was (C# or F#, Java or Kotlin) or state fixed when the object was built.
- I missed a pattern the architecture already documents. It describes a router IO for an ADT whose variants need very different handling. The router holds one implementation per variant, dispatches on the value the caller passes, and never exposes the per-variant implementations to services. A language is exactly that kind of ADT. I didn’t name the pattern, and neither did the reviewers or the coordinator.
- For the one-interface finding, I picked the most expensive fix that would pass. Composition would also have passed, or a router (no per-language struct at all), or a documented exception. I offered only “split” or “ignore”, and neither of those changed the unit.
What I should have designed
I should have started from what the library does: twelve languages, the same operations for all of them, each driving an external toolchain its own way, plus a few constants per language. Then I should have picked the architecture’s shapes for that:
- Router IOs keyed by a language ADT. One IO interface per kind of toolchain work, the same for every language, with the language passed as a value. Each substrate implementation does its own file and process effects, holds its recipe-derived state as fields that the init layer sets, and refuses at entry any language it doesn’t handle. A router holds one instance per enabled language and dispatches. The shared process-spawning and recording machinery becomes an init-layer resource.
- Two services above that, one implementation each. A program service that takes the language ADT, and an agent service that holds the program service and adds the agent-only operations. That’s composition, not a split. A language’s own constants become plain data functions over the ADT.
With that shape there’s no per-language struct to split, the effect boundary sits at the toolchain, the recordings become a replay implementation of the toolchain IO, and nothing changes for callers.
What I’m changing
- Before I port anything, I list what it does, not what types it has. “Same behavior, same tests” is a fine acceptance criterion. It’s a bad design method if I take it to mean “same structure”.
- I name the documented pattern before choosing a fix. When a review finding lands, my first question is which page of the architecture describes this functionality. I should answer that before I ask how to make the current struct pass.
- I treat “every method forwards” as a finding about the layer. If a struct only forwards or returns constants, the struct is the problem. In my own notes I wrote that none of the agent halves called a program-half method, and I read that as support for the split. It actually showed the struct wasn’t the right unit.
- An effect sequence is an IO. If a “service” mostly writes files and runs an external tool, it belongs in an IO implementation, whatever primitive sits under it.
- N traits with the same methods mean one interface plus a value. When implementations differ only in extra parameters, those parameters belong in the variant or in construction-time state.
- The fix options should include changing the unit. If every option keeps the current unit and multiplies it, I’ve framed the question too narrowly.
- I stop and rethink after the second patch. This design went through six patches built on one premise: per-language structs, then per-substrate enums, then bigger enums, then the split, then a transition note on a page the split broke, then a double initialization. Long before the sixth patch, the premise should have been what I questioned.
The old code was never sound enough to keep shape for shape. I translated it into the new architecture’s vocabulary and then defended the translation against the checker. Next time I’ll start from what the code does and fit that to the architecture.