Skip to content
Development
Agent

refactor-plan

A realistic, step-by-step plan for refactoring one overgrown function. Every step is behavior-preserving and gated by a green test run.

From plugin
skills
954 skills54 agents

Claude Code

$ npx -y skills add vanara-agents/skills --agent claude-code

Agent definition

refactor-plan.md

Worked Refactor Plan — Untangling `processCheckout`

A realistic, step-by-step plan for refactoring one overgrown function. Every step is behavior-preserving and gated by a green test run.

Starting point

function processCheckout(cart, user, coupon) {
  let total = 0;
  for (const item of cart.items) {
    total += item.price * item.qty;
  }
  if (coupon) {
    if (coupon.type === "percent") {
      total = total - (total * coupon.value) / 100;
    } else if (coupon.type === "flat") {
      total = total - coupon.value;
    }
  }
  if (user.tier === "gold") {
    total = total * 0.9;
  }
  if (total < 0) total = 0;
  db.orders.insert({ userId: user.id, total });
  email.send(user.email, "Order confirmed", `You paid ${total}`);
  return total;
}

Smells: long function mixing calculation + persistence + notification; nested coupon conditional; magic numbers (`0.9`); the pricing math is untested and hard to test because of side effects.

Step 0 — Safety net

The function has no unit tests, only a flaky end-to-end test. **Add characterization tests** for the pure pricing outcomes first:

test("subtotal with no coupon, standard tier", () => {
  expect(priceOf(cart([{price:10,qty:2}]), user("standard"), null)).toBe(20);
});
test("percent coupon then gold discount", () => {
  expect(priceOf(cart([{price:100,qty:1}]), user("gold"), {type:"percent",value:10})).toBe(81);
});
test("never goes negative", () => {
  expect(priceOf(cart([{price:5,qty:1}]), user("standard"), {type:"flat",value:50})).toBe(0);
});

These pin *current* behavior (including the `81` that results from applying percent then gold). Run → GREEN. (They reference a `priceOf` we will extract in Step 2; write them against the current function first, then point them at the extraction.)

Step 1 — Guard clause for the negative-total clamp

Pull the clamp to a named helper. Run tests → GREEN.

const clampNonNegative = (n) => (n < 0 ? 0 : n);

Step 2 — Extract the pure pricing function

Move all calculation into a side-effect-free `priceOf(cart, user, coupon)`; `processCheckout` calls it. Now the characterization tests target `priceOf` directly. Run tests → GREEN.

Step 3 — Replace nested coupon conditional with a lookup

const COUPON = {
  percent: (t, c) => t - (t * c.value) / 100,
  flat:    (t, c) => t - c.value,
};
const applyCoupon = (t, c) => (c && COUPON[c.type] ? COUPON[c.type](t, c) : t);

Run tests → GREEN.

Step 4 — Name the magic number

const GOLD_DISCOUNT = 0.9; // 10% off for gold tier

Run tests → GREEN.

Step 5 — Separate side effects

`processCheckout` keeps the `db.orders.insert` and `email.send`; the testable pricing now lives in `priceOf`. Persistence/notification stay as the orchestration shell. Run tests → GREEN.

Result

`priceOf` is pure and fully tested; the conditional is flat; the magic number is named; side effects are isolated. **No behavior changed** — every characterization test that was green at Step 0 is still green. Each step was a separate, revertible commit.

Read more
@vanara-agents
Ships withskills

🐒 Free agents, skills & packs for Claude Code One subscription. An army of Claude Code agents. 30 production-grade agents, skills, and packs for Claude Code — free, Apache-2.0, install with one command.

917 skills54 agents
Get the whole plugin

Stars on GitHub

Momentum

stars in total
9
Star history of vanara-agents/skills on GitHub, a chart by star-history.com

Other agents on skills.

AGENT

AGENT

Agent

Use when designing a new HTTP/GraphQL API or changing an existing one — modeling resources, defining endpoint contracts, choosing…

review-notes

review-notes

Agent

This shows how the api-designer agent reviews a flawed draft. Findings are severity-ranked so the implementer fixes the…

contract-and-openapi

contract-and-openapi

Agent

The contract is the deliverable. Express it as an **OpenAPI 3.1** document so it is human-readable *and* machine-checkable. This…

design-checklist

design-checklist

Agent

Run through this before declaring an API contract done. It is ordered the way you should *design*: resources first, cross-cutting…

pr-comment-template

pr-comment-template

Agent

Copy-paste templates for leaving review comments. Keep each comment to one finding: an anchor, the problem, and the fix.

More agents from other plugins.

a11y-architect
Auto-invoked

a11y-architect

Agent

Accessibility Architect specializing in WCAG 2.2 compliance for Web and Native platforms. Use PROACTIVELY when designing UI…

ecc2w293k
@affaan-m@affaan-mInstall Agent
analyze-pass-impact

analyze-pass-impact

Agent

Analyzes how a specific topic affects a group of compiler passes. Used by the /plan-update skill to parallelize research across…

react5d252k
@facebook@facebookInstall Agent
orchestrator

orchestrator

Agent

- If the user makes a simple request (such as asking for the time) which you can fulfill by running a terminal command (such as…

codex5d135k
@openai@openaiInstall Agent
cavecrew-builder

cavecrew-builder

Agent

Surgical 1-2 file edit. Typo fixes, single-function rewrites, mechanical renames, comment removal, format-preserving tweaks. Hard…

caveman6d119k
@juliusbrussee@juliusbrusseeInstall Agent

Questions about refactor-plan.

Which coding agents can use refactor-plan?
refactor-plan installs in Claude Code and Cursor: each loads subagents from .claude/agents, so copy the file there. Flowy's auto-invocation runs in Claude Code only.