joabgonzalez/ai-agents-skills/code-refactoring
Claude Code and agent skill by joabgonzalez/ai-agents-skills
- community
Checking that the SKILL.md file is still in the repository.
Systematic code refactoring with risk mitigation. Trigger: When refactoring legacy code, migrating technologies, or resolving technical debt.
Install
npx vskill@latest install joabgonzalez/ai-agents-skills/code-refactoringSKILL.md
The same SKILL.md was published earlier as joabgonzalez/ai-agents-skills/code-refactoring.
Code Refactoring
Refactoring legacy code, migrating technologies, and resolving technical debt with minimal risk and maximum impact.
When to Use
- Modernizing legacy code (JS→TS, callbacks→async/await)
- Migrating technologies (Redux ORM removal, framework changes)
- Resolving technical debt that blocks new features
- Improving code maintainability and reducing complexity
- Preparing code for performance optimization
Don't use for:
- Adding new features (separate refactoring from feature work)
- Code that works well and won't change
- Production-critical code without adequate test coverage
- Situations with severe time constraints or change freezes
Critical Patterns
✅ REQUIRED: Safety Snapshot Before Starting
Create safety net BEFORE touching any code.
✅ REQUIRED: Test Coverage Before Refactoring
Never refactor production code without test coverage.
✅ REQUIRED: Incremental Changes with Atomic Commits
Make small, verifiable changes. Commit at every stable state.
✅ REQUIRED: Feature Flags for Safe Migration
Use feature flags to enable runtime switching between old/new implementations.
✅ REQUIRED: Preserve Behavior During Refactoring
Refactoring changes HOW code works, not WHAT it does.
✅ REQUIRED: Automated Refactoring Tools (Efficiency)
Use automation for mechanical transformations. Save manual effort for logic improvements.
✅ REQUIRED: Dependency Snapshot for Rollback Safety
Lock dependencies before refactoring to prevent environment variables.
✅ REQUIRED: Gradual Type Introduction (TypeScript Migration)
For JS→TS migration, use // @ts-check for incremental type safety without file conversion.
✅ REQUIRED: ROI-Based Prioritization
Calculate effort vs impact before starting.
Quick Wins (Prioritize First):
- 1-2 weeks effort, 250-375% first-month ROI
- Example: Extract repeated validation logic into shared utility
Medium-Term:
- 1-3 months effort, 100-200% 3-month ROI
- Example: Technology migration (JS→TS)
Long-Term:
- 3-12 months effort, 50-100% 6-month ROI
- Example: Framework migration (monolith→microservices)
❌ NEVER: Big-Bang Replacement
Decision Tree
Example
JavaScript to TypeScript
See references/migration-patterns.md for the full Redux ORM removal, callbacks→async/await, and Redux Classic→RTK walkthroughs.
Edge Cases
- Partial migration state: Use feature flags to run old + new code in parallel. Monitor error rates for both implementations.
- Breaking changes unavoidable: Create deprecation warnings 2+ versions before removal. Document migration guide with before/after examples.
- Rollback during production incident: Feature flags enable instant rollback without code deploy. Monitor metrics: error rate, latency, throughput.
- Test coverage gaps: Don't refactor. Either add tests first (separate initiative) or accept tech debt with monitoring.
- Circular dependencies during refactor: Indicates poor separation of concerns. Introduce dependency injection or event-driven architecture.
- Performance regression: Benchmark before/after with realistic data. If regression >10%, investigate optimization or revert.
- Merge conflicts during long-running refactor: Rebase frequently (daily) to stay in sync with main branch. Use
git rerereto remember conflict resolutions.
- Flaky tests exposed during refactor: Don't fix during refactor. Document flaky tests in separate issue. Refactor assumes stable test suite.
- Refactoring reveals bugs in original code: Stop refactoring. Fix bugs first (separate commit/PR), THEN resume refactor. Never mix bug fixes with refactoring.
- Team members need original code during refactor: Use feature branch + feature flag. Original code remains accessible until migration complete.
Checklist
Summary of the 4 phases — see references/compliance-checklist.md for the full checklist with all gates.
- [ ] Gate 1 — Approval to Start: Test coverage ≥80%, baseline captured, ROI calculated, rollback plan documented, team approved
- [ ] Phase 1 — Baseline Capture: Run full test suite, record API responses, capture performance metrics, export DB state
- [ ] Phase 2 — Refactoring: Atomic commits (<300 lines), tests pass after each commit, no performance regression, type/lint clean
- [ ] Gate 2 — Mid-Refactoring Review: At 50% completion, verify complexity unchanged, timeline on track, tests green
- [ ] Phase 3 — Compliance Audit: Full test suite, integration tests, E2E tests, API contract tests, load testing, staging smoke test
- [ ] Gate 3 — Pre-Merge Validation: 2+ code review approvals, static analysis passes, documentation updated, deployment plan reviewed
- [ ] Phase 4 — Production Deployment: Feature flags configured, rollback tested, on-call notified, monitoring dashboards live
- [ ] Gate 4 — Post-Deployment: Error rate ≤ baseline (24h), latency ≤ baseline (24h), zero user complaints, business metrics unchanged
Resources
Reference Files
- migration-patterns.md - JS→TS, Redux ORM removal, callbacks→async/await, Redux Classic→RTK walkthroughs
- legacy-testing.md - Characterization tests, golden master testing, coverage tools for legacy code
- advanced-techniques.md - Git worktrees, jscodeshift codemods, refactoring metrics dashboard
- deployment-safety.md - Risk assessment, canary deployments, automated rollback, pair programming
- compliance-checklist.md - Full 4-phase checklist with all 4 compliance gates and failure responses
Related Skills
- code-conventions - Code organization and naming
- architecture-patterns - SOLID, DDD, Clean Architecture
- typescript - Type-safe refactoring
- systematic-debugging - Root cause analysis
- unit-testing - Test coverage strategies
External Resources
- <https://refactoring.guru/> - Refactoring patterns catalog
- <https://martinfowler.com/refactoring/> - Refactoring best practices
- <https://www.industriallogic.com/xp/refactoring/> - Incremental refactoring techniques
Security
- Security scanPassed, 98 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/code-refactoring/SKILL.md
Popularity
- Repository stars
- 9
- 7-day trend score
- 12
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/joabgonzalez/ai-agents-skills/code-refactoring)