DEV Community

Attila Albert
Attila Albert

Posted on

Use an AI coding assistant to pin down legacy code before you change it: characterization tests, step by step

By Priya Raman, NBT

You've inherited a function nobody wants to touch. No tests, three hundred lines, a comment from years ago that says "don't change the order". You need to change it this week.

The worst move is to ask an AI coding assistant to "refactor and clean this up" straight away. It will produce something tidier, and you'll have no way of knowing whether it still does what the old code did, including the odd behaviour that some other part of the system quietly relies on.

The better move is to first write characterization tests: tests that record what the code does today, not what it should do. Once those pass, any change that alters behaviour shows up as a red test. AI assistants are very good at this kind of tedious, thorough work, as long as you stop them from "fixing" things on the way.

The examples use Claude Code, whose "Common workflows" page (read on 6 December 2026) covers finding untested code and adding edge-case tests. The prompts work in any agent. For more on writing prompts like these, see how to prompt an AI coding assistant.

The one rule: record, don't judge

Put this at the top of every prompt in this process:

We are writing characterization tests. The goal is to record what the
current code does, including behaviour that looks wrong. Do not change
any code under src/. If you think something is a bug, write the test
for the current behaviour anyway and add a comment starting with
"SUSPICIOUS:" explaining why.
Enter fullscreen mode Exit fullscreen mode

Without this, the assistant will "help" by asserting what the function ought to return, and the test fails. Or, worse, it changes the function so the test passes. Either way you've lost the record of the old behaviour.

Step 1: map the behaviour before writing tests

Read src/billing/invoice.js, function calculateInvoice. Don't write tests
yet. List:
1. Every input it reads (arguments, globals, config, the clock, the database).
2. Every branch (if/else, switch, early return, thrown error) with the
   condition that triggers it.
3. Every output and side effect (return value, writes, logs, calls out).
Number the branches so we can refer to them.
Enter fullscreen mode Exit fullscreen mode

You now have a checklist, and it usually shows where testing will be hard: the function reads the current time, calls a database, or writes a file. That's step 2.

Step 2: make it testable without changing behaviour

If the function calls new Date() or hits a database directly, a test can't control it. Keep the change as small as possible:

For the clock and the database call, propose the smallest change that lets
a test pass in a fixed time and a fake database: for example an optional
parameter with the current behaviour as the default. Show the diff and wait
for my OK. Callers must not need any change.
Enter fullscreen mode Exit fullscreen mode

Read this diff yourself. It's the only production change in the whole process, and it should be dull: a default parameter, nothing else.

Step 3: tests from real inputs

The best test inputs are ones the code has actually seen. If you have logs, fixtures or a staging database, give the assistant a handful of real cases, with secrets and personal data removed:

Here are 8 real inputs from last month (anonymised) in fixtures/invoices.json.
For each one, run calculateInvoice with the fixed clock and fake DB and write
a test that asserts the exact output it produces today. Match the style of
the existing tests in tests/ (framework, file naming, assertion style).
Enter fullscreen mode Exit fullscreen mode

The Claude Code docs note that it looks at your existing test files to match the style, framework and assertion patterns. If you have no tests at all, name the framework you want.

Step 4: walk every branch

Go back to the numbered list from step 1:

Which branches from the list are not covered by the tests so far? Add one
test per uncovered branch, with the smallest input that reaches it. Then
add tests for edge values: zero, negative numbers, empty lists, very large
amounts, missing optional fields, and the boundaries in each condition
(for example exactly 100 if the code checks > 100).
Enter fullscreen mode Exit fullscreen mode

Then run coverage if your stack has it (npx jest --coverage, pytest --cov, go test -cover) and ask for tests for any lines still not run. Coverage doesn't prove the tests are good. It does show you where you haven't looked.

Step 5: prove the tests can fail

A test suite that can't fail is worthless, and AI-written tests sometimes assert so little that nothing could break them. Check quickly:

Temporarily make three small changes to calculateInvoice, one at a time:
flip a > to >=, change a rounding call, and swap the order of two discount
steps. Run the tests after each and tell me which tests caught it. Revert
every change afterwards and show me git diff to prove src/ is unchanged.
Enter fullscreen mode Exit fullscreen mode

If a change goes uncaught, ask for a test that catches it. This is a hand-made version of mutation testing, and it's the step people skip most and regret most.

Step 6: review the SUSPICIOUS list

By now you'll have a few tests marked SUSPICIOUS:. Go through them with someone who knows the business rules:

  • It's a bug nobody relies on: fix it later, in its own commit, by changing the test and the code together.
  • It's a bug something relies on: leave it. Now at least it's written down.
  • It's not a bug, just strange: replace the comment with the reason, for future readers.

Now change the code

With the tests green, make your real change in small steps and run the suite after each one. Something like: "Extract the discount logic into its own function. Behaviour must not change. Run the characterization tests after the change and stop if any fail."

A red test now means one of two things: you changed behaviour by accident, or you meant to and need to update that one test on purpose. Either way, you find out in seconds instead of from a customer.

A checklist

  • [ ] "Record, don't judge" rule in every prompt
  • [ ] Behaviour map with numbered branches
  • [ ] Smallest possible change to control the clock, the database and files, reviewed by you
  • [ ] Real inputs first, then one test per branch, then edge values
  • [ ] Three deliberate breakages, each caught
  • [ ] SUSPICIOUS tests reviewed with someone who knows the rules
  • [ ] src/ unchanged until the suite is green

An AI coding assistant won't tell you what legacy code was meant to do. It will write down what it does, quickly and thoroughly, which is exactly what you need before you change anything.

Top comments (0)