3.4 KiB
3.4 KiB
name, description
| name | description |
|---|---|
| refactoring-patterns | Common refactoring patterns and techniques for improving code quality, reducing complexity, and enhancing maintainability. Use when planning or executing code refactoring. |
Refactoring Patterns
This skill provides proven patterns for safe, effective code refactoring.
Core Refactoring Principles
Design Principles
- SOLID: Single responsibility, Open-closed, Liskov substitution, Interface segregation, Dependency inversion
- KISS (Keep It Simple, Stupid): Favor simplicity over complexity
- YAGNI (You Aren't Gonna Need It): Don't add functionality until necessary
- DRY (Don't Repeat Yourself): Avoid code duplication
Refactoring Guidelines
- Red-Green-Refactor: Ensure tests pass before and after refactoring
- Small Steps: Make incremental changes, commit frequently
- One Thing at a Time: Refactor OR add features, not both
- Maintain Behavior: External behavior stays the same
- Avoid Premature Optimization: Focus on clarity first, optimize when needed based on metrics
Refactoring Approaches
Architectural Level
- Restructure system components and module boundaries
- Refactor service boundaries in microservices/modular monoliths
- Redesign database schema and data access patterns
- Improve system scalability and performance architecture
Module/Package Level
- Reorganize package structure for better cohesion
- Extract shared libraries or utilities
- Improve dependency management and reduce coupling
- Refactor cross-cutting concerns
Class/Component Level
- Apply SOLID principles to class design
- Extract interfaces and abstractions
- Reduce class complexity and responsibilities
- Improve encapsulation and information hiding
Method/Function Level
- Simplify complex logic
- Extract reusable functions
- Improve naming and readability
- Reduce parameter lists
Refactoring Strategy
1. Understand the Code
Before refactoring:
- Read and understand the existing code
- Identify all test coverage
- Document current behavior
- Note any dependencies
2. Ensure Test Coverage
- Add tests if missing to cover refactored parts
- Verify all tests pass
- Focus on testing the specific code being refactored (no specific coverage percentage target)
3. Plan Refactoring
Create a markdown file documenting:
- What needs to be refactored and why
- Sequence of small, incremental steps
- Checklist format with tasks that can be marked as done (- [x]) before moving to next task
- Dependencies and potential risks
4. Execute Incrementally
- Make one change at a time
- Run tests after each change
- Commit working code frequently
- Roll back if tests fail
5. Verify and Clean Up
- All tests pass
- Code is clearer and simpler
- Update documentation
Safe Refactoring Checklist
- Tests exist and pass before starting
- Each refactoring step is small and focused
- Tests pass after each step
- Behavior remains unchanged
- Code is more maintainable
- Team has reviewed changes
- Never remove existing tests - only add new tests for refactoring validation
Integration Points
Works with:
senior-engineeragent for refactoring executiontechnical-architecture-advisorfor large-scale refactoring/refactoring-plancommand for structured refactoringcode-revieweragent for validating improvements