dry-refactoring
できること
このガイドは、重複したコードを見つけて、1つの共有版にまとめる段階的な方法を示します。繰り返されるロジックのソースを1つに保ちたい開発者向けです。
使う場面
- ✓開発者が、同じコードがいくつかの場所に貼り付けられているのを見つけ、それをまとめたいとき。
- ✓チームが、急いだコピー&ペーストの修正の後で、重複を取り除きたいとき。
- ✓開発者が、どの近いコピーが本当の重複で、どれが見た目だけ似ているのかを区別する必要があるとき。
スキルのリンク
https://github.com/kucherenko/jscpd/blob/master/skills/dry-refactoring/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“このプロジェクトでコピー&ペーストされたコードを見つけ、最も悪い繰り返しを共有の関数にまとめてください。”
毎週5つの新しいスキル。1分で読めます。
仕組み
このスキルは、まず重複検出ツールを実行し、その後、近いコピーを見る前に完全に同一のコピーを修正するようAIを導きます。関数や定数の抽出などの方法を使います。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)をGitHubからそのまま表示しています。
dry-refactoring
Guided workflow to eliminate copy-paste duplication in source code. Use after running jscpd to detect clones.
Prerequisites
First, run jscpd to identify duplications:
npx jscpd --reporters ai <path>
In codebases that mix related formats (e.g. JavaScript and TypeScript), add --cross-formats so clones spanning both are detected too:
npx jscpd --reporters ai --cross-formats "js-ts" <path>
On larger codebases, add --summary to get a refactoring-hotspot overview alongside the clone list — top files and folders with a dup% column showing how much of each file is duplicated:
npx jscpd --reporters ai --summary <path>
The default scan reports only exact copies, and those are the ones to refactor first: an exact clone is almost always a real copy-paste. Two more passes find copies that were edited after pasting. They are noisier: they ignore names, values or a few statements on purpose, so they also surface blocks that merely look alike (models and DTOs, config tables, test setup, generated code, shared idioms). Run them only after the exact clones are dealt with, one family at a time, with tight settings, and treat what they report as leads to read rather than defects to fix:
# Type-2: renamed copies (other variable names, other constants), reported as "(renamed)".
# Raise --min-tokens: with identifiers ignored, a short block is mostly placeholders.
npx jscpd --reporters ai --ignore-identifiers --min-tokens 70 <path>
# Type-3: near-miss copies (one or two edited lines, or JS/TS/Python functions with the same structure),
# reported as "[~0.91 gap]" and "[~0.85 ast]". Widen only if the tight run finds nothing.
npx jscpd --reporters ai --max-gap-lines 1 --similarity <path>
See the jscpd skill for full option reference, including cross-format group syntax, the clone-kind suffixes and how to read the summary.
Workflow
- Run jscpd with
--reporters aion the target path (add--summaryon larger codebases to pick a starting point: files with highdup%and high token counts pay off most) - Parse each clone line to identify the two duplicated locations (file + line range) and its kind: no suffix is an exact copy,
(renamed)differs only in names or values,[~N gap]has a few edited lines in the middle,[~N ast]is a function pair with the same structure - Read both code fragments from the source files
- Understand what the duplicated code does, and for renamed and similar clones list exactly what differs between the two sides
- Triage renamed and similar clones before touching them. Skip the pair, and say so, when any of these holds: the two sides do different things despite the same shape (a
switchover different enums, two reducers with unrelated semantics); the sameness is intentional boilerplate (models, DTOs, config, route tables, test fixtures); the code is generated; a shared abstraction would need a vague name likeprocessData; or the pair is under about 10 lines. Only a pair that would let you delete code and give the extraction a precise name goes on to the next step - Design a refactoring: extract a shared function, class, module, or constant; the kind decides the strategy (below)
- Apply the refactoring — update both locations and all other usages
- Re-run jscpd with the same flags to confirm the clone is eliminated and the
dup%of the touched files went down; a clone that was(renamed)will not show in a default run, so check with--ignore-identifiersagain - Repeat for remaining clones, highest-impact first: exact clones, then renamed, then similar. Report the skipped candidates separately from the refactored ones, with the reason, so nobody mistakes a normalized run's count for real duplication
Refactoring Strategies
Extract function — when the duplicate is a block of logic:
// Before: same block in two places
// After: shared function called from both places
Extract module/utility — when the duplicate spans multiple files in different domains:
// Move shared logic to a shared utility file and import it
Extract constant or config — when the duplicate is repeated data or configuration.
Template/base class — when the duplicate is structural (e.g., repeated class shape).
Parameterize — for (renamed) clones. The two sides are the same algorithm over different names or values, so the things that
よくある質問
なぜ完全に同一のコピーを先に直すのですか?
完全に同一のコピーはほぼ常に本当の重複なので、最も安全に始められる場所だからです。
近いコピーは常に重複ですか?
いいえ。スキルは、近いコピーはモデル、テストの設定、生成されたコードであり、見た目だけ似ている場合があると警告しています。
重複を見つけるツールはどれですか?
jscpdの重複検出ツールを使います。
詳細
作者による元の説明
Guided workflow to eliminate copy-paste duplication detected by jscpd. Refactor exact, renamed and near-miss clones using extract function, parameterize, module, constant, or base class strategies, starting from the hotspots the summary ranks.
repo: kucherenko/jscpd
人気
- GitHubのスター
- 6.5k
- ランキング
- #14190
- ライセンス
- MIT
- 更新日
- 2026/11/2
コメント
- …
このリポジトリの他のスキル
code-migration
6.5kエージェントが、テストを先に書き、元のコードに対する進捗を測りながら、1度に1つの関数ずつ、ある言語やフレームワークから別のものへコードを移植できるよう支援します。
codebase-refactoring
6.5kエージェントが、重複したコードを直し、使われていないコードを削除し、最も複雑なファイルを簡素化することで、コードベースの健全性を改善できるよう支援します。その順序で進めます。
compare-codebases
6.5kエージェントが、言語が異なる場合でも、2つのコードフォルダを関数ごとに比べ、どの関数が一致し、どれが欠けているかを確認できるよう支援します。
jscpd
6.5kエージェントが、完全一致、名前を変えた一致、近いコピーの形で、プロジェクト内のコピー&ペーストされたコードを見つけ、どれだけのコードが重複しているかを測れるよう支援します。
似ているスキル
diffs
394k編集内容の実際の共有可能な差分を、手書きの要約ではなく、ビューアーのリンクや画像ファイルとして作成するようエージェントを支援します。
coding-agent
394kCodex、Claude Code、OpenCodeに大きめのコーディング作業をバックグラウンドのワーカーとして渡し、その進捗を確認するようエージェントを支援します。
gh-issues
394kGitHubのイシューを探し、取り組むものを選び、修正を含むプルリクエストを開くようエージェントを支援します。レビューコメントへの対応も含みます。