AgentSkillsCN

refactor

分析代码,并结合前后对比示例,提出切实可行的重构建议。

SKILL.md
--- frontmatter
name: refactor
description: Analyze code and suggest concrete refactorings with before/after examples.
allowed-tools: Read, Grep, Glob, Bash, Task

Refactor

Analyze code and suggest concrete refactorings. For large architectural changes, spawn designer subagent.

Workflow

  1. Identify scope - file, function, or module
  2. Analyze - check against refactoring checklist
  3. For large refactors - spawn designer subagent for architectural planning
  4. Propose changes - specific before/after with priority order
  5. Offer to implement - if user approves

When to Use Designer Subagent

Spawn designer via Task tool when:

  • Splitting a module into multiple files
  • Changing class/type hierarchy
  • Restructuring dependencies
  • Anything touching 3+ files

For single-file refactors, analyze directly.

Refactoring Checklist

Naming

IssueAction
Unclear namesRename with intent
_v2, _new suffixesReplace with descriptive
Magic numbersExtract to constants
Inconsistent styleAlign with codebase

Size

MetricThresholdAction
Function lines>40Extract helpers
Parameters>4Parameter object
Nesting depth>3Early return / extract
File lines>400Split module

Smells

SmellAction
Duplication (3+ lines, 2+ times)Extract function
Feature envyMove method
Long param listParameter object
Primitive obsessionValue types
Dead codeDelete
Speculative generalityDelete

Structure

  • Multiple responsibilities → Split
  • Deep nesting → Early returns
  • God object → Decompose

Output Format

code
## Refactoring: [Name]

Location: file:line
Problem: [Why it matters]

Before:
[code]

After:
[code]

Benefit: [Improvement]

Summary

code
## Refactoring Plan

Priority:
1. [Quick win] - file:line
2. [Medium effort] - file:line
3. [Larger change] - file:line

Risk: [Safe / Needs tests first]

Implement these changes?