Guide safe refactoring with research-backed discipline and small-change rigor. Produce refactoring approach, step sequence, and risk assessment. Use when modifying existing code, improving structure without changing behavior, or deciding between refactor, rewrite, or fix-first. Triggers on: code smells, technical debt, just cleaning while I fix, small change treated casually, regression from trivial change, one-line change errors, production down with cleanup urge.
Refactoring is behavior-preserving change to working code. Three rules govern it:
| Rule | Value | Source | |---|---|---| | Small-change error rate | Peaks at 1–5 lines | Weinberg 1983 | | First-attempt success | <50% for any change | Yourdon 1986b | | Review effect on 1-line changes | 55% → 2% error rate | Freedman and Weinberg 1982 |
Shared thresholds (routine length, parameters, cohesion): Read(${CLAUDE_PLUGIN_ROOT}/references/cc-foundations.md). The full 40-item refactoring checklist: Read(${CLAUDE_SKILL_DIR}/checklists.md).
Definitions:
Refactor toward something, not just away from bad code. Find the best examples of the target pattern in this codebase, the module's conventions, and how similar refactorings were structured; match the existing pattern exactly, or — if none exists — be deliberate about the precedent you're setting. Full gate: Read(${CLAUDE_PLUGIN_ROOT}/references/pattern-reuse-gate.md).
Guide the refactoring decision and the safe process.
When production is down: fix only — no refactoring or cleanup — deploy, then refactor as a separate activity when stable. Combining the two raises complexity and error likelihood.
Refactor vs rewrite:
Safe process (priority order):
If tests fail after a refactoring: don't debug extensively — back out the change, take a smaller step, and reconsider whether you understood the code.
When prerequisites are missing:
Skill(code-foundations:welc-legacy-code) and write characterization tests first; if impossible, document expected behavior, write a manual test script, and increase review rigor.Transform code smells into clean code while preserving all observable behavior (same tests pass before and after): setup/takedown smell → encapsulated interface; tramp data → direct access or restructure; duplicated code → extracted routine; speculative "design ahead" code → remove it. Behavior changes are fixing, not refactoring — fix first.
If you mixed fix and refactor in one commit:
git reset --soft HEAD~1, separate the changes, re-commit properly.If you skipped review on a small change:
Fixing a violation post-commit still costs less than the bugs it would otherwise cause.
| After | Next |
|---|---|
| Refactoring complete | Skill(code-foundations:cc-control-flow-quality) |
| Structure changed | Skill(code-foundations:cc-routine-and-class-design) |
| Reviewing a refactoring | Skill(code-foundations:aposd-reviewing-module-design) |
npx skills add ryanthedev/cc-refactoring-guidance下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer