refactoring
Provides structural assessment and refactoring patterns to improve code quality after tests pass.
Install
mkdir -p .claude/skills/refactoring && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/6702" && unzip -o skill.zip -d .claude/skills/refactoring && rm skill.zipInstalls to .claude/skills/refactoring
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Refactoring assessment and patterns for already-tested code. Use when the user asks to refactor, clean up, simplify, or restructure existing code, and automatically after mutation testing validates test strength (the REFACTOR step of the TDD cycle). Covers commit-before-refactoring discipline, when refactoring adds value vs when to skip it, and the priority classification of improvement opportunities. Do NOT use for untested code (see characterisation-tests and finding-seams first) or for adding behavior (see tdd).Key capabilities
- →Classifies code smells by priority
- →Verifies code safety through test pass status
- →Provides patterns for decoupling knowledge
- →Manages commit history around refactoring
How it works
Follows a defined TDD-cycle checklist, ensuring code is committed, tested, and evaluated before any restructuring occurs.
Inputs & outputs
When to use refactoring
- →Cleaning up complex functions
- →Removing magic numbers
- →Improving code structure after TDD cycle
About this skill
Refactoring
Refactoring is the final step of each fast RED-GREEN-REFACTOR increment when restructuring is applicable. Assess it after GREEN establishes a passing behavior-test baseline. Do not run the automated mutation harness before or after each refactor; mutation testing verifies the completed phase once the work is otherwise ready for a PR.
Because automated mutation evidence is intentionally deferred, the baseline's strength is not yet mutation-harness-verified during refactoring. Keep each refactor small, strictly behavior-preserving, and green under the existing oracles; the final gate validates the accumulated result.
This skill safely implements a bounded, behavior-preserving improvement. Use improve-codebase-architecture to discover and rank architecture candidates, then codebase-design to design a selected module contract before returning here for implementation. If the slice participates in a selected whole-path reduction program, whether as a transition or terminal reduction, reduce-system-complexity governs the ledger and gate state; use this skill only as a secondary refactoring assessment when applicable.
When to Refactor
- Assess after GREEN or another passing proportionate preservation baseline
- Only refactor if it improves the code
- Establish a verified, recoverable baseline before refactoring; commit only with explicit user approval
Establish a Recoverable Baseline - WHY
Having a working baseline before refactoring:
- Allows reverting if refactoring breaks things
- Provides safety net for experimentation
- Makes refactoring less risky
- Can show clear separation in git history when the user authorizes commits
If the baseline cannot be restored safely without creating a commit, stop and ask for approval rather than committing implicitly.
Workflow:
- BASELINE: Applicable tests pass and/or the conserved behavior and guarantees have proportionate evidence
- CHECKPOINT: Record the baseline and preservation evidence. Create a baseline commit only when the user explicitly approves it
- REFACTOR: Improve structure in small steps under the
tddskill's canonical fast-feedback policy. From a clean baseline, prefer a proven repository-owned graph-complete watcher; use diff-selected Vitest watch only when the installed version/configuration has passed the canonical clean-start live proof, otherwise repeat the affected one-shot. In monorepos use the root graph so transitive consumers remain eligible - VERIFY: Keep focused and affected tests plus other proportionate evidence green after each step; do not rerun the full suite after every edit
- CHECKPOINT: Present the verified refactor. Commit it only after explicit user approval
- PRE-PR GATE: When the phase is otherwise ready for a PR, run mutation testing once for the accumulated scope where meaningful, or record explicit
N/Aplus proportionate alternate evidence; address valuable survivors within that gate
Priority Classification
| Priority | Action | Examples |
|---|---|---|
| Critical | Fix now | Behavior-changing mutation, divergent copies of one business rule, control flow that obscures a high-risk path |
| High | This session | Magic numbers, unclear names, functions coordinating multiple responsibilities |
| Nice | Later | Minor naming, single-use helpers |
| Skip | Don't change | Already clean code |
DRY = Knowledge, Not Code
Abstract when:
- Same business concept (semantic meaning)
- Would change together if requirements change
- Obvious why grouped together
Keep separate when:
- Different concepts that look similar (structural)
- Would evolve independently
- Coupling would be confusing
Example Assessment
// After GREEN establishes a passing behavior-test baseline:
const planBatch = (batch: Batch): PlannedBatch => {
const itemSlots = batch.items.reduce((sum, item) => sum + item.quantity, 0);
const bufferSlots = itemSlots > 50 ? 0 : 6;
return { ...batch, plannedSlots: itemSlots + bufferSlots, bufferSlots };
};
// ASSESSMENT:
// ⚠️ High: Magic numbers 50, 6 → extract constants
// ✅ Skip: Structure is clear enough
// DECISION: Extract constants only
New Behavior Needs Evidence
Do not add new behavior without a failing test or other repository-authorized acceptance proof that demands it. A behavior-preserving refactor may change lines without a new RED test only while proportionate preservation evidence stays green. At PR readiness, use mutation evidence for the accumulated scope where meaningful and explicit alternate evidence where it is not; never invent structural mutants.
❌ Speculative additions:
- "Just in case" logic
- Features not yet needed
- Abstractions written only for imagined future flexibility
- New error behavior with no accepted contract
✅ Correct approach: Do not add the speculative behavior. If it is needed, write a failing test that demands it, then implement it.
Existing untested code is not proven speculative or dead. Before removing a branch, characterize its observable behavior, inspect every caller and reachability path, and resolve the behavior authority. Delete it only when the evidence shows it is unreachable or the accepted contract explicitly retires it, then keep the preservation/regression checks green.
// ❌ WRONG - Speculative error handling (no test demands this)
if (items.length === 0) {
throw new Error('Empty cart'); // No test for this path!
}
// ✅ CORRECT - Test-driven error handling
// First: write a test that expects this behavior
// Then: implement the guard clause to make it pass
When NOT to Refactor
Don't refactor when:
- ❌ The current structure isn't impeding the work at hand (clean-enough working code needs no restructuring)
- ❌ Speculative generality — restructuring for requirements that don't exist yet
- ❌ Would change behavior (that's a feature, not refactoring)
- ❌ Premature optimization
- ❌ Code is "good enough" for current phase
- ❌ Extracting purely for testability — if the only reason to move code into a separate file is "so we can unit test it", keep it inline. The consuming function already has behavioral tests that cover this code. Extract for readability, DRY (same knowledge used in multiple places — see "DRY = Knowledge, Not Code" above), or separation of concerns, never for testability alone.
Remember: Refactoring should improve code structure without changing behavior.
Commit Messages for Refactoring
When the user approves a refactoring commit, use a focused message such as:
refactor: extract scenario validation logic
refactor: simplify error handling flow
refactor: rename ambiguous parameter names
Format: refactor: <what was changed>
Note: When commits are used, refactoring commits should not be mixed with feature commits.
Refactoring Checklist
- Existing behavior tests pass; test edits are not hiding a behavior change
- Focused/affected tests stayed green during refactoring, and the repository-defined complete non-watch PR test gate passes before PR
- If the refactored phase is ready for a PR, mutation results were reviewed once for the accumulated scope where meaningful, or explicit
N/Aplus proportionate alternate evidence was recorded - No unplanned consumer-facing API was added; internal or temporary contracts follow the selected design and compatibility plan
- Code more readable than before
- Any commits were explicitly approved and kept separate from features
- A verified, recoverable baseline was established; a baseline commit was created only if approved
- No speculative code added
- Behavior unchanged within the confidence and fidelity of the passing preservation evidence
When not to use it
- →Untested codebases
- →Adding new functionality
Prerequisites
Limitations
- →Requires established test coverage
- →Requires disciplined commit discipline
How it compares
It prioritizes refactoring as a disciplined, post-validation process rather than an ad-hoc cleanup.
Compared to similar skills
refactoring side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| refactoring (this skill) | 1 | 2mo | No flags | Intermediate |
| tdd-workflow | 6 | 4mo | Review | Intermediate |
| qlty-check | 5 | 7mo | Review | Beginner |
| superpowers-tdd | 5 | 6mo | No flags | Intermediate |
Try saying
Example prompts that trigger this skill in your AI assistant.
More by citypaul
View all by citypaul →You might also like
tdd-workflow
affaan-m
在编写新功能、修复错误或重构代码时使用此技能。强制执行测试驱动开发,包含单元测试、集成测试和端到端测试,覆盖率超过80%。
qlty-check
parcadei
Code quality checks, formatting, and metrics via qlty CLI
superpowers-tdd
anthonylee991
Applies tests-first discipline (red/green/refactor) and adds regression tests for bugs. Use when implementing features, fixing bugs, or refactoring.
solid
ramziddin
Use this skill when writing code, implementing features, refactoring, planning architecture, designing systems, reviewing code, or debugging. This skill transforms junior-level code into senior-engineer quality software through SOLID principles, TDD, clean code practices, and professional software design.
bunit-test-migration
FritzAndFriends
Migrate bUnit test files from deprecated beta API (1.0.0-beta-10) to bUnit 2.x stable API. Use this when working on .razor test files in BlazorWebFormsComponents.Test that contain old patterns like TestComponentBase, Fixture, or SnapshotTest.
code-refactor
luongnv89
Systematic code refactoring based on Martin Fowler's methodology. Use when users ask to refactor code, improve code structure, reduce technical debt, clean up legacy code, eliminate code smells, or improve code maintainability. This skill guides through a phased approach with research, planning, and safe incremental implementation.