AGENT SKILL

refactoring

Systematic code improvement via design principles, smell detection, and safe transformation patterns. Triggers: refactor this, clean this up, simplify this code, reduce complexity, extract this, this code is messy, tech debt, improve this code, code smell, rename, extract function, dead code.

License 许可
MIT
最近更新
2026-11-12
Stars
★ 1
Forks
1

01 前言 Front Matter

name
refactoring
description
Systematic code improvement via design principles, smell detection, and safe transformation patterns. Triggers: refactor this, clean this up, simplify this code, reduce complexity, extract this, this code is messy, tech debt, improve this code, code smell, rename, extract function, dead code.

02 资源文件 2

03 SKILL.md 正文

Refactoring

Guide safe, incremental code improvements that preserve behavior while enhancing design. Refactoring is disciplined — every change is small, tested, and reversible.

Core Rule

Never change behavior and structure in the same step. Either you're adding a feature (behavior change) or you're refactoring (structure change). Mixing them creates bugs that are hard to trace.

Workflow

Step 1: Ensure Test Coverage

Before refactoring anything, verify that tests exist for the behavior you're about to restructure. If tests are missing:

  1. Write characterization tests that document current behavior (even if the behavior has quirks)
  2. Run them and confirm they pass
  3. Only then begin refactoring

If the user resists writing tests first, explain: refactoring without tests is not refactoring, it's rewriting with hope.

Step 2: Identify Smells

Read the code and identify code smells. Prioritize by impact — don't try to fix everything at once. The most common smells, roughly ordered by severity:

  1. Long function (>30 lines) → Extract method
  2. Duplicated logic → Extract and share
  3. Deep nesting (>3 levels) → Early returns, extract method
  4. Long parameter list (>4 params) → Introduce parameter object
  5. Feature envy → Move method to the class it uses most
  6. Primitive obsession → Introduce domain types
  7. Shotgun surgery → Consolidate related logic
  8. Divergent change → Split class by responsibility

See references/transformations.md for the complete catalog of safe transformations.

Step 3: Plan the Sequence

Order refactoring steps so that each step:

  • Is small enough to verify easily
  • Keeps all tests green
  • Makes the next step easier

A good sequence often looks like:

  1. Rename for clarity (cheapest, highest readability impact)
  2. Extract helpers to reduce function length
  3. Consolidate duplicates using the extracted helpers
  4. Move methods to better homes
  5. Simplify interfaces

Step 4: Execute — One Step at a Time

For each transformation:

  1. Explain what you're about to do and why
  2. Apply the single transformation
  3. Run tests and confirm green
  4. Show the diff to the user

If tests break, undo immediately. A failing test after refactoring means the transformation changed behavior — that's a bug, not a test to fix.

Step 5: Review the Result

After completing the planned sequence:

  • Compare before/after: Is the intent clearer? Is the code simpler?
  • Run the full test suite
  • Check that no public API changed (unless that was the goal)
  • Look for any new smells introduced by the refactoring

Principles Applied

  • DRY: Eliminate duplication discovered during refactoring
  • KISS: Every transformation should make the code simpler, not more abstract
  • SRP: Each extracted function/class should have one reason to change
  • YAGNI: Don't introduce abstractions for hypothetical future needs during refactoring
  • Functional Independence: Refactoring should reduce coupling, not increase it
  • Boy Scout Rule: Leave the code cleaner than you found it — every refactoring session should improve the surrounding code slightly, not just the target. Small cleanups (renaming, removing dead code) near the area you're working in compound over time.
  • Kent Beck's 4 Rules of Simple Design: After refactoring, the code should: (1) keep all tests green, (2) reveal intention clearly, (3) contain no duplication, (4) use the fewest classes and methods needed. Apply these as a checklist during Step 5 review.

Cross-Skill References

  • technical-debt-review — use to identify which areas are worth refactoring at a strategic level before starting
  • dependency-impact-analysis — use before refactoring a shared component or public interface to understand blast radius
  • code-slop-cleanup — most slop cleanup is a micro-refactor: use that skill for the pre-PR diff-scoped pass (removal-only, judged against file conventions); use this one when the cleanup turns structural (extract, move, re-abstract). They chain naturally: strip slop first, then refactor what remains

04 同仓库更多 Skills

SWEStash
SWEStash/swe-workflow-skills

writing-skills

Use when authoring, editing, or reviewing a skill in this library — a new SKILL.md, one that under-triggers, or hardening an existing one. Covers description and listing-budget rules, frontmatter, progressive disclosure, the 3-eval rule, and pressure-testing against rationalizations.

内容创作者财务测试与质量写作与文案
★ 1
SWEStash
SWEStash/swe-workflow-skills

working-agreement

Define and validate how a multi-session project is run — where plans, status and decisions live, session routines, new work, milestone close. README/CONTRIBUTING → project-documentation.

Skill与智能体开发
★ 1
SWEStash
SWEStash/swe-workflow-skills

verification-before-completion

Evidence gate before any claim that work is done, fixed, passing, or deployed. Route here when a message declares the work finished or asks to commit, push, merge, ship, or close it out, including a brief sign-off, and before git-workflow or release-management act on it. Writing the commit message or PR text itself → git-workflow.

Skill与智能体开发
★ 1
SWEStash
SWEStash/swe-workflow-skills

ui-ux-design

Design user experiences from an engineer's perspective — user flows, wireframes, interaction patterns, responsive strategy, navigation, loading/error/empty states. Triggers: design the UI, user flow, wireframe, how should this screen look, loading state, error state, empty state, responsive design, navigation, page layout, form design, mockup.

设计师视觉设计Skill与智能体开发
★ 1
SWEStash
SWEStash/swe-workflow-skills

threat-modeling

Design-time security analysis of a system or feature before it's built — trust boundaries, attack surface, STRIDE per element, abuse cases, prioritized mitigations that become security requirements. Triggers: threat model, STRIDE, trust boundary, attack surface, what could go wrong security-wise, security design review, abuse case, secure this design, security requirements. Reviewing EXISTING code/config for vulnerabilities → security-audit; use this skill while the design is still on the whiteboard.

开发工程师Skill与智能体开发
★ 1
SWEStash
SWEStash/swe-workflow-skills

test-suite-design

Design comprehensive test suites for existing code — strategy across unit/integration/e2e, fixtures, factories, helpers. Triggers: add tests, write tests for, increase coverage, test this module, testing strategy, test plan, what tests do I need, test infrastructure, test helpers, this has no tests. Not for TDD — use tdd-workflow.

开发工程师设计师测试与质量视觉设计
★ 1