Loading...
Loading...
Python code refactoring skills, covering code smell identification, design pattern application, readability improvement, and practical experience. This skill is applicable when users request "refactor code", "refactor", "code optimization", "improve code quality", "code smell review", "apply design patterns", "enhance readability", or submit code review requests. It supports generating structured refactoring documents after refactoring completion ("output refactoring document", "generate refactoring report"). It includes practical patterns extracted from 20+ real refactoring PRs in the vllm-ascend repository.
npx skill4agent add ascend/agent-skills python-refactoringrefactoring-config.json| Task | Reference Document |
|---|---|
| Customize constraint thresholds | refactoring-config.json |
| Identify code issues | references/code-smells.md |
| Select design patterns | references/design-patterns.md |
| Improve readability | references/readability.md |
| Practical patterns and cases | references/refactoring-in-practice.md |
| Generate refactoring documents | references/refactoring-report.md |
| Code Smell | Recommended Pattern | Detailed Reference |
|---|---|---|
| Long Function | Extract Method, Guard Clause | code-smells.md §1.1 |
| Long Parameter List | dataclass Parameter Object, Builder | design-patterns.md §1.3 |
| Duplicate Code (Cross-cutting Concerns) | Decorator Pattern | design-patterns.md §2.1 |
| Duplicate Code (Same Process Skeleton) | Template Method / Strategy Pattern | practice §Pattern 3 |
| God Class | Split by Responsibility, Composition over Inheritance | practice §Pattern 2, 5 |
| Excessive Inheritance | Composition + Protocol | design-patterns.md §3.1 |
| if-elif Instance Creation | Factory Pattern (Registry) | practice §Pattern 1 |
| if-elif Type Dispatch | Polymorphism / singledispatch | design-patterns.md §3.4 |
Multiple | Strategy Pattern (Protocol) | practice §Pattern 3 |
| dict/tuple for Composite Data Transfer | dataclass Type Formalization | practice §Pattern 7 |
| Magic Numbers/Strings | Enumeration (Enum / IntEnum) | code-smells.md §3.3 |
| Custom Implementation Duplicating Upstream | Align with Upstream / Inherit Base Class | practice §Pattern 4 |
| Oversized Module (>500 lines) | Separate Concerns, Package化 | practice §Pattern 2 |
| Disadvantaged Path Proven by Benchmark | Delete Dead Path | practice §Pattern 6 |
┌─────────────────────────────────────────────────────────┐
│ 0. Pre-check │
│ - Confirm test coverage (add critical path tests first if none) │
│ - Confirm code runs normally │
│ - Evaluate refactoring scale → refactoring-in-practice.md Scale Matrix │
├─────────────────────────────────────────────────────────┤
│ 1. Identify Issues → references/code-smells.md │
│ - Read through code, check item by item by category │
│ - Output issue list, sorted by severity │
├─────────────────────────────────────────────────────────┤
│ 2. Select Strategy → references/refactoring-in-practice.md │
│ - Match seven practical patterns, choose the most suitable refactoring path │
│ - Develop phased plan for large-scale refactoring (refer to Quantization/MoE cases) │
├─────────────────────────────────────────────────────────┤
│ 3. Structural Refactoring → references/design-patterns.md │
│ - Select appropriate pattern for structural issues │
│ - Implement refactoring, ensure interface compatibility │
├─────────────────────────────────────────────────────────┤
│ 4. Polish and Optimize → references/readability.md │
│ - Improve naming, structure, type annotations │
│ - Add necessary comments and documentation │
├─────────────────────────────────────────────────────────┤
│ 5. Validation │
│ - Run all existing tests, confirm no regression │
│ - Run linter / type checker │
│ - Compare before and after refactoring, confirm consistent behavior │
├─────────────────────────────────────────────────────────┤
│ 6. Output Refactoring Document (optional, generated when requested by user) │
│ - Summarize complete record of this refactoring │
│ - Generate structured document, usable for PR description / team sharing / project archiving │
│ - See references/refactoring-report.md │
└─────────────────────────────────────────────────────────┘| Scale | Scope | Recommendations | Reference Case |
|---|---|---|---|
| Small | Single function/method | Modify directly, submit in one commit | MoE #5189 Reuse upstream all_reduce (-38 lines) |
| Medium | Single class or module | Split into 2-3 commits | MoE #5481 dict/tuple → dataclass (6 files) |
| Large | Cross-module/package | Develop a plan, execute in phases | Quantization 4-phase refactoring (36 files) |
### [Issue Type] Issue Brief
**Location:** `file_path:line_number`
**Issue:** Describe the current code issue
**Solution:** Describe the fix plan
**Practical Reference:** Matching practical pattern (if applicable)
**Before Fix:**
(code snippet)
**After Fix:**
(code snippet)
**Basis:** Cited reference documents and specific rules