refactor
Refactor Skill
Collaborating skills
- TDD: skill:
tddfor guiding through Red-Green-Refactor cycles and ensuring tests are in place before refactoring - Design Patterns: skill:
design-pattern-adopterfor guidance on applying design patterns effectively
Quick Reference
Code Smells Reference
| Smell | Detection | Refactoring |
|---|---|---|
| Long function | >20 lines | Extract Method |
| Duplicated code | Similar logic | Extract Method, Pull Up |
| Long parameter list | >3 params | Introduce Parameter Object |
| Large class | >10 methods | Extract Class |
| Feature envy | Method uses other class | Move Method |
| Switch statements | Type checking | Replace with Polymorphism |
| Speculative generality | Unused abstractions | Inline, Collapse Hierarchy |
| Message chains | a.b.c.d | Hide Delegate |
| Middle man | Excessive delegation | Remove Middle Man |
| Incomplete library class | Missing methods | Introduce Foreign Method |
Process
1. VERIFY - Tests exist and pass
2. ANALYZE - Identify code smells (see references/code-smells.md)
3. SELECT - Choose appropriate pattern (see references/catalog.md)
4. APPLY - One small step at a time
5. VERIFY - Run tests after each change
6. REPEAT - Until clean
TDD Cycle
For each discrete refactoring:
🔴 RED - If adding behavior, write failing test first
🟢 GREEN - Make minimal changes to pass tests
🔵 REFACTOR - Apply pattern while keeping tests green
✅ VERIFY - Run tests, lint, type check
Prerequisites
- Tests must exist - If no tests, write them first
- Tests must pass - Run tests before starting
- Understand the code - Read and comprehend behavior
Verification Commands
Python:
pytest # Run tests
ruff check src/ # Lint
mypy src/ # Type check
Ruby:
rspec # Run tests
rubocop # Lint
steep check # Type check (if using Steep)
Refactoring Checklist
- [ ] Function does one thing only (SRP)
- [ ] Function name clearly describes intent
- [ ] Function is 20 lines or fewer
- [ ] 3 or fewer parameters
- [ ] No duplicate code (DRY)
- [ ] If nesting is 2 levels or fewer
- [ ] No magic numbers
- [ ] Self-documenting code
- [ ] Tests pass
- [ ] Lint passes
- [ ] Type check passes
Constraints
MUST:
- Write/run tests before refactoring
- Change one thing at a time
- Preserve behavior exactly
MUST NOT:
- Refactor + add features simultaneously
- Refactor without tests
- Batch multiple refactorings
References
- Code Smells Guide - Detection patterns and fixes for all code smells
- Refactoring Catalog - Complete catalog with Ruby/Python examples
- Advanced Patterns - SOLID refactorings, prompt patterns, functional patterns
External Resources
More from mguinada/agent-skills
ai-engineering
Build AI agents and agentic workflows. Use when designing/building/debugging agentic systems: choosing workflows vs agents, implementing prompt patterns (chaining/routing/parallelization/orchestrator-workers/evaluator-optimizer), building autonomous agents with tools, designing ACI/tool specs, or troubleshooting/optimizing implementations. **PROACTIVE ACTIVATION**: Auto-invoke when building agentic applications, designing workflows vs agents, or implementing agent patterns. **DETECTION**: Check for agent code (MCP servers, tool defs, .mcp.json configs), or user mentions of \"agent\", \"workflow\", \"agentic\", \"autonomous\". **USE CASES**: Designing agentic systems, choosing workflows vs agents, implementing prompt patterns, building agents with tools, designing ACI/tool specs, troubleshooting/optimizing agents.
13git-commit
Generate concise, descriptive git commit messages following best practices. Use when creating git commits from staged changes, crafting commit messages, or reviewing commit message quality. Use when the user says /commit or asks to create a git commit. **PROACTIVE ACTIVATION**: Auto-invoke when staged changes detected or user asks to commit/save work. **DETECTION**: Run git status - if staged changes exist, offer to commit. User says \"commit\", \"save\", \"done with feature\". **USE CASES**: Staged changes detected, work completed, user wants to save progress.
12tdd
Guide Test-Driven Development workflow (Red-Green-Refactor) for new features, bug fixes, and refactoring. Supports both Python (pytest) and Ruby (RSpec). Use when writing tests, implementing features, or following TDD methodology. **PROACTIVE ACTIVATION**: Auto-invoke when implementing features or fixing bugs in projects with test infrastructure. **DETECTION**: Check for tests/ directory, pytest.ini, pyproject.toml with pytest config, spec/ directory, .rspec file, or *_spec.rb files. **USE CASES**: Writing production code, fixing bugs, adding features, legacy code characterization.
11create-pr
Creates GitHub pull requests with properly formatted titles, a body matching the project's PR template, and appropriate type/scope labels. Automatically creates labels if they don't exist. Use when creating PRs, submitting changes for review, or when the user says /pr or asks to create a pull request. **PROACTIVE ACTIVATION**: Auto-invoke when a branch has commits ahead of main and the user signals the work is ready. **DETECTION**: Run git log origin/main..HEAD - if commits exist and user signals readiness, offer to open a PR. User says \"open a PR\", \"ready for review\", \"this is done\", \"let's merge\", \"submit this\". **USE CASES**: Feature or fix complete, user finished a series of commits and mentions review or merging.
11prompt-engineering
Creates system prompts, writes tool descriptions, and structures agent instructions for agentic systems. Use when the user asks to create, generate, or design prompts for AI agents, especially for tool-using agents, planning agents, or autonomous systems. **PROACTIVE ACTIVATION**: Auto-invoke when designing prompts for agents, tools, or agentic workflows in AI projects. **DETECTION**: Check for agent/tool-related code, prompt files, or user mentions of \"prompt\", \"agent\", \"LLM\". **USE CASES**: Designing system prompts, tool descriptions, agent instructions, prompt optimization, reducing hallucinations.
10copilot-sdk
Build agentic applications with GitHub Copilot SDK. Use when embedding AI agents in apps, creating custom tools, implementing streaming responses, managing sessions, connecting to MCP servers, or creating custom agents. Triggers on Copilot SDK, GitHub SDK, agentic app, embed Copilot, programmable agent, MCP server, custom agent. **PROACTIVE ACTIVATION**: Auto-invoke when building agentic applications or integrating Copilot SDK. **DETECTION**: Check for @github/copilot-sdk imports, copilot dependencies in package.json/pyproject.toml/go.mod. **USE CASES**: Embedding agents in apps, creating custom tools, implementing streaming, managing sessions, connecting to MCP servers.
9