Refactoring
Golden Rule
Never refactor and add features at the same time. Two separate changes, two separate commits/PRs.
Process
- Ensure tests exist before touching code. Add characterization tests if needed
- Identify the smell — long function, duplicate code, god class, shotgun surgery, primitive obsession
- Apply one refactoring at a time — extract, rename, move, inline
- Run tests after each step — if tests break, revert the last change
- Commit frequently — each atomic refactoring is one commit
Common Refactorings
- Extract Method — turn a code block into a named function
- Rename — give variables, functions, and classes meaningful names
- Replace Conditionals with Polymorphism — if/else chains to strategy pattern
- Introduce Parameter Object — groups of related params into a single object
- Decompose Conditional — complex if/else into separate functions
- Replace Magic Number with Constant
- Separate Query from Modifier
Signs You're Done
- Same behavior, same output
- Fewer lines of code
- Better names
- Clearer structure
- Tests still pass