refactoring

A code-refactoring command improves the structure and readability of existing code while keeping its behavior. Refactoring can include splitting large methods or classes, removing duplication, and improving names.

In plain words
What is it for?
Use it to clean up selected files or classes, simplify their design, apply listed design patterns, and verify the resulting code.
Why use it?
It gives a structured way to reduce technical debt—code that makes future changes harder—while checking that tests and quality checks still pass.

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/kewton/commandmate/refactoring
Clone the repo
git clone --depth 1 https://github.com/Kewton/CommandMate

Made for: Claude Code.

Per session 20 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 825 The whole file, excluding the scripts and references it only reads on demand.
Security scan B 1 finding. 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.00020 $0.00825
Opus 5 $0.00010 $0.00413
Sonnet 5 $0.00004 $0.00165
Haiku 4.5 $0.00002 $0.00082

Measured yesterday against content hash 5144a0a8319c, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade B, and why

refactoring scanned grade B with 1 finding 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 yesterday.

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.

Reads agent configuration directoriesmediumAgent snooping

.claude/, .codex/, .gemini/ hold keys, settings and other credentials a mod has no legitimate need for.

cat .claude/prompts/refactoring-core.md
.claude/commands/refactoring.md · 120 lines

What it actually says

リファクタリングスキル

概要

コード品質を改善し、SOLID原則に基づく設計パターンを適用して、技術的負債を解消するスキルです。

このスキルはスラッシュコマンドモードで動作します(ユーザーが直接実行)。

使用方法

  • /refactoring [対象ファイル/クラス]
  • 「[ファイル名]をリファクタリングしてください」
  • 「コンポーネントを分割してください」

実行内容

共通プロンプトを読み込んで実行します:

cat .claude/prompts/refactoring-core.md

このプロンプトの内容に従って、リファクタリングを実行してください。


動作モード

スラッシュコマンドモード:

  • ユーザーから対話的に情報を取得
  • リファクタリング対象、改善目標、適用パターンなどを確認
  • コード品質分析を実行
  • リファクタリングを段階的に適用
  • 結果をターミナルに表示

リファクタリング原則

  • SOLID: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, Dependency Inversion
  • KISS: Keep It Simple, Stupid
  • DRY: Don't Repeat Yourself
  • YAGNI: You Aren't Gonna Need It

リファクタリングパターン

メソッド抽出

長いメソッドを小さなメソッドに分割

クラス抽出

大きなクラスを責任ごとに分割

重複コード削除

共通処理を関数/クラスに抽出

命名改善

意図を明確に表す命名に変更

設計パターン適用

  • Repository Pattern
  • Factory Pattern
  • Strategy Pattern
  • Dependency Injection

完了条件

以下をすべて満たすこと:

  • すべてのテストが引き続き成功
  • 品質メトリクスが改善(カバレッジ、複雑度)
  • 静的解析エラーがゼロ
  • コミットが完了

出力例

リファクタリング完了

## リファクタリング内容
- 長いメソッドの分割
- 重複コードの削除
- 命名の改善

## 品質メトリクス改善
| 指標 | Before | After | 改善 |
|------|--------|-------|------|
| Coverage | 75.0% | 85.0% | +10.0% |
| ESLint errors | 5 | 0 | -5 |
| TypeScript errors | 2 | 0 | -2 |

## ファイル変更
- src/lib/xxx.ts (リファクタリング)
- src/utils/helper.ts (新規)

## Commits
- abc1234: refactor(xxx): improve code quality

サブエージェントモード

サブエージェントとして呼び出す場合は、PM Auto-Devが以下のように実行します:

Use refactoring-agent to improve code quality for Issue #166.

この場合、.claude/agents/refactoring-agent.md が使用されます。

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. yesterday First seen · 120 lines · 20 tokens per session scan B 5144a0a8319c

Subscribe to this mod's changes

refactoring is a command published in the GitHub repository Kewton/CommandMate (39 stars, last pushed yesterday), licensed MIT. It adds 20 tokens to every session and 825 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it B with 1 finding (reads agent configuration directories). No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.