refactoring

A set of rules for making existing code easier to understand and maintain through structured refactoring.

In plain words
What is it for?
Use it when reorganising code, applying guard clauses and early returns, or moving repeated logic into separate functions.
Why use it?
It gives developers a checklist for preparing, testing, and making common code changes such as simplifying nested conditions or extracting functions.

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/cursor-doctor/refactoring
Clone the repo
git clone --depth 1 https://github.com/nedcodes-ok/cursor-doctor
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 2d ago 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 2d ago.

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.

pro-kit/templates/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. 2d ago 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/cursor-doctor (9 stars, last pushed 5mo 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-08-31.