Home › Agent workflows › Safe refactoring rules

Safe refactoring rules

Cursor rules for behavior-preserving refactors: small steps, tests first, no mixed feature changes, and clear diffs.

CursorAgent workflows

How to use

Save this as .cursor/rules/refactoring.mdc in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

.cursor/rules/refactoring.mdcDownload  
---
description: Use when the user asks to refactor, clean up, restructure, rename, or extract code.
alwaysApply: false
---

# Refactoring

A refactor changes structure, not behavior.

## Before
- Find the tests covering the code. If coverage is thin, add characterization tests that pin current behavior (including odd behavior) first.
- Run the suite and note the result, so you know the starting point.
- State the plan in 3-6 steps and the end state.

## During
- One kind of change per step: rename, extract function, move file, inline, change signature. Run tests after each step.
- Use the editor/language tooling for renames and moves so all references update; then search (`rg 'oldName'`) for strings, docs, and configs.
- Keep public APIs stable. If a signature must change, add the new one, migrate callers, then delete the old one.
- Don't mix in bug fixes, new features, formatting of untouched files, or dependency upgrades. Note them as follow-ups.
- Delete dead code you've proven unused; don't comment it out.

## Smells worth fixing
Long functions, deep nesting (use early returns), duplicated logic, boolean flag parameters, large modules mixing concerns, unclear names.

## After
- Tests, typecheck, and lint pass with no new warnings.
- Summarize what moved where and confirm "no behavior change", or list any intentional change explicitly.

More: all Agent workflows rules · all Cursor rules

Related rules

Code review Copilot instructions

Copilot instructions focused on pull request review: what to flag, severity labels, and how to word comments.

GitHub CopilotAgent workflows

Code review prompt file (Copilot)

A Copilot prompt file (/review-changes) that reviews the current changes and reports issues by severity without editing.

GitHub CopilotAgent workflows

Code reviewer subagent (Claude Code)

A Claude Code subagent definition that reviews the current diff for bugs, security, and tests, reporting by severity.

Claude Code (CLAUDE.md)Agent workflows

/commit slash command (Claude Code)

A Claude Code /commit slash command that reviews staged changes and writes a Conventional Commits message.

Claude Code (CLAUDE.md)Agent workflows

Context management for long agent sessions CLAUDE.md

CLAUDE.md practices for keeping Claude Code effective in long sessions: lean memory, notes files, subagents, and resets.

Claude Code (CLAUDE.md)Agent workflows

Git commits & PRs AGENTS.md

AGENTS.md rules for how agents commit: Conventional Commits, atomic changes, safe git commands, and PR descriptions.

AGENTS.mdAgent workflows

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.