refactor

TDD-based code refactoring preserving behavior through tests. Use Red-Green-Refactor cycles to apply refactoring patterns one test-verified change at a time. **TRIGGERS**: 'clean up code', 'make code simpler', 'reduce complexity', 'refactor this', 'apply DRY', 'extract method', 'remove duplication'. **DISTINCT FROM**: Adding features (use /tdd) or fixing bugs. **PROACTIVE**: Auto-invoke when test-covered code has complexity (functions >50 lines, high cyclomatic complexity, duplication).

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "refactor" with this command: npx skills add mguinada/agent-skills/mguinada-agent-skills-refactor

Refactor Skill

Collaborating skills

  • TDD: skill: tdd for guiding through Red-Green-Refactor cycles and ensuring tests are in place before refactoring
  • Design Patterns: skill: design-pattern-adopter for guidance on applying design patterns effectively

Quick Reference

Code Smells Reference

SmellDetectionRefactoring
Long function>20 linesExtract Method
Duplicated codeSimilar logicExtract Method, Pull Up
Long parameter list>3 paramsIntroduce Parameter Object
Large class>10 methodsExtract Class
Feature envyMethod uses other classMove Method
Switch statementsType checkingReplace with Polymorphism
Speculative generalityUnused abstractionsInline, Collapse Hierarchy
Message chainsa.b.c.dHide Delegate
Middle manExcessive delegationRemove Middle Man
Incomplete library classMissing methodsIntroduce 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

  1. Tests must exist - If no tests, write them first
  2. Tests must pass - Run tests before starting
  3. 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

External Resources

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Automation

tdd

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

code-quality

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

clean-code

No summary provided by upstream source.

Repository SourceNeeds Review