Refactoring Legacy Code with AI: Guide
Matteo Migliore

Matteo Migliore is an entrepreneur and software architect with over 27 years of experience developing .NET-based solutions and evolving enterprise-grade application architectures.

He has led enterprise projects, trained hundreds of developers, and helped companies of all sizes simplify complexity by turning software into profit for their business.

A logistics company in northern Italy runs an 80,000-line C# management system, written over ten years by six people, with no tests. One developer says AI will fix it in a month. Another says it will break it in a week. Both are right: it depends on how AI is used.

Refactoring legacy code with AI, done with a method, cuts the work by 30-35% compared with doing it by hand. Done without one, it creates a new problem that is harder to find, because the generated code looks tidy and convincing. For a reference system of about 80,000 lines and no tests, it costs 27,000-39,000 euro with AI against 42,000-57,000 euro by hand, at 600 euro a day.

This is the short English version. The full guide (in Italian) covers the characterization test code, the six-step method, a before and after example, tooling, confidentiality of source code and the mistakes I see repeated. The choice between refactoring and rewriting is in rewrite or refactor legacy software.

Where does AI really speed up legacy refactoring?

On five long, boring and verifiable tasks: understanding unfamiliar code, documenting it, writing starter tests, mechanical transformations and proposing structure. It saves nothing on human review.

Bar chart of estimated time saved with AI by activity: documentation 60%, mechanical changes 50%, understanding code 40%, writing tests 35%, QA 10%, review 0%

The wider picture of how to keep a system running while it is modernized is in the strangler fig pattern.

Where does AI go wrong on legacy code?

Where behaviour depends on unwritten rules: rounding, order of operations, date formats, concurrency and side effects. The new code is cleaner, and different.

A typical case: .NET rounds half to even by default, and an assistant "fixing" it to round away from zero changes cents on invoices. Only characterization tests, which record what the code does today and compare after every change, catch it. For the cost of keeping old systems alive, see the real cost of legacy software.

How do you decide in five lines?

If the code changes often, you can write tests on the important parts and someone can judge the result, go step by step with AI. If any of the three is missing, start there.

  1. Measure how often the code changes and how many people really know it.
  2. Check that the source can be shared with an external service, or prepare a controlled environment.
  3. Write characterization tests on the thirty most important functions, from real data.
  4. Refactor one piece at a time, with small requests and a person reviewing each diff.
  5. Budget 45-65 person-days per 80,000 lines and measure change time and production errors at the end.

With those five numbers I can tell you in half an hour which parts to touch and what it costs. The method is described on legacy software modernization.

Does your company need custom software or AI in the team?

We design and build custom software, modernize legacy systems and train your developers to work with AI. Tell us the problem: you get a concrete answer within one working day.

Frequently asked questions

Yes, on one condition: write characterization tests first, recording today's behaviour. AI speeds up understanding, documentation, tests and mechanical changes, but it does not know unwritten business rules, so every change goes through tests and human review.

A C# system of about 80,000 lines with no tests costs 27,000-39,000 euro with AI used properly, against 42,000-57,000 euro by hand, at a reference rate of 600 euro a day. The saving is 30-35% and does not come from review.

It silently changes rounding, order of operations, date formats and side effects. The new code is cleaner but different, compiles, and passes quick checks; only characterization tests catch it.

Leave your details in the form below

Matteo Migliore

Matteo Migliore is an entrepreneur and software architect with over 27 years of experience developing .NET-based solutions and evolving enterprise-grade application architectures.

Throughout his career, he has worked with organizations such as Cotonella, Il Sole 24 Ore, FIAT and NATO, leading teams in developing scalable platforms and modernizing complex legacy ecosystems.

He has trained hundreds of developers and supported companies of all sizes in turning software into a competitive advantage, reducing technical debt and achieving measurable business results.

Stai leggendo perché vuoi smettere di rattoppare software fragile.Scopri il metodo per progettare sistemi che reggono nel tempo.