# Refactor

> Use when the user wants to refactor, clean up, restructure, or reorganize existing code without changing behavior.

- Skill: `majiayu000/refactor-18` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds add majiayu000/refactor-18`
- Raw SKILL.md: https://api.skillmd.com/api/skills/majiayu000/refactor-18/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: majiayu000 (https://skillmd.com/u/majiayu000)
- Updated: 2026-09-09
- Page: https://skillmd.com/skills/majiayu000/refactor-18

---


Refactor: **$ARGUMENTS**

## Rules

- NEVER change behavior — only improve structure
- ALWAYS run tests before AND after
- Make small, incremental changes
- Each change should be independently verifiable

## Steps

1. **Run tests first** — establish baseline
   ```
   [test command]
   ```
   If tests fail BEFORE refactoring, STOP and tell the user.

2. **Read the code** to refactor and understand it fully

3. **Read the tests** for this code — understand what behavior is expected

4. **Identify refactoring opportunities:**
   - Extract method (function too long)
   - Extract variable (complex expression)
   - Rename for clarity
   - Remove duplication (DRY)
   - Simplify conditionals
   - Replace magic numbers with constants
   - Reduce nesting depth
   - Split large files
   - Improve type safety

5. **Apply changes incrementally**
   - One refactoring at a time
   - Run tests after each change
   - If tests fail, revert immediately

6. **Run full test suite**
   ```
   [test command]
   ```

7. **Report**
   ```
   ## Refactoring Summary

   ### Changes
   - [what was refactored and why]

   ### Before → After
   - [specific improvements]

   ### Tests
   - All [X] tests passing
   - No behavior changes
   ```

<!-- Skill by Huzefa Nalkheda Wala | github.com/huzaifa525 | claude-code-optimizer -->

