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.
npx agentmods add skills/cosmicstack-labs/mercury-agent-skills/refactoring-patternsnpx skills add cosmicstack-labs/mercury-agent-skills --skill refactoring-patternsgit clone --depth 1 https://github.com/cosmicstack-labs/mercury-agent-skillsWrote 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.
[](https://agentmods.dev/skills/cosmicstack-labs/mercury-agent-skills/refactoring-patterns)<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>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.
| Model | Per session | Once 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 |
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.
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
- Identify a code smell or pain point
- Ensure you have tests (write them first if not)
- Apply one refactoring at a time
- Test after each change (green = continue, red = revert)
- Commit — small, focused, descriptive
- 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
- Characterize with tests: Write tests that capture current behavior
- Sprout method: Add new code in new methods, don't modify old
- Wrap method: Wrap entire methods with new behavior (logging, caching)
- Step-by-step: Extract small pieces over time
- 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 |
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.
- 2d ago First seen · 83 lines · 21 tokens per session scan A 518f672bc709
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.
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.
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…
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…
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…
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.
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.