# Refactoring

> Restructure code without changing behavior — improve readability, maintainability, and performance.

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

---


# Refactoring

## Golden Rule
**Never refactor and add features at the same time.** Two separate changes, two separate commits/PRs.

## Process
1. **Ensure tests exist** before touching code. Add characterization tests if needed
2. **Identify the smell** — long function, duplicate code, god class, shotgun surgery, primitive obsession
3. **Apply one refactoring at a time** — extract, rename, move, inline
4. **Run tests after each step** — if tests break, revert the last change
5. **Commit frequently** — each atomic refactoring is one commit

## Common Refactorings
- **Extract Method** — turn a code block into a named function
- **Rename** — give variables, functions, and classes meaningful names
- **Replace Conditionals with Polymorphism** — if/else chains to strategy pattern
- **Introduce Parameter Object** — groups of related params into a single object
- **Decompose Conditional** — complex if/else into separate functions
- **Replace Magic Number with Constant**
- **Separate Query from Modifier**

## Signs You're Done
- Same behavior, same output
- Fewer lines of code
- Better names
- Clearer structure
- Tests still pass

