Refactor

Refactor code incrementally while preserving behavior and passing tests.

3|1|Updated Jun 23, 2024
One-click install
npx skills add https://github.com/channingwalton/dotfiles --skill refactor-channingwalton
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Refactor
Source: https://github.com/channingwalton/dotfiles/tree/main/.claude/skills/refactor
Command: npx skills add https://github.com/channingwalton/dotfiles --skill refactor-channingwalton

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Improve code design and readability without changing external behaviour. This skill enforces a careful, incremental approach so you keep existing functionality while making it clearer and nicer to maintain.

Core Features & Use Cases

  • Non-behaviour changing: all changes preserve existing functionality.
  • Test-driven guardrails: run tests before and after each change to prevent regressions.
  • Incremental commits: small, verifiable steps that are easy to review.

Quick Start

Start a refactor on a module by first verifying the test suite is green, then apply a single, well-scoped improvement.

Frequently Asked Questions about Refactor

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I refactor code while keeping tests green?

Refactor code by making small, incremental changes that preserve behavior while improving structure. Run your full test suite before and after each change to ensure tests stay green, then commit each working state. This approach eliminates duplication, renames for clarity, extracts methods, and simplifies conditionals without altering external APIs.

What's the best way to improve code architecture without breaking functionality?

Apply incremental refactoring guided by TDD principles: verify tests pass, apply a single well-scoped improvement (eliminate duplication, rename variables, extract methods, simplify conditionals, or move responsibilities), run tests again, and commit. This preserves existing behavior while making code clearer and easier to maintain.

Can I refactor large classes and long methods safely?

Yes. Refactor large classes and long methods incrementally by extracting smaller methods, moving responsibilities, and renaming for clarity. Keep your test suite green throughout by running tests before and after each change, ensuring external behavior never changes while improving internal design.

Why should I run tests before starting a refactor?

Running tests before refactoring establishes a green baseline so you can detect regressions immediately. This test-driven guardrail prevents accidental behavior changes during incremental transformations and confirms that external APIs remain unchanged after each step.

How do I eliminate code duplication through refactoring?

Identify duplicated logic, extract it into reusable methods or functions, and update all call sites to use the extracted code. Run tests after each extraction to verify behavior is preserved, then commit. This reduces maintenance burden while keeping functionality intact.

What should I do if tests fail during refactoring?

Stop immediately and revert to the last green state. Refactoring should never change external behavior, so failing tests indicate an unintended side effect. Review the change, fix it to preserve original behavior, and re-run tests before proceeding or committing.