Use when the goal is to change code structure without changing behavior — this includes extracting a service object from a fat controller or model, splitting a large class, renaming abstractions, reducing duplication, or reorganizing modules. Covers characterization tests (write tests that document current behavior before touching the code), safe extraction in small steps, and verification after every step. Do NOT use for bug fixes or new features — those follow the TDD gate in rspec-best-practices. Do NOT mix structural changes with behavior changes in the same step.
90
88%
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Passed
No known issues
Use this skill when the task is to change structure without changing intended behavior.
Core principle: Small, reversible steps over large rewrites. Separate design improvement from behavior change.
| Step | Action | Verification |
|---|---|---|
| 1 | Define stable behavior | Written statement of what must not change |
| 2 | Add characterization tests | Tests pass on current code |
| 3 | Choose smallest safe slice | One boundary at a time |
| 4 | Rename, move, or extract | Tests still pass |
| 5 | Remove compatibility shims | Tests still pass, new path proven |
NO REFACTORING WITHOUT CHARACTERIZATION TESTS FIRST.
SEPARATE behavior changes from structural refactors.
VERIFY tests pass after EVERY step — not just at the end.If you haven't run the test suite after a refactoring step, you cannot claim it works.
EXTREMELY-IMPORTANT: Run verification after every refactoring step.
AFTER each step:
1. Run the full test suite
2. Read the output — check exit code, count failures
3. If tests fail: STOP, undo the step, investigate
4. If tests pass: proceed to next step
5. ONLY claim completion with evidence from the last test runForbidden claims:
Stable behavior to preserve: "Creating an order validates line items, applies pricing, persists the order, and enqueues NotifyWarehouseJob."
Smallest safe sequence (extract service):
OrdersController#create flow.Orders::CreateOrder with the same behavior; call it from the controller; keep controller response/redirect logic. Verify tests pass.Red-flag refactor (avoid): "Rename Order to Purchase and update all 50 call sites in one PR" — too many touchpoints; do renames in small steps with find/replace and tests after each commit.
| Mistake | Reality |
|---|---|
| "Quick refactor, no tests needed" | No characterization tests = no safety net. Bugs will slip through. |
| Mixing behavior change with structural change | Do one or the other. Mixed changes are impossible to review. |
| "I'll verify at the end" | Verify after EVERY step. Bugs compound when you batch. |
| Renaming 50 call sites in one commit | Do it in small batches with tests between each. |
| Adding abstraction to satisfy a pattern | Abstractions must serve a real boundary, not a textbook. |
| "Should work now" without running tests | Evidence before claims, always. |
| Removing old code before new path is proven | Keep compatibility shims until callers are fully migrated. |
When asked to refactor:
| Skill | When to chain |
|---|---|
| rspec-best-practices | For writing characterization tests before refactoring |
| rails-architecture-review | When refactor reveals structural problems |
| rails-code-review | For reviewing the refactored code |
| ruby-service-objects | When extracting logic into service objects |
ae8ea63
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.