Sub topic 2
Refactoring versus migration: what changes and what must not
This chapter assumes you can read and write code in one language, use basic Git (branches, commits, diffs), and run a test suite and linter from the command line. It also assumes you have used an AI coding assistant before.
Two kinds of change
A refactoring changes the structure of code and keeps its observable behavior the same. Extracting a function, renaming a variable, or splitting a long module are refactorings. Callers see the same inputs and outputs before and after.
A migration moves code from one technology to another. Upgrading a framework, replacing a library, or changing an API version are migrations. Behavior should usually stay the same for users, but the code, its dependencies, and often its tests must change.
The distinction decides what you ask the agent to do and what you check afterward.
| Row | Refactoring | Migration |
|---|---|---|
| What changes | Internal structure | Libraries, APIs, or frameworks |
| What must not change | Observable behavior and public signatures | User-facing behavior |
| Typical scope | One function, file, or module | Many files, sometimes the whole repository |
| Main risk | Silent behavior drift | Invented APIs and missed call sites |
| Typical approach | One scoped task with a test check | A tracked series of small steps |
What must not change
Before you start, state what is fixed. For a refactoring, the fixed part is the public contract: function signatures, return values, and side effects. The next example extracts the tax calculation and keeps format_invoice unchanged for callers.
# Before
def format_invoice(items):
subtotal = sum(i["price"] * i["qty"] for i in items)
tax = round(subtotal * 0.2, 2)
return {"subtotal": subtotal, "tax": tax, "total": subtotal + tax}
# After: same inputs and outputs, tax logic extracted
TAX_RATE = 0.2
def calculate_tax(subtotal):
return round(subtotal * TAX_RATE, 2)
def format_invoice(items):
subtotal = sum(i["price"] * i["qty"] for i in items)
tax = calculate_tax(subtotal)
return {"subtotal": subtotal, "tax": tax, "total": subtotal + tax}The structure changed: there is a new function and a named constant. For any input, format_invoice returns exactly what it returned before. That is the test of a refactoring.
For a migration, the fixed part is the behavior users depend on, such as response formats, error codes, and data stored on disk. Everything else may change, including imports, configuration, and test helpers.
Why the difference matters for an agent
An agent treats your request literally. If you say "clean up this module", it may rename public functions, change defaults, or fix bugs it notices. Each of those alters behavior. You get a safer result when you name the category of change and its limits.
- For a refactoring, say that behavior and public signatures must stay the same. Ask for a before-and-after test run.
- For a migration, say which technology you are moving to and which behavior must be preserved. Expect to split the work into steps.
Mixing the two is a common mistake. If an agent upgrades a library and also restructures code in the same change, a failing test could come from either one. Do them in separate commits, and preferably separate branches.
Choosing an approach
Pick the approach from the size and risk of the change.
- Small refactoring: give one scoped task, ask for a plan, and run the tests before and after.
- Refactoring of poorly tested code: write tests that record current behavior first, then refactor.
- Migration: break it into steps, commit after each verified step, and use deterministic tools for repetitive edits where you can.
Later chapters cover each of these in detail.
Exercise
You ask an agent to extract a helper function from a module. After the change, the tests pass, but the agent also changed the default value of an optional argument on a public function. Is this still a refactoring? Why does it matter?
Show answer
No. Changing a default value alters observable behavior for callers who omit the argument, so the change is no longer behavior-preserving. Passing tests do not prove otherwise if no test covers that default. Reject that edit, or move it into a separate, explicitly requested change.
Exercise
Name one thing that should stay fixed and one thing that may change when you migrate from one HTTP client library to another.
Show answer
Fixed: user-facing behavior, such as the request format sent to the server and how callers see errors. Allowed to change: imports, the library calls, configuration, and test helpers that mock the old library.