intermediate

Refactoring

Improve structure without changing behavior by using small verified steps, characterization tests, and reversible commits.

Refactoring changes structure without changing external behavior—small steps, green tests, reversible commits. Use characterization tests when legacy code lacks coverage. Common moves: extract function, rename, move module, simplify conditional, replace magic with named constants.

Never mix behavior features and structural cleanup in one opaque PR.

On interviews: outline a safe refactor sequence for untested legacy code.

Common pitfalls: big-bang rewrites, refactoring without automated checks, and "cleanup" that silently fixes bugs.

The trade-off is incremental safety versus dramatic rewrite temptation.

Checklist:

  • One refactoring step at a time.
  • Tests or characterization before moves.
  • Separate refactor PRs from feature PRs.