refactor-function

A command that simplifies a selected function by measuring its complexity and applying common code-organization changes.

In plain words
What is it for?
Use it to split large functions into helpers, reduce nesting, replace long condition chains, and move repeated text into constants.
Why use it?
It reduces deeply nested or hard-to-maintain code and shows the proposed changes before they are applied.

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/doobidoo/mcp-memory-service/refactor-function
Clone the repo
git clone --depth 1 https://github.com/doobidoo/mcp-memory-service

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,588 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.01588
Opus 5 $0.00000 $0.00794
Sonnet 5 $0.00000 $0.00318
Haiku 4.5 $0.00000 $0.00159

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

Security

Grade A, and why

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

.claude/commands/refactor-function.md · 273 lines

How it starts

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

/refactor-function Command

Automated function complexity reduction using multi-agent workflow.

Usage

# In editor, select a function and run:
/refactor-function

# With options:
/refactor-function --target-complexity 6 --max-helpers 3
/refactor-function --pattern extract-method
/refactor-function --dry-run  # Show plan without applying

Workflow

1. Context Extraction

  • Detect selected function or function at cursor
  • Extract function signature, body, surrounding context
  • Parse imports and dependencies

2. Baseline Analysis (code-quality-guard)

- Measure current complexity
- Identify nesting depth
- Count boolean operators
- Calculate maintainability index
- Generate refactoring targets

3. Refactoring (amp-bridge)

- Apply Extract Method pattern
- Create helper functions
- Reduce nesting with early returns
- Replace if-elif chains with dictionaries
- Extract magic strings to constants

4. Validation (code-quality-guard)

- Measure new complexity
- Verify targets met
- Check for regressions
- Generate diff report

5. User Review

Before:
  Function: parse_output
  Complexity: 12 (C-grade)
  Nesting: 5 levels

After:
  Main: parse_output (C=6, B-grade)
  Helpers:
    - _parse_field (C=4, A-grade)
    - _validate_output (C=3, A-grade)

Apply changes? [y/n]

6. Optional Commit

If applied, offer to commit:
  "refactor: Reduce complexity in <function_name>

  - Complexity: C=12→6 (-50%)
  - Extracted N helpers (avg C=X)
  - Pattern: Extract Method"

Options

Option Description Default
--target-complexity Target cyclomatic complexity 6
--max-helpers Max helper functions to create 5
--pattern Refactoring pattern extract-method
--dry-run Show plan without applying false
--auto-commit Commit if successful false
--scope Scope (function|class|file) function

Patterns Supported

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

Subscribe to this mod's changes

refactor-function is a command published in the GitHub repository doobidoo/mcp-memory-service (1,919 stars, last pushed 4d ago), licensed Apache-2.0. It costs nothing until one of its globs matches a file; then it loads 1,588 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.