refactoring

refactoring is a cursor rule for coding agents from nedcodes-ok/cursorrules-collection. It costs 683 tokens per session, scanned A, original, MIT.

A set of rules for improving existing code without changing what it does. It recommends tests, small focused changes, guard clauses, and clearer function structure.

In plain words
What is it for?
Use it to simplify nested conditions, split large functions, remove duplication, and organize refactoring work safely.
Why use it?
It reduces the risk of breaking behavior while making difficult-to-read code easier to maintain.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/nedcodes-ok/cursorrules-collection/refactoring
Clone the repo
git clone --depth 1 https://github.com/nedcodes-ok/cursorrules-collection

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for refactoring

README.md
[![agentmods](https://agentmods.dev/badge/rules/nedcodes-ok/cursorrules-collection/refactoring.svg)](https://agentmods.dev/rules/nedcodes-ok/cursorrules-collection/refactoring)
Your own site
<a href="https://agentmods.dev/rules/nedcodes-ok/cursorrules-collection/refactoring"><img src="https://agentmods.dev/badge/rules/nedcodes-ok/cursorrules-collection/refactoring.svg" alt="Measured on agentmods" height="20"></a>
Per session 683 This file is loaded in full into every session.
When invoked 683 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00683 $0.00683
Opus 5 $0.00342 $0.00342
Sonnet 5 $0.00137 $0.00137
Haiku 4.5 $0.00068 $0.00068

Measured yesterday against content hash c5c297793cbe, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

refactoring scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

Origin

Copies of this mod

1 near-identical copy found in the catalogue:

rules-mdc/practices/refactoring.mdc · 80 lines

How it starts

The opening of the file, as written. The whole thing — 80 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Refactoring Cursor Rules

You are an expert in systematic code refactoring. Follow these rules:

Before Refactoring

  • Ensure tests exist for the code being refactored — no tests, no refactoring
  • Run the full test suite before starting — establish a green baseline
  • Commit the current state before making any changes
  • Identify the specific code smell you're fixing — don't refactor "just because"

Guard Clauses & Early Returns

  • Replace nested if/else with early returns for error cases
  • Check preconditions at the top of the function, return/throw early
  • Each guard clause should handle one condition — not compound boolean expressions
  • After all guards pass, the main logic runs at the base indentation level
// ❌ Nested
function process(user) {
  if (user) {
    if (user.isActive) {
      if (user.hasPermission) {
        // actual logic buried 3 levels deep
      }
    }
  }
}

// ✅ Guard clauses
function process(user) {
  if (!user) throw new Error('User required');
  if (!user.isActive) return;
  if (!user.hasPermission) throw new ForbiddenError();
  // actual logic at base level
}

Extract Function/Method

  • Extract when a code block needs a comment explaining what it does — the function name IS the comment
  • Extracted functions should do one thing and be nameable with a verb phrase
  • Pass only what the extracted function needs — not the entire context/object
  • If you extract and the original function is just a list of calls, that's good — it reads like a plan

Inline

  • Inline when a function/variable adds indirection without clarity
  • Inline temporary variables that are used once and the expression is clear
  • Inline trivial delegating functions that just call another function

Rename

  • Rename when the name doesn't match what the code does (after behavior changes)
  • Use find-all-references, not find-and-replace — catch all usages including types
  • Rename in a separate commit from behavior changes

Simplify Conditionals

  • Replace complex boolean expressions with named boolean variables or functions
  • Use De Morgan's laws to simplify negated compound conditions
  • Replace switch/case with object lookup when mapping values
  • Replace type-checking conditionals with polymorphism when the pattern repeats

Read the full file on GitHub · 80 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 80 lines · 683 tokens per session scan A c5c297793cbe

Subscribe to this mod's changes

refactoring is a cursor rule published in the GitHub repository nedcodes-ok/cursorrules-collection (37 stars, last pushed 6mo ago), licensed MIT. It adds 683 tokens to every session, about $0.0034 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-03.