Skip to content
cc-refactoring-guidance logo

Cc Refactoring Guidance

cc-refactoring-guidance

Guides safe refactoring using Code Complete's fix-first-then-refactor discipline: decides between refactor, rewrite, and fix-first; enforces separate commits; applies small-change rigor to high-error-rate one-liners.

ryanthedev/code-foundations0installs376stars

SKILL.md

Full skill instructions

cc-refactoring-guidance

Refactoring is behavior-preserving change to working code. Three rules govern it:

  • Fix first, then refactor — never in the same change; separate commits with a boundary between them.
  • Small changes need MORE rigor, not less — 1–5-line changes have the highest error rate.
  • Over half of changes fail the first attempt — review and test even "trivial" ones; the error rate is per change, not cumulative.
RuleValueSource
Small-change error ratePeaks at 1–5 linesWeinberg 1983
First-attempt success<50% for any changeYourdon 1986b
Review effect on 1-line changes55% → 2% error rateFreedman and Weinberg 1982

Shared thresholds (routine length, parameters, cohesion): Read(${CLAUDE_PLUGIN_ROOT}/​references/​cc-foundations.md). The full 40-item refactoring checklist: Read(${CLAUDE_SKILL_DIR}/​checklists.md).

Definitions:

  • small = ONE structural change (one rename, one extract, one inline). If you describe it with "and", it's not small.
  • review = a distinct person (or AI) examines the diff. Self-review does not satisfy this.
  • working code = all tests pass AND no known behavior bugs. Compiles-but-fails-tests is not working.
  • then (fix first, then refactor) = with a commit boundary between the activities, not the same commit.

Before refactoring: identify the target pattern

Refactor toward something, not just away from bad code. Find the best examples of the target pattern in this codebase, the module's conventions, and how similar refactorings were structured; match the existing pattern exactly, or — if none exists — be deliberate about the precedent you're setting. Full gate: Read(${CLAUDE_PLUGIN_ROOT}/​references/​pattern-reuse-gate.md).

Core patterns

  • Mixing fix and refactor → separate commits. Fix the bug, verify, commit; then refactor, commit. "I'm just cleaning while I fix" is the anti-pattern.
  • Casual small change → rigorous small change. Desk-check, review (55% → 2%), and run regression tests even on one line.
  • Documenting bad code → rewriting it. A comment explaining confusing code is a signal to make the code self-explanatory instead.

Modes

APPLIER

Guide the refactoring decision and the safe process.

When production is down: fix only — no refactoring or cleanup — deploy, then refactor as a separate activity when stable. Combining the two raises complexity and error likelihood.

Refactor vs rewrite:

  1. Does the code currently work? NO → this is fixing, not refactoring; fix first.
  2. Is the design fundamentally sound? NO → consider rewriting.
  3. Would changes touch >30% of the module? YES → "big refactoring"; consider a rewrite.

Safe process (priority order):

  1. Save the starting code (version-control checkpoint).
  2. Keep each refactoring small — one change at a time.
  3. List the planned steps before executing.
  4. Do one at a time; recompile and retest after each — state "tests pass after [change]".
  5. Use frequent checkpoints (commit working states).
  6. Enable the pickiest compiler warnings.
  7. Retest after each change and report results, not just "tests pass".
  8. Review changes — required even for 1-line changes.
  9. Adjust for risk — extra caution for high-risk changes.

If tests fail after a refactoring: don't debug extensively — back out the change, take a smaller step, and reconsider whether you understood the code.

When prerequisites are missing:

  • No automated tests → Read(${CLAUDE_PLUGIN_ROOT}/​skills/​welc-legacy-code/​SKILL.md) and write characterization tests first; if impossible, document expected behavior, write a manual test script, and increase review rigor.
  • No version control → back up files before starting; set up VCS first if at all possible.
  • No reviewer → systematic checklist-based self-review, smaller changes, more commits and tests to compensate.

TRANSFORMER

Transform code smells into clean code while preserving all observable behavior (same tests pass before and after): setup/​takedown smell → encapsulated interface; tramp data → direct access or restructure; duplicated code → extracted routine; speculative "design ahead" code → remove it. Behavior changes are fixing, not refactoring — fix first.

Recovering from a violation

If you mixed fix and refactor in one commit:

  • Before push: git reset --soft HEAD~1, separate the changes, re-commit properly.
  • After push: document it and split in a follow-up PR; don't force-push shared branches.

If you skipped review on a small change:

  • Before merge: get the review now.
  • After merge: note it and move on.

Fixing a violation post-commit still costs less than the bugs it would otherwise cause.

Chain

AfterNext
Refactoring completeRead(${CLAUDE_PLUGIN_ROOT}/​skills/​cc-control-flow-quality/​SKILL.md)
Structure changedRead(${CLAUDE_PLUGIN_ROOT}/​skills/​cc-routine-and-class-design/​SKILL.md)
Reviewing a refactoringRead(${CLAUDE_PLUGIN_ROOT}/​skills/​aposd-reviewing-module-design/​SKILL.md)

More skills from ryanthedev

cc-defensive-programming logo
ryanthedev/code-foundations

cc-defensive-programming

Applies Code Complete's defensive programming rules: barricade design, assertion vs error-handling selection, input validation policy, and correctness-vs-robustness strategy for the current context.

376 0
View
cc-pseudocode-programming logo
ryanthedev/code-foundations

cc-pseudocode-programming

Structures routine design through pseudocode-first iteration: clarifies naming and purpose before any code, catches design flaws cheaply, and prevents compile-debug loops on poorly understood routines.

376 0
View
aposd-simplifying-complexity logo
ryanthedev/code-foundations

aposd-simplifying-complexity

Transforms complex code by applying APOSD's pull-complexity-downward principle: resolves error hierarchies, collapses configuration, and moves caller-side logic into modules. Produces edited code, not just assessment.

376 0
View
aposd-designing-deep-modules logo
ryanthedev/code-foundations

aposd-designing-deep-modules

Guides module and API design using APOSD principles: generates multiple design alternatives, compares them on information hiding and interface depth, and produces a documented design decision.

376 0
View
aposd-reviewing-module-design logo
ryanthedev/code-foundations

aposd-reviewing-module-design

Assesses existing module and interface design for complexity symptoms: information leakage, shallow interfaces, pass-through layers, and unknown unknowns. Produces a structured assessment — not transformations.

376 0
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View