Agentic Refactoring

Test-Driven Tech-Debt Cleanup

11 / 12

In Practice

  • Start with a net: generate a behavior spec from existing code, then characterization tests
  • Get a green baseline before you move a line; tests must pass against current code first
  • Mark each behavior: must-preserve, intentionally-changing, or actually-a-bug
  • Move in small, reviewable steps: isolate I/O, split validation from side effects, retire one branch of conditionals at a time; never “rewrite this module”
  • Keep the suite green at every chunk; never carry red into the next step
  • Preserve external contracts: APIs, schemas, logs, outputs, and screenshots
  • Do not patch a test just to make the refactor pass

The first question you should always ask on any new codebase is: do we have behavioural-focused tests that give us confidence that we can change the internals and the actual experience for a user or client is maintained? That's number one. That is how we refactor.

— Ben SE, CTO, CodeWalnut

Page 54 of 72: Agentic Refactoring: In Practice

Contents