Skills: Refactoring Skills
Last updated: 2026-08-31
Refactoring is not rewriting — it's improving code structure without changing behavior. Skills make refactoring methodical.
1. Code Smell Identification
(1) Common Code Smells
| Smell | Grep Pattern | Risk |
|---|---|---|
| Long function | Lines > 50 | 🟡 |
| Duplicate code | Similarity > 80% | 🟡 |
| Deep nesting | Nesting level > 3 | 🟡 |
| Magic numbers | Hardcoded constants | 🟢 |
| God class | Methods > 20 | 🔴 |
| Circular dependency | Mutual imports | 🔴 |
(2) Auto-Detection
YAML
---
name: smell-detector
description: "Detect code smells"
tools:
- Grep
- Glob
- Read
---
MARKDOWN
## Detection Flow
1. Glob to get file list
2. Grep to search for code smell patterns
3. Read to confirm in depth
4. Output report sorted by severity
2. Refactoring Strategy
(1) Refactoring Technique Mapping
| Smell | Refactoring Technique | Complexity |
|---|---|---|
| Long function | Extract Function | 🟢 Low |
| Duplicate code | Extract Method/Template Method | 🟡 Medium |
| Deep nesting | Guard Clauses/Strategy Pattern | 🟡 Medium |
| Magic numbers | Extract Constant | 🟢 Low |
| God class | Split Responsibilities | 🔴 High |
| Circular dependency | Introduce Interface/Mediator | 🔴 High |
(2) Refactoring Risk Assessment
TEXT
📖 Display only
Refactoring Risk Matrix
Small Impact Large Impact
Small Change 🟢 Safe 🟡 Needs testing
Large Change 🟡 Needs review 🔴 Needs step-by-step
(3) Refactoring Order Principles
MARKDOWN
## Refactoring Order
1. Low risk before high risk (extract constants → extract functions → split classes)
2. Local before global (function level → class level → module level)
3. Every step verifiable (small steps, run tests each step)
4. Stoppable at any time (code should work after any step)
3. Safe Refactoring Process
(1) Pre-Refactoring Preparation
MARKDOWN
## Pre-Checks
1. Bash: Run full test suite, confirm baseline passes
2. Bash: git commit current state (snapshot point)
3. Read: Understand full context of refactoring target
4. Identify all call sites (who depends on this code)
(2) Execute Refactoring
TEXT
📖 Display only
Safe Refactoring Loop
┌──────────────────┐
│ 1. Small modification │
│ 2. Run tests │
│ 3. Tests pass? │
│ ├─ Yes → Continue │
│ └─ No → Rollback │
└──────────────────┘
(3) Post-Refactoring Verification
MARKDOWN
## Verification Checklist
- [ ] Full test suite passes
- [ ] Functional behavior unchanged
- [ ] Code is simpler/cleaner
- [ ] No new TODO/FIXME
- [ ] No circular dependencies
4. Refactoring Skill Practice
▶ Example: Safe Extract Function
Alice needs to refactor a 200-line long function:
YAML
---
name: safe-extract-function
description: "Safe function extraction"
tools:
- Read
- Edit
- Grep
- Bash
---
MARKDOWN
## Extraction Flow
1. Read to analyze the long function, identify independent logic blocks
2. List extraction plan (which lines → new function name)
3. Execute step by step:
a. Edit to create new function below the original
b. Edit to modify original function to call new function
c. Bash to run tests
4. After all extractions, run full test suite
Bob said: "The biggest fear in refactoring is breaking functionality while modifying — taking small steps and verifying at each step is ten thousand times safer than changing everything at once and testing at the end."
❓ FAQ
Q What if tests fail during refactoring?
A Immediately roll back to the last passing state and analyze the failure cause. Common causes: missed call sites, incompatible function signatures, implicit dependencies.
Q How far should I refactor?
A Enough to meet team standards — don't over-refactor. The goal is eliminating code smells, not pursuing perfect architecture.
Q Can refactoring and feature development happen simultaneously?
A Not recommended. Refactoring doesn't change behavior; feature development does. Mixing the two makes it impossible to identify the source of problems.
📖 Summary
- Code smell identification: Auto-detect with Grep/Glob pattern matching
- Refactoring strategy: Smell → technique mapping, sorted by risk
- Safe process: Baseline test → small modification → step-by-step verification → rollback guarantee
- Core principle: Every step verifiable, rollbackable, stoppable
📝 Exercises
- Basic (⭐): Create a code smell detection Skill that detects at least 3 common issues.
- Intermediate (⭐⭐): Create a safe function extraction Skill implementing the small-step extraction + step-by-step verification flow.
- Advanced (⭐⭐⭐): Create an intelligent refactoring Skill that auto-identifies code smells, selects refactoring techniques, executes in safe order, and outputs before/after comparison reports.