# Refactor

> Use when improving the structure of existing code without changing its external behavior — renaming, extracting, simplifying, or removing duplication.

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

---


# Refactor

## When to use
- A function grew too large or does too many things.
- The same logic is duplicated across files.
- Names no longer reflect what the code does.

## Workflow
1. **Lock behavior first.** Ensure there is a test suite (or write one) that captures the
   current contract. Refactoring without a safety net is rewriting.
2. **Pick one improvement** and do it as a single, reviewable step.
3. **Prefer mechanical moves:** rename, extract function, inline, introduce parameter.
   Avoid changing logic while restructuring.
4. **Run the tests after every step.** Small, frequent checkpoints beat one big rewrite.
5. **Delete dead code** rather than commenting it out.

## Constraints
- No behavior change. If you fix a bug along the way, do it in a separate commit.
- Don't introduce new abstractions for hypothetical future needs (YAGNI).
- Keep the public API stable unless the change is explicitly about the API.

## Definition of done
- Tests are green and coverage did not drop.
- Diff is dominated by structure changes, not logic changes.
- The code is measurably simpler (fewer branches, less duplication).

