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


📝 Exercises

  1. Basic (⭐): Create a code smell detection Skill that detects at least 3 common issues.
  2. Intermediate (⭐⭐): Create a safe function extraction Skill implementing the small-step extraction + step-by-step verification flow.
  3. Advanced (⭐⭐⭐): Create an intelligent refactoring Skill that auto-identifies code smells, selects refactoring techniques, executes in safe order, and outputs before/after comparison reports.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏