"TDD-based code simplification that preserves behavior through tests. Use Red-Green-Refactor cycles to simplify code one test-verified change at a time. **DISTINCT FROM**: General code review or AI rewriting—this skill requires existing tests and only proceeds when tests confirm behavior is preserved. **PROACTIVE**: Auto-invoke when test-covered code has complexity (functions >50 lines, high cyclomatic complexity, duplication) and user wants to simplify it safely. Trigger phrases: 'clean up code', 'make code simpler', 'reduce complexity', 'refactoring help'. **NOT FOR**: Adding features or fixing bugs—use /tdd skill instead."
Resources
1Install
npx skillscat add mguinada/agent-skills/refactor Install via the SkillsCat registry.
Refactor Skill
Overview
Language Agnostic: Examples use Python; port to your project's language.
Core Principles:
- Functionality preserved through tests
- Small, incremental iterations
- All checks must pass before completion
Prerequisites
Before starting refactoring:
- Tests must exist: If no tests exist for the code, request them first
- Tests must pass: Verify
uv run pytestpasses before starting - Understand the code: Read and understand what the code does
- Create a backup: Optionally commit current state before changes
Refactoring Process
Phase 1: Analysis
- Read the target code thoroughly to understand its purpose
- Identify code smells - see code-smells.md for detection patterns
- List refactoring opportunities (wait for user approval before implementing)
Phase 2: TDD Cycle for Each Change
For each discrete refactoring iteration:
🔴 RED (if applicable)
- If adding new simplified behavior, write a failing test first
- If only simplifying existing code, skip to GREEN phase
- Run tests to confirm the new test fails
🟢 GREEN
- Make minimal changes to pass the tests
- Focus on making tests pass, not perfection
- Run
uv run pytestafter each small change - Iterate until tests are green
🔵 REFACTOR
- Apply simplification while keeping tests green
- Extract functions, improve naming, reduce complexity
- Continuously run tests after each small change
- Never batch multiple changes - one small step at a time
✅ VERIFY
- Run
uv run pytestto ensure all tests pass - Run
uv run ruff check src/for lint checks - Run
uv run mypy src/for type checks - If any check fails, fix and repeat verification
Phase 3: Final Verification
After all refactoring iterations complete:
# Run full CI pipeline until everything passes
bin/ci-localThis runs:
- Lint checks (
ruff) - Static type checks (
mypy) - Tests with coverage (
pytest)
Repeat until all checks pass with no errors.
Refactoring Patterns
For common refactoring patterns with before/after examples, see patterns.md.
Includes: Prompt refactoring patterns for code that contains prompts or prompt templates.
Examples
Inline: Extract Function
Before - complex function with embedded calculation:
def generate_report(users, threshold):
result = []
for user in users:
score = user.login_count * 0.3 + user.posts * 0.7
if score >= threshold:
result.append({"name": user.name, "score": score})
return resultAfter - extracted calculation improves readability and testability:
def calculate_engagement_score(user) -> float:
return user.login_count * 0.3 + user.posts * 0.7
def generate_report(users, threshold):
result = []
for user in users:
score = calculate_engagement_score(user)
if score >= threshold:
result.append({"name": user.name, "score": score})
return resultSingle Iteration Pattern:
- 🔴 Write test for simplified behavior (if adding new behavior)
- 🟢 Make minimal changes to pass tests
- 🔵 Simplify while tests stay green
- ✅ Run
bin/ci-localto verify all checks pass
Repeat for each discrete improvement.