refactoring-patterns

A catalogue of ways to improve code structure without changing what the code does, organized around common code problems such as duplication or overly long methods.

In plain words
What is it for?
Use it to break up large methods, remove duplicate logic, move responsibilities, simplify conditions, and address other listed code smells.
Why use it?
It helps identify why code is difficult to maintain and choose a specific structural improvement instead of making broad changes.

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/dasomel/oh-my-cursor/refactoring-patterns
Clone the repo
git clone --depth 1 https://github.com/dasomel/oh-my-cursor
Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 817 The whole file, excluding the scripts and references it only reads on demand.
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.00000 $0.00817
Opus 5 $0.00000 $0.00409
Sonnet 5 $0.00000 $0.00163
Haiku 4.5 $0.00000 $0.00082

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

Security

Grade A, and why

refactoring-patterns 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.

rules/practices/refactoring-patterns.mdc · 94 lines

How it starts

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

Refactoring Patterns

A catalog of refactoring patterns organized by category. Apply these when improving code structure without changing behavior.

1. Code Smells (Triggers for Refactoring)

Smell Symptom Primary Fix
Long Method Function > 20 lines Extract Method
Duplicate Code Same logic in 2+ places Extract Method / Extract Class
Feature Envy Method uses another class's data more than its own Move Method
Shotgun Surgery One change requires editing many files Move Method / Inline Class
Divergent Change One class modified for many different reasons Extract Class
Primitive Obsession Using primitives instead of domain types Replace Data Value with Object
Long Parameter List Function takes > 4 parameters Introduce Parameter Object
Data Clumps Same group of data appears together repeatedly Extract Class

2. Composing Methods

Break large methods into smaller, intention-revealing pieces:

  • Extract Method — Move code fragment into a named method
  • Inline Method — Replace a method call with the method's body (when name adds nothing)
  • Replace Temp with Query — Extract expression into a method, replace temp variable
  • Split Temporary Variable — One variable for one purpose
  • Replace Method with Method Object — Turn long method into its own class

3. Moving Features Between Objects

Redistribute responsibilities:

  • Move Method — Method uses more features of another class
  • Move Field — Field used more by another class
  • Extract Class — Class doing work of two → split it
  • Inline Class — Class doing almost nothing → merge it
  • Hide Delegate — Client calls through a chain → add wrapper
  • Remove Middle Man — Too many delegating methods → expose delegate

4. Organizing Data

  • Replace Magic Number with Constant — Named constants over raw numbers
  • Replace Data Value with Object — Primitive → domain type
  • Encapsulate Field — Public field → getter/setter
  • Encapsulate Collection — Return unmodifiable view
  • Replace Type Code with Subclass/Strategy — Type codes → polymorphism

Read the full file on GitHub · 94 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 · 94 lines · 0 tokens per session scan A cbec4c57674e

Subscribe to this mod's changes

refactoring-patterns is a cursor rule published in the GitHub repository dasomel/oh-my-cursor (2 stars, last pushed 5mo ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 817 tokens. 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.