BoilerPrompt

AI refactoring

AI refactoring is using an AI tool to restructure existing code without changing its behaviour — extracting functions, renaming concepts across a codebase, splitting large modules, or migrating between patterns. It is where agentic tools show their advantage most clearly.

Refactoring is well suited to agents because success is checkable. The tests that passed before must pass after. That gives the agent loop a ground-truth signal, which is exactly what it lacks on more open-ended work.

The risk is scope creep. Asked to extract a function, an agent may also rename variables it finds unclear and reorganise imports, producing a diff where the intended change is buried in noise.

What this means in practice

State the boundary explicitly — "behaviour must not change, do not rename anything else, do not reformat" — and require the test suite to pass. Refactor on a clean working tree so the diff shows exactly what happened.

A refactor prompt with the boundary stated

Refactor: [what and where — e.g. extract the retry logic in
src/api/client.ts into a reusable helper]

Boundaries:
- Behaviour must not change. The existing tests define behaviour;
  they must pass unmodified.
- Do not rename anything outside the files named above.
- Do not reformat untouched code; keep the diff minimal.

Verify: run the test suite and the type checker, then show me the
diff per file.

Scope creep is the failure mode — the two "do not" lines are what keep the intended change visible in the diff.

Prompts for the tools this applies to

Related terms

  • AI code review

    AI code review is using a model to examine a diff or a file for bugs, security problems, and quality issues before a human reviews it.

  • Test-driven development with AI

    Test-driven development with AI means writing or generating the tests first, then letting an agent implement until they pass.

  • Agentic coding

    Agentic coding is using an AI tool that plans and executes a whole task in your codebase rather than completing one line at a time.

All glossary terms