home / skills / laurigates / claude-plugins / refactor

refactor skill

/code-quality-plugin/skills/refactor

This skill rewrites user-selected code to improve quality, readability, and performance while preserving external behavior.

npx playbooks add skill laurigates/claude-plugins --skill refactor

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
1.4 KB
---
model: opus
created: 2025-12-16
modified: 2025-12-16
reviewed: 2025-12-16
allowed-tools: Read, Write, Edit, MultiEdit, Glob, Grep, SlashCommand
argument-hint: <code-selection>
description: Refactor selected code for quality improvements
name: refactor
---

You are now in **Refactor Mode**. Your goal is to rewrite the user's selected code to improve its quality, readability, and performance without changing its external behavior.

### Instructions

1. **Identify Refactoring Opportunities:** Look for code smells such as long methods, large classes, duplicated code, feature envy, or primitive obsession.

2. **Apply Best Practices:** Refactor the code by applying established software design principles (SOLID, DRY, KISS). This may involve extracting methods, simplifying conditional logic, or introducing new data structures.

3. **Preserve Functionality:** The refactored code must pass all existing tests and produce the exact same output as the original code. Its external contract must not change.

4. **Verify Quality:** After refactoring:
   - Use SlashCommand: `/lint:check --fix` to ensure code quality
   - Use SlashCommand: `/test:run` to verify functionality is preserved
   - If tests fail, revert changes and try a different approach

5. **Output Code Only:** Provide only the improved, refactored code block. Do not include explanations unless specifically requested.

Begin refactoring.

Overview

This skill refactor rewrites selected code to improve quality, readability, and maintainability without changing behavior. It focuses on targeted structural improvements and automated verification so you get cleaner code that still passes the same tests. The skill integrates with linting and testing workflows to ensure safe refactors.

How this skill works

It analyzes the provided code for common code smells such as long functions, duplicated logic, and complex conditionals, then applies refactorings like method extraction, renaming, and simplification. After changes, it runs linting with auto-fixes and executes the test suite to confirm behavior is preserved. If tests fail, it reverts and attempts alternate refactor strategies to avoid breaking the contract.

When to use it

  • Cleaning up legacy code before adding features
  • Improving readability and reducing technical debt
  • Preparing code for code review or maintainability audits
  • Consolidating duplicated logic across modules
  • Optimizing slow or complex functions while preserving outputs

Best practices

  • Keep changes small and focused: one refactor per change set to simplify review
  • Preserve public interfaces and external contracts exactly
  • Run linting and the full test suite after every change
  • Prefer expressive names and small functions over comments to explain behavior
  • Avoid introducing new features during a refactor; refactor only for internal quality

Example use cases

  • Extracting helper functions from a 200-line script to reduce complexity
  • Replacing duplicated validation logic with a single utility function
  • Renaming unclear variables and parameters for immediate readability gains
  • Simplifying nested conditionals into guard clauses for clearer control flow
  • Grouping related constants into a single configuration structure for maintainability

FAQ

Will refactoring change program output?

No. The goal is to preserve external behavior and test outputs exactly; tests must pass before changes are accepted.

What verification steps are performed?

The skill runs automated linting with fixes and executes the test suite. If tests fail, changes are reverted and alternate approaches are attempted.

Can it add new dependencies or features?

No. Refactors should not introduce new external dependencies or behavior; only internal structure and clarity are improved.

How are risky refactors handled?

Risky changes are broken into smaller steps, validated by tests after each step, and rolled back if any test fails to ensure safety.