Applicability Rubric
| Condition | Pass | Fail |
|---|---|---|
| Existing code modification | Need to change existing code | Writing new code only |
| Code comprehension issues | Code hard to understand/extend | Code is clear |
| Feature preparation | Preparing for new functionality | Direct implementation possible |
| Incremental improvement | Improving quality step by step | No improvement needed |
Apply when: Any condition passes
Core Principles
The Refactoring Cycle
code
1. Ensure tests exist (or add them) ↓ 2. Make small change ↓ 3. Run tests ↓ 4. Commit if green ↓ 5. Repeat
Golden Rules
- •Never refactor and change behavior simultaneously
- •Always have tests before refactoring
- •Small steps, frequent commits
- •If tests fail, revert immediately
Common Refactoring Techniques
Code Organization
| Technique | When to Use | Before → After |
|---|---|---|
| Extract Method | Long method, repeated code | Inline code → Named method |
| Extract Class | Class has multiple responsibilities | One class → Two classes |
| Move Method | Method uses another class more | A.method() → B.method() |
| Rename | Name doesn't reveal intent | d → elapsedDays |
Simplification
| Technique | When to Use | Before → After |
|---|---|---|
| Replace Conditional with Polymorphism | Type-based switching | if/switch → Subclasses |
| Replace Magic Number | Unexplained literals | 86400 → SECONDS_PER_DAY |
| Remove Dead Code | Unused code | Code → Nothing |
| Simplify Conditional | Complex boolean logic | Nested ifs → Guard clauses |
Dealing with Dependencies
| Technique | When to Use | Before → After |
|---|---|---|
| Extract Interface | Need to mock or swap | Concrete → Interface + Concrete |
| Inject Dependency | Hard-coded dependency | new Dep() → Constructor param |
| Replace Inheritance with Delegation | Inheritance misused | extends → has-a |
Safe Refactoring Steps
Extract Method
- •Identify code to extract
- •Create new method with descriptive name
- •Copy code to new method
- •Replace original code with method call
- •Run tests
- •Commit
Rename
- •Find all usages
- •Rename (use IDE refactoring if available)
- •Update documentation/comments
- •Run tests
- •Commit
Move Method
- •Copy method to target class
- •Adjust for new context
- •Update original to delegate
- •Run tests
- •Remove original method
- •Run tests
- •Commit
Completion Rubric
Before Refactoring
| Criterion | Pass | Fail |
|---|---|---|
| Test coverage | Tests exist and pass | No tests or failing tests |
| Behavior understanding | Current behavior understood | Unclear behavior |
| Clear goal | Refactoring goal defined | No clear objective |
| Team awareness | Team knows the scope | Undisclosed changes |
During Refactoring
| Criterion | Pass | Fail |
|---|---|---|
| Single focus | One refactoring at a time | Multiple simultaneous changes |
| Test validation | Tests run after each change | No test verification |
| Incremental commits | Commit after each step | Large uncommitted changes |
| Behavior preservation | No behavior changes | Behavior modified |
After Refactoring
| Criterion | Pass | Fail |
|---|---|---|
| Tests passing | All tests still pass | Tests failing |
| Code clarity | Code is cleaner/clearer | Same or worse clarity |
| No new features | No functionality added | Features added |
| Review completed | Changes reviewed | No review |
Code Smells to Watch For
| Smell | Indication | Refactoring |
|---|---|---|
| Long Method | Method > 20 lines | Extract Method |
| Large Class | Class > 200 lines | Extract Class |
| Long Parameter List | > 3 parameters | Introduce Parameter Object |
| Duplicated Code | Same code in multiple places | Extract Method/Class |
| Feature Envy | Method uses other class's data | Move Method |
| Data Clumps | Same data groups appear together | Extract Class |