Immutable. This exact content is served forever at /api/v1/blob/3573233fe583fed8.
--- name: refactor description: > Restructure existing code without changing its behavior. USE WHEN refactoring, cleaning up code, reducing duplication, improving naming, or simplifying complex logic. --- # Refactor Safely restructure code while preserving existing behavior. ## Steps 1. **Verify tests exist** — Before touching anything, confirm test coverage on the code you'll change. If tests are missing, write them first against current behavior. 2. **Name the problem** — What exactly is wrong? - Duplicated logic? Function doing too many things? Poor naming? Tangled dependencies? Wrong abstraction? 3. **Plan the change** — Describe what you'll do before doing it. Small, safe steps. 4. **One change at a time** — Each step keeps the code working. Don't rewrite everything at once. 5. **Run tests after each step** — If tests fail, you changed behavior. Undo and try smaller. 6. **Stop when good enough** — Clear, tested, easy to change = done. ## Gotchas - Don't extract abstractions used only once — three similar lines is better than a premature abstraction. - Don't add design patterns just to demonstrate knowledge. - Don't rename things across the entire codebase without asking. - Don't change public APIs without understanding downstream impact. - Don't refactor and add features in the same change — separate commits/PRs. - Don't refactor code that works, is clear, and is tested just because you'd write it differently.