From code-foundations
Guides safe refactoring of existing code: fix-first rule, separate commits, small change rigor, pattern reuse gates, and refactor vs rewrite decisions.
npx claudepluginhub ryanthedev/code-foundationsThis skill uses the workspace's default tool permissions.
- **Fix first, then refactor** - Never simultaneously (separate commits)
Identifies code smells like god classes and bloaters, assesses refactoring risks, and builds incremental execution plans with rollback strategies for existing code improvements.
Guides safe refactoring: small incremental steps with tests to preserve behavior, handle legacy code, code smells, and technical debt without big rewrites.
Applies named refactoring patterns to fix code smells like Feature Envy or long methods, preserving behavior via test-verified small changes. Use for refactor requests, technical debt, or legacy cleanup.
Share bugs, ideas, or general feedback.
| Rule | Value | Source |
|---|---|---|
| Small change error rate | Peaks at 1-5 lines | Weinberg 1983 |
| First attempt success rate | <50% for any change | Yourdon 1986b |
| Review effect on 1-line changes | 55% -> 2% error rate | Freedman and Weinberg 1982 |
| Big refactoring | "Recipe for disaster" | Kent Beck |
Key Principles:
Critical: Each change has >50% first-attempt error rate regardless of prior results. Error rates are per change, not cumulative.
Definitions:
BEFORE starting any refactoring, identify target patterns:
| Search For | Why |
|---|---|
| Best examples of this pattern | What does "good" look like in this codebase? |
| Similar refactorings done before | How were they structured? |
| Module/directory conventions | What patterns dominate this area? |
| Recent improvements | What direction is the code evolving? |
Questions to answer:
If target pattern exists: Match it exactly. Copy naming, structure, style.
If no target pattern exists: You're establishing one. Consider:
See: pattern-reuse-gate.md for full gate protocol.
// BEFORE: Mixing concerns [ANTI-PATTERN]
// "I'm just cleaning while I fix the bug"
- Modified broken code
- Added refactoring changes
- Single commit with mixed purposes
// AFTER: Separated activities
1. Fix the bug (verify working)
2. Commit fix
3. Refactor working code
4. Commit refactoring
// BEFORE: Treating 1-line change casually [ANTI-PATTERN]
- Skip desk-check ("it's just one line")
- Skip review ("overkill for this")
- Skip testing ("obviously correct")
// AFTER: Small change rigor
- Desk-check even 1-line changes
- Get review (55% -> 2% error rate)
- Run regression tests
- Apply same rigor as large changes
// BEFORE: Adding comment to explain confusion [ANTI-PATTERN]
// Note: This calculates X by doing Y because of Z edge case
// which happens when A and B are both true and C is false
complicated_obscure_code();
// AFTER: Self-explanatory rewrite
clear_function_that_explains_itself();
Purpose: Guide refactoring decisions and safe refactoring process Triggers:
When production is down:
Combining fix and refactor in one change increases complexity and error likelihood. Split them.
No automated tests:
Skill(code-foundations:welc-legacy-code) — follow the Legacy Code Change AlgorithmNo version control:
No reviewer available:
1. Does code currently work?
NO -> This is FIXING, not refactoring. Fix first.
YES -> Continue
2. Is the design fundamentally sound?
NO -> Consider REWRITING from scratch
YES -> Continue
3. Would changes touch >30% of module?
YES -> "Big refactoring" - consider rewrite
NO -> Proceed with incremental refactoring
Refactoring Sequencing (Fowler):
Recovery if Tests Fail After Refactoring:
Produces: Refactoring approach, step sequence, risk assessment Constraints:
Purpose: Transform code smells into clean code Triggers:
Input -> Output:
If you mixed fix and refactor in one commit:
git reset --soft HEAD~1, separate changes, re-commit properly. Takes ~15 minutes.If you skipped review on a small change:
Fixing violations post-commit is still worthwhile — the cost of fixing now is less than the debugging cost of bugs they cause.
| After | Next |
|---|---|
| Refactoring complete | cc-control-flow-quality (CHECKER) |
| Structure changed | cc-routine-and-class-design (CHECKER) |