refactoring-guide
Refactoring Guide
Overview
Refactoring is changing the internal structure of code without changing its observable behavior. The goal is readability, maintainability, and testability — never new features and never bug fixes in the same step.
Keywords: refactor, clean up, simplify, extract function, extract method, extract variable, inline, rename, remove duplication, reduce nesting, decompose conditional, guard clause, god class, long function, code smell, DRY, separation of concerns, make testable, reduce cyclomatic complexity.
The two non-negotiable rules:
- Behavior must not change. No new functionality, no bug fixes, no API changes in a refactoring commit. If you spot a bug, note it and fix it in a separate commit.
- Tests stay green at every step. Refactor in small steps; run tests after each. If there are no tests, write characterization tests first (see
references/characterization-tests.md).
When the task mixes refactoring with feature work, separate them: "make the change easy (this may be hard), then make the easy change." Refactor first, commit, then build the feature.
Workflow
Follow this loop. Do not batch many changes before testing.