refactoring-guru
Diagnose a named code smell and apply the smallest behavior-preserving refactoring, with verification before and after and an explicit stop condition. Use for structural refactors (extract, move, consolidate, change ownership), not for feature or bug work; for readability cleanup of recent changes use code-simplification.
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- refactoring-guru
- license
- MIT
- description
- Diagnose a named code smell and apply the smallest behavior-preserving refactoring, with verification before and after and an explicit stop condition. Use for structural refactors (extract, move, consolidate, change ownership), not for feature or bug work; for readability cleanup of recent changes use code-simplification.
02 资源文件 48
- 参考文档refactoring-guru.md49.6 KB
- 参考文档refactoring-guru.mini.md5.9 KB
- 参考文档refactoring-guru.nano.md2.4 KB
- 参考文档references/ATTRIBUTION.md443 B
- 参考文档references/catalog/cond-consolidate.md2.2 KB
- 参考文档references/catalog/cond-decompose.md2.0 KB
- 参考文档references/catalog/cond-guard-clauses.md2.1 KB
- 参考文档references/catalog/cond-lookup-table.md2.1 KB
- 参考文档references/catalog/cond-polymorphism.md2.3 KB
- 参考文档references/catalog/cond-special-case.md2.1 KB
- 参考文档references/catalog/couple-dependency-injection.md2.0 KB
- 参考文档references/catalog/couple-feature-envy.md1.9 KB
- 参考文档references/catalog/couple-hide-delegate.md1.7 KB
- 参考文档references/catalog/couple-interface-segregation.md1.9 KB
- 参考文档references/catalog/couple-preserve-whole-object.md1.9 KB
- 参考文档references/catalog/couple-remove-middle-man.md1.9 KB
- 参考文档references/catalog/data-encapsulate-collection.md2.0 KB
- 参考文档references/catalog/data-encapsulate-record.md2.3 KB
- 参考文档references/catalog/data-replace-primitive.md2.5 KB
- 参考文档references/catalog/data-replace-temp-with-query.md2.0 KB
- 参考文档references/catalog/data-split-variable.md2.4 KB
- 参考文档references/catalog/error-custom-exceptions.md2.9 KB
- 参考文档references/catalog/error-exceptions-over-codes.md2.6 KB
- 参考文档references/catalog/error-fail-fast.md2.2 KB
- 参考文档references/catalog/error-separate-concerns.md3.0 KB
- 参考文档references/catalog/micro-inline-variable.md2.0 KB
- 参考文档references/catalog/micro-remove-dead-code.md1.9 KB
- 参考文档references/catalog/micro-rename-for-clarity.md1.9 KB
- 参考文档references/catalog/micro-simplify-expressions.md1.9 KB
- 参考文档references/catalog/name-avoid-abbreviations.md1.6 KB
- 参考文档references/catalog/name-avoid-encodings.md1.9 KB
- 参考文档references/catalog/name-consistent-vocabulary.md1.7 KB
- 参考文档references/catalog/name-intention-revealing.md1.3 KB
- 参考文档references/catalog/name-searchable-names.md1.5 KB
- 参考文档references/catalog/pattern-composition-over-inheritance.md2.1 KB
- 参考文档references/catalog/pattern-extract-superclass.md2.3 KB
- 参考文档references/catalog/pattern-factory.md3.0 KB
- 参考文档references/catalog/pattern-open-closed.md2.4 KB
- 参考文档references/catalog/pattern-strategy.md2.7 KB
- 参考文档references/catalog/pattern-template-method.md2.9 KB
- 参考文档references/catalog/README.md4.8 KB
- 参考文档references/catalog/struct-compose-method.md2.4 KB
- 参考文档references/catalog/struct-extract-class.md2.0 KB
- 参考文档references/catalog/struct-extract-method.md2.2 KB
- 参考文档references/catalog/struct-function-length.md2.9 KB
- 参考文档references/catalog/struct-parameter-object.md2.2 KB
- 参考文档references/catalog/struct-replace-method-with-object.md3.4 KB
- 参考文档references/catalog/struct-single-responsibility.md1.9 KB
03 SKILL.md 正文
Refactoring.Guru Skill
Refactoring: small, smell-driven, behavior-preserving change, verified, with stop condition. Not general cleanup. Not an excuse to apply patterns.
Before deciding anything, read and apply refactoring-guru.mini.md: decision rules, trigger rules, final checklist. Deeper reference for full smell catalog (bloaters, OO abusers, change preventers, dispensables, couplers) or tradeoffs mini rules don't settle: refactoring-guru.md.
Workflow
1. Diagnose the smell
Name smell. Record symptom, current maintenance cost, scope, expected cleaner end state. Can't name a smell with present cost: don't refactor.
2. Establish the behavior boundary and proof
- Decide what must not change: public interfaces, return values, error types and messages, side effects, ordering, serialized or reflected names. Treat each found bug as separate change.
- Pick proof to run before and after. Use existing tests if they cover changed code.
- When no tests cover the code, write characterization tests first. They record current behavior, oddities included, not intended behavior. Output-heavy code: golden-master snapshot. Capture outputs for representative set of inputs, compare after each step.
- Run proof once before editing; confirm it passes on current code.
3. Pick the smallest technique
Map smell to treatment via mini file trigger rules. Then open matching entry in references/catalog/README.md for mechanics. Escalate (method object, new class, polymorphism, pattern) only when smaller treatment is blocked.
4. Apply in small steps
- Move one ownership boundary per step (one method, field, or responsibility).
- Keep every intermediate state buildable and testable. Never leave tree broken between steps.
- No new dependencies or configuration unless correctness requires them.
- Changing signatures or other externally reachable APIs: keep transition path (delegating old entry point, deprecation) unless breaking them is in scope.
5. Verify
Rerun same proof after each risky step (moves, signature changes, state-flow changes, algorithm substitutions) and at end. If a test needs editing to pass, behavior changed. Stop and split out that change.
6. Stop
Stop when named smell is gone or materially reduced and requested structure reached. Log other smells as follow-ups; don't expand the change.
Report
Smell, technique(s) applied, proof run (results before and after), split-out behavior changes, smells deliberately left untreated.
Related
code-simplification: readability cleanup of recent changes (naming, nesting, dead code) without structural moves.code-refactoring-tech-debt: choosing which debt to refactor across codebase.gof-patterns: judging if existing pattern is over-engineered before removing or adding one.
04 同仓库更多 Skills
wiki-builder
Create and maintain a local Markdown research wiki (sources with provenance, compiled concept/source pages, index, maintenance log) driven by a per-wiki config. Use when the user wants to start a wiki or knowledge base, ingest sources into one, or query and update it; use wiki-page-writer for a single code deep-dive page.
legacy-seams
When changing untested legacy code, pin the observed results with characterization tests and open the smallest seam that substitutes only the blocking dependency, separate from any later logic change. Use when a clock, random source, I/O, global, or hidden constructor blocks a test or a safe edit. Use test-driven-development for a new behavior or a bug, refactoring-guru for one named structural refactor after the seam exists, and code-refactoring-tech-debt to choose which legacy to replace.
idea-refine
Refine a raw idea into a one-page brief (problem statement, recommended direction, assumptions to validate, MVP scope, not-doing list) through divergent then convergent thinking. Use when an idea is still vague or you want options and a stress test before committing; use interview-me when the goal itself is unclear and spec-driven-development once the direction is chosen.
code-review-and-quality
Review rubric and approval standard for any change before merge, including code written by you, another agent, or a human: five axes (correctness, readability, architecture, security, performance), change sizing, structural remedies, severity labels, and feedback technique. Use to judge whether a change is fit to merge; use code-review to run a PR or diff review end to end.
code-perfection
Smell and style gate for code you write or change. Bans known code smells (long method, large class, primitive obsession, long parameter list, data clumps, duplicate code, dead code, feature envy, shotgun surgery, flag arguments, message chains) and enforces behavior-preserving structure. On Scala files, enforces worxbend style. Use while implementing, reviewing a diff for smells, or when a smell or style violation is suspected. Routine construction stays with code-complete. SOLID and DRY stay with clean-code-guard. One named structural refactor stays with refactoring-guru.
code-complete
Construction discipline from Steve McConnell's Code Complete for implementing code: routine and class design, variable naming and data types, simple control flow, defensive checks at trust boundaries, and pseudocode-first design for complex routines. Use while writing or restructuring routines; for a post-edit quality pass use clean-code-guard.