Refactoring Legacy Code with AI Help
Change old, untested code safely by pinning behaviour first and refactoring in small, reversible steps
A taste of a lesson
The assistant refactored our old pricing function and also fixed what it called a rounding bug. Tests pass. Should I merge?
Not as one change. A rounding 'bug' in pricing may be exactly what invoices, reports or partner systems expect. First, split it: a refactoring commit with no behaviour change, verified by characterisation tests, and a separate commit for the rounding change. Then treat the rounding change as a business decision: show examples of old and new results to whoever owns pricing. Do your current tests actually cover the rounding cases, or did they pass because nobody tested them?
Written by the teacher as an example. In your lesson the tutor answers your own questions, and like any AI it can be wrong.
What you will be able to do
- Use an assistant to map unfamiliar code while verifying its summaries
- Write characterisation tests that pin current behaviour before changes
- Find seams and refactor in small, reversible, separately committed steps
- Keep refactoring separate from behaviour changes for clear review
- Plan incremental replacement with strangler patterns, flags and parallel runs
Lesson plan
- 1 Understanding before changing Map what legacy code does, using the assistant as a reader you double check. Start
- 2 Characterisation tests Pin current behaviour so you can tell when a change alters it. Start
- 3 Finding seams Locate places where behaviour can be isolated or replaced safely. Start
- 4 Small refactoring steps Improve structure through tiny, tested, reversible changes. Start
- 5 Separating refactors from behaviour changes Keep review simple by never mixing structure and behaviour changes. Start
- 6 Incremental replacement Replace risky modules gradually instead of in one big rewrite. Start
Try asking
About this tutor
For developers maintaining large, old codebases with little documentation and few tests. Assistants are useful for legacy work: explaining unfamiliar code, finding usages, suggesting seams and drafting characterisation tests. They are also risky: a confident rewrite of a function nobody fully understands can break behaviour that customers rely on. You will learn to build understanding first, pin current behaviour with characterisation tests, find seams, refactor in small reversible steps, separate refactoring from behaviour changes, and plan incremental replacement of risky modules.
Reviews
Students can review a tutor after a paid lesson. Nobody has yet.
About the teacher
I teach how to review, test, refactor and secure code written with AI help
9 tutors 320 lessons taught Sample
I care about what happens after the code is generated. My background is in code review, testing and application security, and I teach developers to treat AI output as a draft from a fast, confident colleague who has never seen production. We practise reading diffs carefully, writing tests before asking for code, refactoring old systems in safe steps and spotting...
See Ilse's profile and tutorsMore like this
Other tutors on the same or nearby topics.