# Refactor

> Refactor legacy code toward a stated goal, with safety. Use when improving readability, adopting patterns, or reducing coupling while preserving behavior.

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

---


Use this template to refactor legacy code toward a stated goal, with safety.

## INPUTS

- PROJECT_CONTEXT
- LEGACY_CODE
- REFACTOR_GOAL // e.g., improve readability, adopt a pattern, reduce coupling
- (optional) CONSTRAINTS

## INSTRUCTIONS

1. Restate REFACTOR_GOAL and constraints. Preserve behavior.
2. Propose a target design (patterns, boundaries, contracts).
3. Provide a refactored version or representative slices.
4. Explain rationale trade-offs (perf, readability, testability).
5. Add safety checks: tests, metrics, and rollout steps.
6. Show an incremental plan of small commits.
7. Keep lines ≤80 chars.

## OUTPUT FORMAT

### Goal & constraints

- **Goal:**
- **Constraints:**

### Diagnosis

- **Smells:**
- **Risks:**

### Target design

- **Patterns:**
- **Module boundaries:**
- **Public interfaces:**

### Refactored code (slice)

```diff
- old
+ new
```

### Rationale

- **Why this is better:**
- **Alternatives considered:**

### Safety & verification

- **Regression tests:**
- **Contracts/property checks:**
- **Perf baselines:**
- **Observability:**

### Incremental plan

1. ...
2. ...

### Backout plan

- **How to revert safely:**

### Follow-ups

- ...

