Cc refactoring guidance

From ryanthedev/code-foundations · View interactive version

Cc refactoring guidance is an AI agent skill from the GitHub repository ryanthedev/code-foundations. 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. Not for diagnosing the bug itself (use cc-debugging), transforming working code's c

How to use this skill

Skill file

cc-refactoring-guidance

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

| Rule | Value | Source |

|---|---|---|

| Small-change error rate | Peaks at 1–5 lines | Weinberg 1983 |

| First-attempt success | <50% for any change | Yourdon 1986b |

| Review effect on 1-line changes | 55% → 2% error rate | Freedman and Weinberg 1982 |

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

Definitions:

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.

Core patterns

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:

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. To assess whether the result is a better module design, hand off to Skill(code-foundations:aposd-reviewing-module-design).

Recovering from a violation

If you mixed fix and refactor in one commit:

If you skipped review on a small change:

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

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.