Coding Workflows Workflow Advanced

AI Legacy Code Modernization Workflow

Update old, risky code you didn't write — safely — by understanding and pinning its behavior in tests before you change a single line.

The problem

Modernizing old code is dangerous precisely because nobody remembers what it's allowed to do. The original author is gone, the edge cases are undocumented, and the behavior you'd casually clean up turns out to be load-bearing for some customer you've never met. Rushing into a rewrite is how a tidy diff becomes an incident. The safe order is the opposite of the tempting one: understand first, capture the behavior, protect it with tests, and change the shape only after the meaning is pinned down.

Recommended workflow

Each step uses an existing NewPrompt tool, pre-filled by a matching resource. Open the resource to read it, or jump straight into the tool with the inputs ready.

  1. Recover what the code actually does

    Have the AI walk the legacy section and surface its real behavior, including the assumptions and edge cases written down nowhere. You can't safely modernize logic you can only half-read.

    Outcome An honest map of current behavior, including the parts that look like bugs but might be features.
  2. Review the risk before you touch it

    Run a review aimed at legacy code specifically: where the hidden coupling is, what breaks if you move things, and which parts are safe to change versus radioactive.

    Outcome A risk map showing where modernization is cheap and where it's a trap.
  3. Pin the behavior with characterization tests

    Write tests that capture what the code does today — not what it ought to do. These are the safety net: if modernization changes an output, the tests say so before users do.

    Outcome A test suite that fails the moment behavior changes, intended or not.
  4. Modernize without changing meaning

    Now update the code to the current language and patterns, with behavior preservation as a hard constraint and the step-3 tests as the referee for every change.

    Outcome Code that reads like it was written today and still does exactly what it did yesterday.

Expected outcome

The legacy code is understood, its current behavior is captured in tests, and it has been brought up to current standards with no unintended change in what it does. You can ship the update without holding your breath.

Best for

  • Updating inherited code with no original author around
  • Migrating old patterns to a current language version
  • Touching code where a behavior change would be costly

Not for

  • Greenfield code you're writing from scratch
  • Small readability cleanups on code you already understand — use the AI Refactoring Workflow

FAQ

AI legacy modernization workflow vs AI refactoring workflow?

The legacy modernization workflow front-loads understanding and characterization tests because you don't own the code; the refactoring workflow assumes you already understand it. Modernization adds steps 1-2 (recover behavior, map risk) and pins behavior with tests before any change. Refactoring skips straight to reshaping code you already trust.

What do I get out of the AI legacy modernization workflow?

Four artifacts: a behavior map of what the legacy code actually does, a risk map of safe-versus-radioactive areas, a characterization test suite that fails on any behavior change, and modernized code that reads current but behaves identically. You produce each by running that step's prompt in your own AI tool.

How do I run the AI legacy modernization workflow?

Work the four steps in order, each with its own prompt: code-explanation to recover behavior, code-review-generator for the risk map, test-case-generator for characterization tests, then refactor-prompt-builder to modernize. NewPrompt supplies the prompts and order; you run them in your AI tool and review every output before moving on.

Why write tests for behavior that might be wrong?

Because 'wrong' behavior in legacy code is often load-bearing. Characterization tests capture what is, so modernization can't change it silently. Fix genuine bugs as a separate, deliberate step — never as a side effect of cleanup.

Can I skip steps in the AI legacy modernization workflow?

No — the order is the safety mechanism. Steps 1-3 (recover behavior, map risk, pin characterization tests) are precisely what make step 4's modernization safe; skipping them is how a tidy diff becomes an incident. Only skip the whole workflow if you already fully understand the code and own its behavior.

Does the AI rewrite my whole file?

No. Each tool generates a prompt you run against your own AI, and the refactor step treats behavior preservation as a hard rule. You stay in control of what actually changes.

Part of these projects

Complete build journeys that include this workflow as a stage.

Recommended next workflow

Tip: Each step's resource opens its tool pre-filled — start at step one and carry the output forward.