Expand-Contract: Migrating Patterns Without a Big Bang

Expand-Contract: Migrating Patterns Without a Big Bang

Atomic cutovers strain codebases that keep shipping. Expand-contract sequences a migration so every intermediate state is releasable, with a lint ratchet to hold the line.

An atomic cutover assumes a frozen codebase, and a continuously shipping one never freezes — while a sweeping rewrite branch ages, the trunk keeps adding new call sites of the old form, and the merge becomes its own project. Expand-contract sequences the migration instead: introduce the new form alongside the old, migrate call sites in verified slices, then delete the old form once its caller count reaches zero. A lint ratchet holds the line against new old-form usage mid-migration, and the two-forms tax is real — a migration needs a credible path to contract before it expands at all.

Why in-place swaps strain at scale

An atomic in-place swap — replace the old pattern with the new one everywhere, in one commit — carries a hidden assumption: that the codebase holds still while the swap happens. In a continuously shipping codebase, it never does. While a large sweeping branch is being written, reviewed, and re-reviewed, the trunk keeps moving; other engineers keep adding new call sites of the old pattern, because the old pattern is what still exists in the trunk they are working from. By the time the sweeping branch is ready to merge, it is out of date against a moving target, and reconciling the two has become its own separate project layered on top of the original migration.

The all-at-once diff has a second failure mode independent of staleness: it is hardest to review at exactly the moment review matters most. Hundreds of changed files behind one approval button work against the line-by-line verification that review exists for; Google's engineering-practices guide on small changes notes that with large changes, reviewers and authors tend to get frustrated by large volumes of detailed commentary shifting back and forth, sometimes to the point where important points get missed or dropped. A reviewer facing hundreds of changed files has, realistically, three options: spend days they do not have, spot-check a sample and hope the rest follows the same pattern, or approve on trust. All three fall short of the review a smaller, sliced diff would have allowed, and none of them is the reviewer's fault — the diff size made a thorough review far harder before the first comment was ever left.

Releasability is why expand-contract suits this setting — Danilo Sato notes the pattern is particularly useful when practicing continuous delivery, because it allows code to be released in any of its three phases. In a codebase that ships continuously, every intermediate state has to build, pass its tests, and deploy safely. A migration technique that requires a long-lived, unmergeable branch violates that constraint by design. One that only ever asks the codebase to be in a valid state does not.

The expand-contract sequence

The pattern, which Sato describes on Martin Fowler's site as parallel change — a way to make backward-incompatible changes to an interface safely — runs in three phases.

Expand: introduce the new form alongside the old one. Both work. Nothing that currently depends on the old form breaks, because the old form has not been touched yet — the commit is purely additive, which keeps its risk low.

Migrate: move call sites over to the new form in slices — by module, by pattern instance, by whatever grouping makes each slice reviewable on its own. Each slice is verified green before landing, as its own commit or its own small pull request.

Contract: once the old form's caller count reaches zero, delete it. This deletion is mechanical, and the zero caller count is the evidence for it: by this point nothing in the codebase references the old form anymore — the same confidence-tier discipline that governs any dead-code deletion applies directly here.

text
Phase 1 (expand):    introduce fetchUserV2() alongside fetchUser()
Phase 2 (migrate):   move callers to fetchUserV2(), module by module,
                      each slice landed and verified independently
Phase 3 (contract):  once fetchUser() has zero callers, delete it

The pattern is not limited to function signatures — the same three-phase shape applies from small function-level swaps up through module-boundary changes, wherever an old form and a new form need to coexist safely during a transition.

Holding the line mid-migration

The migration's real enemy is not the existing call sites of the old form — those are known, counted, and being worked through deliberately. The enemy is new old-form usage appearing behind the sweep, which can accumulate faster than the migration clears it. Colleagues working on unrelated features copy the pattern that already exists nearby, because copying existing, working code is the natural thing to do — and until the migration is well along, the old form is what a colleague is likely to find nearby.

The lint ratchet is the mechanism for holding the line: a check that fails the build when occurrences of the old form grow past the known remaining count that has not been migrated yet, while tolerating that remainder. The rule for the ratchet is simple and one-directional — the tolerated count may only go down, never up. As long as the cap is lowered to the new count each time a slice lands, every new violation is caught the moment it is introduced, rather than discovered later as one more instance to migrate.

javascript
// A lint ratchet: fails on growth, tolerates the known remainder
const MAX_LEGACY_FETCHUSER_CALLS = 14; // decreases as migration proceeds

const currentCount = countOccurrences('fetchUser('); // placeholder for your codebase search, for example a lint rule or an AST query
if (currentCount > MAX_LEGACY_FETCHUSER_CALLS) {
  throw new Error(
    `fetchUser() usage grew to ${currentCount}, ` +
    `expected at most ${MAX_LEGACY_FETCHUSER_CALLS}. ` +
    `Use fetchUserV2() for new code.`
  );
}

Making the new form the discoverable default helps on the same front. A deprecation marker on the old form — which editors with deprecation-aware language tooling can show at the moment someone is about to copy it — can surface the guidance right where a colleague is making the choice, rather than relying on them to already know a migration is underway.

The two-forms tax

The period during which both forms exist is a real, ongoing cost, not a neutral waiting room. Two ways to do the same thing means a reader can be left asking which one is current, and a new contributor may have to guess, or ask, or copy whichever example they happened to find first.

A migration stalled halfway can leave a codebase worse off than one that never started; as Sato puts it, "If the contract phase is not executed you might end up in a worse state than you started." A codebase that never began the migration pays no two-forms tax at all — it simply has the old form, consistently. A codebase mid-migration pays the tax indefinitely if the migration stalls, without ever collecting the full benefit the new form was supposed to deliver, because the payoff of a single, unambiguous form only arrives once the old form is gone.

This is why finish-line honesty matters before a migration even begins expanding. "We will migrate the rest eventually," with no concrete mechanism behind it — no lint ratchet, no tracked remaining count, no owner — is how a codebase can end up with three different ways to do the same thing, each one a partial migration that stalled before contracting. A credible path to the contract phase is a precondition for starting the expand phase, not an afterthought to figure out later.

Where PR-scale ends

This article's territory is in-codebase pattern migration that one engineer can own end to end: a function signature, a data-access pattern, a validation approach, replaced across a codebase whose call sites are all internal.

Framework and stack migrations — moving an application from one framework or major version to another — bring ecosystem compatibility, build-tooling, and dependency-graph dimensions that this three-phase shape does not fully capture on its own; that migration category has its own literature, named here and deferred rather than folded in.

Public API contract changes bring external consumers into the picture, along with the versioning and deprecation-notice obligations that come with breaking a contract other teams or other companies depend on. The expand-contract shape recurs there too — old and new endpoints coexisting during a transition is a familiar pattern — but the constraints around timing, communication, and backward compatibility differ enough that it deserves its own treatment, also named and deferred here.

What stays constant across every scale — function, module, and the larger territories named above — is the underlying discipline: never force a single atomic moment to carry the entire weight of a transition. Whether the transition is a renamed function or a migrated framework, the same three questions apply. Can both forms coexist safely while the migration runs. Can the remaining count of the old form only go down, never quietly climb back up. And is there a credible, concrete mechanism for reaching zero, rather than an intention to get there eventually.

Project Inquiry