refactor

A set of instructions for the refactoring stage of test-driven development (TDD), where tests guide code changes. It requires improving the code while keeping the existing tests passing and recording the reasoning.

In plain words
What is it for?
Use it after tests pass to improve names, structure, and simplicity, while comparing the code before and after the changes.
Why use it?
It helps clean up code without losing behavior that the tests protect.

Command for Claude Code

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 commands/marcoemrich/agentic_coding_lab/refactor
Clone the repo
git clone --depth 1 https://github.com/marcoemrich/agentic_coding_lab

Made for: Claude Code.

Per session 0 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 1,471 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.01471
Opus 5 $0.00000 $0.00736
Sonnet 5 $0.00000 $0.00294
Haiku 4.5 $0.00000 $0.00147

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

Security

Grade A, and why

refactor 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.

experiments/runs/2026-05-03_04-15-52_mars-rover-prose_v5-exact-single-context_haiku-4-5/.claude/commands/refactor.md · 212 lines

How it starts

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

TDD Refactor Phase

You are now in the Refactor Phase of TDD. Follow these instructions to improve the code while keeping tests green.

Your Mission

  1. MUST attempt at least one refactoring - mandatory, not optional
  2. Apply Kent Beck's Four Rules of Simple Design (priority order)
  3. Use Absolute Priority Premise (APP) to measure improvements
  4. Keep all tests green throughout
  5. Document decisions and mass calculations
  6. If no improvement possible, explicitly document why

Context: $ARGUMENTS

Refactor Phase Rules

  • Mandatory refactoring attempt: MUST try at least one improvement
  • Tests must stay green: Never break passing tests
  • Apply Simple Design Rules: In priority order (1 → 2 → 3 → 4)
  • Calculate APP mass: Before and after refactoring
  • Document decisions: Explain improvements or why none were possible
  • Naming is first priority: Evaluate if function name still fits

Simple Design Rules (Priority Order)

Rule 1: Tests Pass (Highest Priority)

  • All tests must pass before and after refactoring
  • If tests fail, revert and try different approach

Rule 2: Reveals Intent

  • Use meaningful names for variables, functions, classes
  • Structure code to be self-documenting
  • Prefer explicit over clever code
  • Naming Evaluation (FIRST):
    • Does this name clearly describe what the function does?
    • Has the function's purpose become clearer through the latest test?
    • Rename if the name doesn't capture the current full intent

Rule 3: No Duplication (DRY)

  • Extract common functionality
  • Look for obvious and conceptual duplication
  • Balance with Rule 2: If DRY hurts clarity, choose clarity

Rule 4: Fewest Elements (Lowest Priority)

  • Remove unnecessary abstractions
  • Keep it simple - don't over-engineer
  • Only add complexity when it serves clear purpose

Absolute Priority Premise (APP)

Mass Calculation

Total Mass = (constants × 1) + (bindings × 1) + (invocations × 2) +
             (conditionals × 4) + (loops × 5) + (assignments × 6)

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

Subscribe to this mod's changes

refactor is a command published in the GitHub repository marcoemrich/agentic_coding_lab (11 stars, last pushed 15d ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 1,471 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-30.