Refactoring
Restructure code without changing its external behavior.
When to Use
Use this skill when:
- Improving code structure
- Reducing complexity
- Making code more maintainable
- Removing duplication
Workflow
Step 1: Ensure Tests Exist
npm run test
If no tests exist, write them first using TDD.
Step 2: Identify Refactoring Opportunities
- Duplicate code
- Long functions (>30 lines)
- Complex conditionals
- Unclear naming
- Large classes
Step 3: Make Small Changes
- Extract duplicate code to functions
- Split long functions
- Simplify conditionals
- Improve naming
- Reduce complexity
Step 4: Run Tests After Each Change
npm run test
Keep tests green throughout.
Anti-Patterns
- rocket_launch Refactoring without tests
- rocket_launch Making big changes at once
- rocket_launch Changing behavior while refactoring
- rocket_launch Not running tests after changes
Verification
npm run test # All tests pass
npm run typecheck # No new type errors
Related
- TDD - Test-first development
- Error Handling - Add error handling