Fundamentals

Two frameworks, one app: adding to legacy code

Add a feature inside old, coupled code and the change spreads; add it behind a seam and the old code barely moves.

6 min readMatters more in the AI era

The failure

Add a feature inside old, coupled code and the change does not stay where you put it. The code leans on globals and big shared classes, so everything that calls them can now behave differently. Most of those paths have no test. Unit tests are hard to write there. A reviewer has to read the classes around the diff to judge it, and the diff shows only a small part of what moved.

The simulator

Start inline, in the shared code. Switch to behind the seam, pin the legacy behaviour, then port the page.

SimulatorAdd one feature to old, coupled code

The change

Turn on JavaScript to change the approach.

Approach
Where the feature lands

Available behind the seam

Legacy lines touched
660
Legacy nodes edited
5
Nodes affected
8 legacy · 0 new
Untested paths affected
268
Lines to read around the diff
22,800
Share of the diff an architecture test can check
0 %

Legacy: 46,300 lines · 275 untested paths · 20 % covered. New side: 88 % covered.

Tests that reach the change

LevelReaches itWhy
Unit testsnoNo: globals and large shared classes cannot be isolated.
HTTP testsnoNo: nothing pins how the old pages behave yet.
End-to-end testsyesYes: only through the server-rendered pages, which inverts the pyramid.

Every scenario

ScenarioLegacy lines touchedLegacy lines removedNodes affectedUntested paths affectedLines to readCheckable shareLegacy lines left
Inline, shared code (current)66008 legacy · 0 new26822,8000 %46,300
Inline, independent area55004 legacy · 0 new1447,0000 %46,300
Behind the seam1001 legacy · 5 new192,00099 %46,300
Behind the seam + port203,0001 legacy · 6 new233,20099 %43,300

Nodes

NodeSideLinesCoverageState in this changeCalls
Front controller and routerlegacy60020 %affectedPage controller, Other pages using the same classes, Boot the new framework
Shared globalslegacy80010 %edited-
Sessionlegacy40030 %untouched-
Big shared model classlegacy9,00015 %editedShared globals
Big service classlegacy6,00015 %editedBig shared model class, Helper functions, Shared globals
Helper functionslegacy2,50030 %affectedShared globals
Page controllerlegacy3,0008 %editedBig shared model class, Big service class, Helper functions, Server-rendered views, Shared globals, Session
Server-rendered viewslegacy4,0000 %editedHelper functions, Shared globals
Other pages using the same classeslegacy20,00030 %affectedBig shared model class, Big service class, Helper functions, Server-rendered views, Shared globals, Session
Boot the new frameworknew15075 %untouchedRead the legacy session
Read the legacy sessionnew80100 %untouchedSession
Controller with early validationnew--not built-
Handlernew--not built-
Repository returning DTOsnew--not built-
Separate front endnew--not built-

Model with illustrative nodes, lines and paths, not the real codebase. Coverage levels follow the real ones roughly: about 20 % on the legacy side, about 80 % on the new side.

What’s happening

The map is a model. Its nodes, lines and paths are illustrative. What it shows is how a change spreads along calls.

Inline. One edit to the globals reaches almost every legacy node, because almost every legacy node calls them. Where the feature lands matters. In an independent area the blast radius is smaller, but it still runs through the shared pages and the classes behind them. Either way, most of the paths the change can affect have no test.

Behind the seam. The legacy side gets one small edit: a route that boots the new framework. The session is the one legacy dependency. The new code sits in layers: validation early in the controller, a specific handler, a repository that returns collections and DTOs, JSON endpoints, and a separate front end.

The diff is larger, because it is mostly new code. The code a reviewer has to read around it is much smaller, and almost all of it sits in layers with an expected shape. That is also what lets an architecture test check it.

Test levels. Inline, the change is mostly reachable end to end, through the server-rendered pages. That inverts the test pyramid. Behind the seam, unit, HTTP and end-to-end tests can all reach it.

The seam is legacy code too. It has few tests. HTTP characterisation tests pin how it behaves before you change it. In the model they pin only the code you edit. Its callers stay unpinned.

Strangler fig. Once the seam exists, the legacy page a project touches can move behind it. Its route moves, its legacy lines are retired and its coverage rises. The shared classes it used stay, because other pages still need them. Legacy code left shrinks only when something is ported. Building next to it does not shrink it. Characterisation tests written against the old page keep checking the ported one, because the URLs behave the same.

Where I hit this in production

The app was 13 years old and about 500,000 to 600,000 lines, roughly 70 % backend and 30 % front end. By the numbers above, that is roughly 350,000 to 420,000 lines of backend and 150,000 to 180,000 of front end. Two frameworks lived side by side: an old one, CodeIgniter, and Symfony. Line coverage, measured by a tool, was about 20 % on the CodeIgniter code and about 80 % on the Symfony code.

The CodeIgniter code was hard to test. It relied on many globals reused in many places, on large classes that did many things and were used in many places, and on a lot of coupling.

It was not one feature. It was many features over many years. Most took a few months, bigger ones up to a year, and there were many a year. The team rotated, so the app shows layers of how things were done over time. Some features landed in the riskier parts of the app. Others were new and independent, and easier to work with. Symfony already existed in the app, but it was not fully used.

The seam. Symfony could be booted from anywhere in the legacy app. The one remaining dependency on legacy code was the session. Everything else ran on Symfony, with its conventions and tests, modules kept reusable and inside their domain, and an expected structure. Over the years the seam touched a few thousand legacy lines in total.

The new layers. Validation came early, at the controller. Then very specific handlers with a clear structure, for both POST and GET payloads. Reading data was organised too: collections were easy to retrieve and came back as DTOs with an expected structure. Endpoints returned plain JSON instead of the old framework’s server-rendered pages, so they were much easier to test. The front end was fully separate from the back end, similar to an SPA. Tests were PHPUnit, HTTP integration tests and end-to-end tests.

What it bought. Reviews were much easier, because the structure of the code was easy to see. Legacy code was replaced piece by piece while features kept shipping. Regressions concentrated in the places with more legacy code. Every new module had about 80 % coverage. Porting was targeted by which projects were being worked on, so legacy that was not changing was not blocking.

What I’d do differently. Add HTTP characterisation tests much earlier, to pin down how the legacy code behaves before changing it. And decide the structure much earlier. The strategy itself worked. The test pyramid was partly inverted, with more end-to-end tests on the legacy part, because unit tests there were hard: the dependencies and the way the old framework works got in the way.

Why it matters more with AI

Agent-written code struggled in the parts that did not follow the standards. Specific rules and architecture tests in the harness made it much easier for agents to write code there. Given a structure, an easy way to seed data and a set shape for tests, agents also wrote many more end-to-end tests. That raised coverage a lot and raised confidence in releasing.

What worked was giving developers a high-level view of how the codebase and its architecture work, so they could give agents the right rules. The company wanted more code written in this new era without losing quality. More checks, more structure and more of the codebase written to good standards meant agents could write more code on their own.

← All fundamentals