refactoring-patterns

refactoring-patterns is a skill for Claude Code, Codex from cosmicstack-labs/mercury-agent-skills. It costs 21 tokens per session (645 once invoked), scanned A, original, MIT.

A guide to improving existing code without changing what it does. Refactoring means making code clearer or easier to maintain while preserving its behavior.

In plain words
What is it for?
Use it to split large methods, replace growing conditional logic, extract reusable patterns, and safely update legacy code.
Why use it?
It provides a repeatable way to remove code smells and modernize older code while using tests to catch accidental behavior changes.

Skill for Claude CodeCodex

Written for no agent in particular: nothing here depends on one.

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 skills/cosmicstack-labs/mercury-agent-skills/refactoring-patterns
Any agent
npx skills add cosmicstack-labs/mercury-agent-skills --skill refactoring-patterns
Clone the repo
git clone --depth 1 https://github.com/cosmicstack-labs/mercury-agent-skills

Made for: Claude Code, Codex.

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

README.md
[![agentmods](https://agentmods.dev/badge/skills/cosmicstack-labs/mercury-agent-skills/refactoring-patterns.svg)](https://agentmods.dev/skills/cosmicstack-labs/mercury-agent-skills/refactoring-patterns)
Your own site
<a href="https://agentmods.dev/skills/cosmicstack-labs/mercury-agent-skills/refactoring-patterns"><img src="https://agentmods.dev/badge/skills/cosmicstack-labs/mercury-agent-skills/refactoring-patterns.svg" alt="Measured on agentmods" height="20"></a>
Per session 21 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 645 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.1 $0.00021 $0.00645
Opus 5 $0.00010 $0.00322
Sonnet 5 $0.00004 $0.00129
Haiku 4.5 $0.00002 $0.00064

Measured 2d ago against content hash 518f672bc709, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-06, 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.

categories/development/refactoring-patterns/SKILL.md · 83 lines

How it starts

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

Refactoring Patterns

Systematically improve code without changing its behavior.

The Refactoring Workflow

  1. Identify a code smell or pain point
  2. Ensure you have tests (write them first if not)
  3. Apply one refactoring at a time
  4. Test after each change (green = continue, red = revert)
  5. Commit — small, focused, descriptive
  6. Repeat

Extract Method

When: A method does multiple things or is too long. How: Find a cohesive block → extract to new method → call it.

// Before
function processOrder(order) {
  const total = order.items.reduce((sum, i) => sum + i.price, 0);
  const tax = total * 0.08;
  const discount = order.coupon ? total * 0.1 : 0;
  return total + tax - discount;
}

// After
function processOrder(order) {
  const subtotal = calculateSubtotal(order);
  const tax = calculateTax(subtotal);
  const discount = calculateDiscount(order, subtotal);
  return subtotal + tax - discount;
}

Replace Conditional with Polymorphism

When: Switch/if-else chains based on type grow too long.

// Before
function calculateShipping(order) {
  if (order.type === 'standard') return order.weight * 0.5;
  if (order.type === 'express') return order.weight * 1.5 + 5;
  if (order.type === 'overnight') return order.weight * 3 + 15;
}

// After
class StandardShipping {
  calculate(weight) { return weight * 0.5; }
}
class ExpressShipping {
  calculate(weight) { return weight * 1.5 + 5; }
}

Legacy Code Strategy

  1. Characterize with tests: Write tests that capture current behavior
  2. Sprout method: Add new code in new methods, don't modify old
  3. Wrap method: Wrap entire methods with new behavior (logging, caching)
  4. Step-by-step: Extract small pieces over time
  5. Strangler pattern: New code replaces old incrementally, old gets retired

Common Refactorings

Refactoring When Risk
Rename Variable Unclear name Low
Extract Method Long function Low
Replace Magic Literal Hard-coded values Low
Introduce Parameter Object Many parameters Medium
Replace Temp with Query Reused expressions Low
Decompose Conditional Complex condition Medium
Extract Class Class doing too much Medium-High

Read the full file on GitHub · 83 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 · 83 lines · 21 tokens per session scan A 518f672bc709

Subscribe to this mod's changes

refactoring-patterns is a skill published in the GitHub repository cosmicstack-labs/mercury-agent-skills (471 stars, last pushed 11d ago), licensed MIT. It adds 21 tokens to every session and 645 once invoked, about $0.0001 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.

Related

Other skills, from other repositories

autonomous-tdd-debugger

Empowers the agent to autonomously run tests, read terminal stack traces, and self-heal code until tests pass. Transforms the agent from a passive coder to an active CI pipeline debugger.

roedyrustam/vibes-plug · 46 tokens

test-first-bugfix

Test-driven bug fixing — reproduce before you fix. Use this skill whenever the user reports a bug, describes unexpected behavior, says something is broken, mentions a regression, or asks you to fix an error. This includes phrases like "this is broken", "X doesn't work", "there's a bug in", "getting an error when", "it…

The-Artificer-of-Ciphers-LLC/skills-from-the-artificer · 138 tokens

pair-programming

AI-assisted pair programming with multiple modes (driver/navigator/switch), real-time verification, quality monitoring, and comprehensive testing. Supports TDD, debugging, refactoring, and learning sessions. Features automatic role switching, continuous code review, security scanning, and performance optimization with…

frankxai/claude-skills-library · 85 tokens

fix-bug

Guide for fixing bugs in ClaudeBar following Chicago School TDD and rich domain design. Use this skill when: (1) User reports a bug or unexpected behavior (2) Fixing a defect in existing functionality (3) User asks "fix this bug" or "this doesn't work correctly" (4) Correcting behavior that violates the user's mental…

tddworks/SkillsManager · 78 tokens

triage-issue

Triage a bug or issue by exploring the codebase to find root cause, then create a GitHub issue with a TDD-based fix plan. Use when user reports a bug, wants to file an issue, mentions "triage", or wants to investigate and plan a fix for a problem.

qhuang20/shazam · 65 tokens

test-driven-development

Drives development with tests. Use when implementing any logic, fixing any bug, or changing any behavior. Use when you need to prove that code works, when a bug report arrives, or when you're about to modify existing functionality.

addyosmani/agent-skills · 50 tokens