{"slug": "use-an-ai-coding-assistant-to-pin-down-legacy-code-before-you-change-it-tests-by", "title": "Use an AI coding assistant to pin down legacy code before you change it: characterization tests, step by step", "summary": "A developer outlines a step-by-step workflow for using AI coding assistants like Claude Code to write characterization tests around legacy code before modifying it. The process starts with a prompt that forbids changing source code and records current behavior, including suspected bugs flagged with a \"SUSPICIOUS:\" comment, then inventories inputs, branches and side effects, makes minimal dependency-injection changes for the clock and database, seeds tests with anonymized real inputs, and fills coverage gaps branch by branch.", "body_md": "*By Priya Raman, NBT*\n\nYou'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.\n\nThe 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.\n\nThe 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.\n\nThe 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](https://www.ninjabuilder.app/blog/how-to-prompt-ai-coding-agents).\n\nPut this at the top of every prompt in this process:\n\n```\nWe are writing characterization tests. The goal is to record what the\ncurrent code does, including behaviour that looks wrong. Do not change\nany code under src/. If you think something is a bug, write the test\nfor the current behaviour anyway and add a comment starting with\n\"SUSPICIOUS:\" explaining why.\n```\n\nWithout 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.\n\n```\nRead src/billing/invoice.js, function calculateInvoice. Don't write tests\nyet. List:\n1. Every input it reads (arguments, globals, config, the clock, the database).\n2. Every branch (if/else, switch, early return, thrown error) with the\n   condition that triggers it.\n3. Every output and side effect (return value, writes, logs, calls out).\nNumber the branches so we can refer to them.\n```\n\nYou 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.\n\nIf the function calls `new Date()` or hits a database directly, a test can't control it. Keep the change as small as possible:\n\n```\nFor the clock and the database call, propose the smallest change that lets\na test pass in a fixed time and a fake database: for example an optional\nparameter with the current behaviour as the default. Show the diff and wait\nfor my OK. Callers must not need any change.\n```\n\nRead this diff yourself. It's the only production change in the whole process, and it should be dull: a default parameter, nothing else.\n\nThe 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:\n\n```\nHere are 8 real inputs from last month (anonymised) in fixtures/invoices.json.\nFor each one, run calculateInvoice with the fixed clock and fake DB and write\na test that asserts the exact output it produces today. Match the style of\nthe existing tests in tests/ (framework, file naming, assertion style).\n```\n\nThe 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.\n\nGo back to the numbered list from step 1:\n\n```\nWhich branches from the list are not covered by the tests so far? Add one\ntest per uncovered branch, with the smallest input that reaches it. Then\nadd tests for edge values: zero, negative numbers, empty lists, very large\namounts, missing optional fields, and the boundaries in each condition\n(for example exactly 100 if the code checks > 100).\n```\n\nThen 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.\n\nA test suite that can't fail is worthless, and AI-written tests sometimes assert so little that nothing could break them. Check quickly:\n\n```\nTemporarily make three small changes to calculateInvoice, one at a time:\nflip a > to >=, change a rounding call, and swap the order of two discount\nsteps. Run the tests after each and tell me which tests caught it. Revert\nevery change afterwards and show me git diff to prove src/ is unchanged.\n```\n\nIf 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.\n\nBy now you'll have a few tests marked `SUSPICIOUS:`. Go through them with someone who knows the business rules:\n\nWith 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.\"\n\nA 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.\n\n`src/` unchanged until the suite is green\nAn 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.", "url": "https://wpnews.pro/news/use-an-ai-coding-assistant-to-pin-down-legacy-code-before-you-change-it-tests-by", "canonical_source": "https://dev.to/attilaalbert/use-an-ai-coding-assistant-to-pin-down-legacy-code-before-you-change-it-characterization-tests-b67", "published_at": "2026-12-06 10:07:04+00:00", "updated_at": "2026-12-06 10:14:45.438060+00:00", "lang": "en", "topics": ["ai-tools", "developer-tools", "ai-agents"], "entities": ["Claude Code", "NBT", "Priya Raman", "Jest", "pytest"], "also_reported_by": [], "takeaway": "Prompt the assistant to record current behavior as characterization tests with a no-source-changes rule and \"SUSPICIOUS:\" comments for suspected bugs, then inject the clock and database via optional default parameters so tests run deterministically.", "recommendation": {"advice": "Before refactoring legacy code with an AI assistant, have it write characterization tests of current behavior, forbid edits under src/, and flag suspected bugs with \"SUSPICIOUS:\" comments instead of fixing them.", "playbook": "coding-agents", "playbook_url": "https://wpnews.pro/playbooks/coding-agents"}, "alternates": {"html": "https://wpnews.pro/news/use-an-ai-coding-assistant-to-pin-down-legacy-code-before-you-change-it-tests-by", "markdown": "https://wpnews.pro/news/use-an-ai-coding-assistant-to-pin-down-legacy-code-before-you-change-it-tests-by.md", "text": "https://wpnews.pro/news/use-an-ai-coding-assistant-to-pin-down-legacy-code-before-you-change-it-tests-by.txt", "jsonld": "https://wpnews.pro/news/use-an-ai-coding-assistant-to-pin-down-legacy-code-before-you-change-it-tests-by.jsonld"}}