refactoring
Detect smell -> establish behavior -> regression test -> minimal transformation -> verify -> repeat
- License 许可
- MIT
- 最近更新
- 2026-11-02
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- refactoring
- description
- Detect smell -> establish behavior -> regression test -> minimal transformation -> verify -> repeat
02 资源文件 6
- 参考文档checks/refactoring-invariants.md226 B
- 参考文档examples/extract_method_refactoring.md958 B
- 参考文档manifest.json1.1 KB
- 参考文档references/refactoring-patterns.md1.4 KB
- 脚本scripts/find_long_methods.sh1.3 KB
- 参考文档templates/refactoring-template.md794 B
03 SKILL.md 正文
Refactoring Engineering
1. Smell Detection
Proactively identify code smells that indicate deeper design problems. Look for duplicated code, long methods, large classes, long parameter lists, divergent change, and shotgun surgery. Smells are heuristics, not strict rules; they guide you to areas needing investigation.
2. Establishing Behavior
Before changing any code, you must deeply understand its current behavior, including edge cases and undocumented quirks. Read the code, consult domain experts, and review version control history. Do not guess; prove the behavior.
3. Regression Testing
Ensure a robust suite of fast, deterministic regression tests exists before refactoring. If tests are missing, write characterization tests (golden master tests) that lock down the current output for given inputs, regardless of whether that output is 'correct'.
4. Minimal Transformation
Execute refactoring in tiny, safe, verifiable steps. Use proven automated refactoring tools provided by your IDE whenever possible (e.g., Extract Method, Rename, Inline). Avoid manual text editing for structural changes to minimize human error.
5. Verification
Run the test suite immediately after every small transformation. If a test fails, you have taken too large a step or made a mistake. Undo (revert) immediately, break the step down into smaller pieces, and try again. Never commit broken code.
6. Iterative Process
Refactoring is not a single large event; it is an iterative loop: smell -> test -> refactor -> verify -> commit. Repeat this loop constantly. Keep refactoring commits separate from feature commits to ensure bisectability and clear history.
7. Extract and Override
When dealing with legacy code, use the Extract and Override Call pattern to break dependencies. Extract the problematic dependency instantiation into a protected factory method, then subclass and override it in your test to inject a mock or fake.
8. Strangler Fig Application
For large-scale structural refactoring or rewriting, use the Strangler Fig pattern. Build the new system around the edges of the old system. Intercept calls, route them to the new system where implemented, and fall back to the old system otherwise. Gradually expand the new system.
9. Parallel Change
When changing APIs or database schemas, use the Parallel Change (Expand and Contract) pattern. 1. Expand: Add the new element alongside the old. 2. Migrate: Update clients to use the new element. 3. Contract: Remove the old element. This ensures backward compatibility during the transition.
10. Feature Flags for Refactoring
Use feature flags to hide large-scale refactorings in production until they are fully verified. Run both the old and new code paths concurrently in a 'dark launch' mode, compare their results, log discrepancies, and switch over only when confidence is high.
04 同仓库更多 Skills
lang-aipo
Aipo V1 canonical engineering, ADR-001 syntax conformance, braces blocks, Tipo:nome method bindings, structural interfaces, transactional failure handling, and anti-hallucination guardrails for LLMs.
goal-management
暂无简介
evidence-collection
暂无简介
code-review
暂无简介
testing-quality
Testing pyramid, deterministic tests, regression tests, behavior assertions, test isolation, flaky detection, representative fixtures
cognitive-clarity
Unambiguous communication, single-decision focus, visual structuring, and rationale-first explanation tailored for neurodivergent collaboration