Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 2 min read

Refactor or Rewrite? How to Decide for One Module

Decide module by module, from facts. Check what the module does, who depends on it, how well it's tested, how often it breaks and which rules it breaks. Then you can leave it, clean it up, refactor it with care or rewrit

Decide module by module, from facts. Check what the module does, who depends on it, how well it's tested, how often it breaks and which rules it breaks. Then you can leave it, clean it up, refactor it with care or rewrite it behind tests, and you'll know why. The free AI starter kit we build our own products with gathers those facts and puts them on one screen.

Gather the facts

  • Purpose, what it does in two sentences, from its code and the code that calls it.
  • Shape, its size, what it exposes, what it imports and what imports it.
  • Tests, which ones cover it, whether they test behavior, and roughly how much of it they cover.
  • History, how often it changed in the last 90 days, how many of those changes were bug fixes, and how many people touch it.
  • Rules, any architecture rule it breaks, and whether it sits in a danger zone like auth or payments.
  • Smells, like import cycles, dead exports, copied logic, files over 500 lines and deeply nested functions.

An AI can pull all of this from your repo and git history in minutes.

Make the call

What the facts say Call
Stable, tested, rarely touched Leave it alone
Works, but with dead code and copied logic Clean it up in small commits
Changes often, breaks often, lots of importers Refactor with care, behind tests
Breaks the rules everywhere and tests prove its behavior Rewrite this one module
Risky and barely tested Add tests first, then decide again

Add tests before you touch anything risky

If the module is risky and the tests are thin, write tests for what it does today first, each one run and passing. Then every refactor or rewrite has to keep them passing, so you find out the moment it changes behavior.

Rewrite one module, never the whole app

A full rewrite throws away the bug fixes hiding in the old code. Rewriting one module behind tests keeps the rest of the app working while you replace the worst part.

Get the module check we use

The starter kit checks one module on one screen with a clear call, adds tests to old code before anything risky changes, and shows what a change will break before it's made. Get the starter kit free on GitHub, or work through your codebase with us.

Questions

When should you rewrite code instead of refactoring it?

Rarely, and only one module at a time. Rewrite when the module breaks its rules everywhere, keeps producing bugs and has tests that prove what it must keep doing. Without those tests, add them first.

How do I know if a module is risky to change?

Count how many files import it, how often it changed in the last 90 days, how many of those changes were bug fixes and how much of it the tests cover.

Can AI help decide whether to refactor?

Yes. It can gather the facts in minutes, like importers, churn, bug-fix commits and test coverage, and put them on one screen so you make the call.

Originally published at wednesday.is.

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.