Agent skill
refactor-code
Safely improve code quality through test-driven refactoring with automatic validation and rollback. Use during quality review to reduce technical debt while preserving behavior.
Install this agent skill to your Project
npx add-skill https://github.com/nikhillinit/Updog_restore/tree/main/.claude/skills/refactor-code
SKILL.md
Refactor Code Skill
Purpose
Safely improve code quality through automated refactoring while maintaining behavioral correctness. This skill identifies refactoring opportunities from quality findings and applies them incrementally with continuous test validation.
Core Capabilities:
- Test-driven refactoring (run tests after each change)
- Automatic rollback on test failures
- Risk-based prioritization (P0-P3, Low/Medium/High risk)
- Incremental application (one refactoring at a time)
- Full traceability (log all changes with rationale)
- Quality metrics tracking (before/after comparison)
Prerequisites
- Tests exist and are currently passing
- Code to refactor has been identified
Workflow
Step 1: Validate Prerequisites
- Verify tests exist and are passing:
npm test - Capture baseline metrics (test count, pass rate)
- Create git checkpoint:
git stashor ensure clean working tree
Halt if:
- No tests exist (can't validate safety)
- Tests are currently failing
Step 2: Analyze Code for Refactoring Opportunities
Scan implementation files and identify patterns:
- Extract Method: Long methods (>50 lines)
- Extract Variable: Complex expressions
- Rename: Unclear variable/function names
- Remove Duplication: Repeated code blocks (2+ occurrences)
- Simplify Conditionals: Nested if/else chains
- Extract Class: Classes with too many responsibilities
- Inline: Unnecessary indirection
Prioritize refactorings:
- P0 (Critical): Addresses critical/high severity quality issues
- P1 (High): Reduces technical debt
- P2 (Medium): Improves maintainability
- P3 (Low): Nice-to-have improvements
Estimate risk for each:
- Low Risk: Rename, extract variable, inline
- Medium Risk: Extract method, simplify conditionals
- High Risk: Extract class, move method, large-scale changes
Filter by aggressiveness level:
- Conservative: P0 only, low-risk refactorings
- Moderate: P0 + P1, low-to-medium risk refactorings
- Aggressive: P0 + P1 + P2, all risk levels
Step 3: Apply Refactorings Incrementally
For each selected refactoring (in priority order):
- Announce what's being refactored (file, type, risk)
- Apply single refactoring
- Run tests immediately (
npm test) - Evaluate results:
- If pass: Log success, keep changes, proceed
- If fail: Rollback via git, log failure, skip to next
- Update progress count (applied/skipped/failed)
Safety rules:
- Never apply multiple refactorings simultaneously
- Never skip test validation
- Never continue if critical (P0) refactoring fails
- Never modify tests to make them pass
- Always preserve ability to rollback until tests pass
Step 4: Final Validation and Summary
- Run full test suite:
npm test - Compare before/after metrics
- Generate summary of changes
Halt if:
- Final test suite fails (regression)
- Coverage drops significantly
Completion Criteria
- Prerequisites validated (tests passing)
- Refactoring opportunities identified and prioritized
- Selected refactorings applied incrementally
- All tests passing after refactoring
- Quality metrics improved or maintained
- Changes committed separately from features
Safety Guarantees
- Behavioral preservation: No functionality changes
- Test validation: After each change
- Automatic rollback: On failures
- Incremental: One refactoring at a time
- User control: Choose scope (conservative/moderate/aggressive)
When to Use
- Quality review identifies refactoring opportunities
- Technical debt needs systematic reduction
- Code quality metrics below targets
- After a feature is complete and tests pass
When NOT to Use
- Feature additions (use implementation skills)
- Bug fixes (use bugfix skill)
- Projects without tests (can't validate safety)
- Breaking changes (requires manual review)
Best Practices
- Start Conservative - First session: use conservative mode
- Commit Before Refactoring - Safety net for rollback
- Commit Separately - Message:
refactor: {description} - Review Changes - Always review via git diff
Based on BMAD Enhanced Quality Suite refactor-code skill
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
financial-calc-correctness
statistical-testing
ui-ux-pro-max
UI/UX design intelligence. 50 styles, 21 palettes, 50 font pairings, 20 charts, 9 stacks (React, Next.js, Vue, Svelte, SwiftUI, React Native, Flutter, Tailwind, shadcn/ui). Actions: plan, build, create, design, implement, review, fix, improve, optimize, enhance, refactor, check UI/UX code. Projects: website, landing page, dashboard, admin panel, e-commerce, SaaS, portfolio, blog, mobile app, .html, .tsx, .vue, .svelte. Elements: button, modal, navbar, sidebar, card, table, form, chart. Styles: glassmorphism, claymorphism, minimalism, brutalism, neumorphism, bento grid, dark mode, responsive, skeuomorphism, flat design. Topics: color palette, accessibility, animation, layout, typography, font pairing, spacing, hover, shadow, gradient. Integrations: shadcn/ui MCP for component search and examples.
code-reviewer
Comprehensive multi-AI code review system for Python, JavaScript, and TypeScript. Use when reviewing code for quality, security, performance, or best practices. Includes automated analysis scripts, language-specific patterns, and AI collaboration workflows for complex decisions.
dependency-guardian
Automated dependency management with security scanning, update orchestration, and compatibility validation
documentation-sync
Detects code/documentation drift, validates examples, generates diagrams, auto-updates documentation
Didn't find tool you were looking for?