Intelligently Refactor and Improve Code Quality
Refactor code systematically: analyze, test, refactor incrementally, verify, and document. Preserve external behavior while improving internal structure.
When to Use
- User requests refactoring or code improvements
- Code needs quality improvements (readability, maintainability, performance)
- Applying design patterns or reducing complexity
- Before major changes to ensure a solid foundation
Core Workflow
- Pre-refactoring: Identify what to refactor and why; understand current behavior; review tests and docs; map dependencies.
- Test coverage: Ensure tests exist; write missing tests before refactoring; run tests to establish baseline.
- Strategy: Define goals (performance, readability, maintainability); choose techniques (extract, rename, move, eliminate dead code, etc.); plan incremental steps.
- Environment: Create branch (
git checkout -b refactor/<name>); ensure tests pass; set up tooling if needed. - Incremental changes: Small, focused changes; run tests after each; commit frequently with clear messages; use IDE refactoring tools when available.
- Quality: Improve naming; eliminate duplication (DRY); simplify conditionals; reduce length/complexity; improve separation of concerns.
- Performance: Identify bottlenecks; optimize algorithms/data structures; reduce unnecessary work; improve memory usage.
- Patterns: Apply design patterns where beneficial; improve abstraction/encapsulation; enhance modularity/reusability; reduce coupling.
- Error handling: Standardize approaches; improve messages/logging; add exception handling; enhance resilience.
- Documentation: Update comments; revise API docs if interfaces changed; ensure accuracy.
- Testing: Add tests for new paths; improve coverage/quality; remove obsolete tests.
- Static analysis: Run linters; use static analysis tools; check security; verify complexity metrics.
- Performance verification: Run benchmarks; compare before/after; ensure no degradation.
- Integration: Run full test suite; test dependent systems; verify functionality and edge cases.
- Review prep: Review changes; ensure goals met; prepare explanation and rationale.
- Documentation: Summarize changes; document breaking changes or new patterns; update project docs.
- Deployment: Plan strategy; consider feature flags; prepare rollback; set up monitoring.
Key Principles
- Preserve behavior: External behavior must remain unchanged; tests verify this.
- Test-first: Write or verify tests before refactoring; use tests as a safety net.
- Incremental: Small, focused changes; test after each; commit working state frequently.
- Safety over speed: Prioritize correctness; maintain test coverage throughout.
- Document: Explain changes, rationale, and benefits for future reference.
Refactoring Techniques
- Extract Method/Function, Extract Class/Component
- Rename Variable/Method, Move Method/Field
- Replace Conditional with Polymorphism
- Eliminate Dead Code
- Apply design patterns where beneficial
Reference
- reference.md: Full 17-step checklist with detailed instructions. Read when executing a full refactoring or validating the process.
