Skip to main content
skill Claude Code CX Codex Cursor GitHub Copilot

dry-refactoring — Claude Code skill: eliminate code clones

This skill automates refactoring of copy-paste clones found by jscpd. It guides you through extraction strategies—function, parameterization, module or class—and highlights which files to start with.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add kucherenko/jscpd --skill dry-refactoring -a claude-code
Open on GitHub

What dry-refactoring does

After running jscpd with the --reporters ai flag, the skill helps you tackle discovered clones. It distinguishes three kinds: exact copies (most common), renamed copies (different variable names but same logic) and near-miss clones (a few edited lines). Each type gets its own strategy: extract a function, move to a constant, create a parameterized version or merge via a strategy object.

The skill teaches you to read jscpd output correctly: identify clone type by its suffix, locate both code fragments, understand what differs. The critical step is triage—some clones should stay put (generated code, intentional boilerplate like models and configs, pairs under ten lines with a vague abstraction name). Refactor only code that truly repeats logic and deserves a clear name.

For large codebases, run jscpd with --summary to see files with the highest duplicate percentage—refactoring there pays off first. After each change, re-run jscpd with the same flags to confirm the clone vanished and the duplicate percentage dropped.

What it helps with

  • eliminate exact copy-paste clones and unify logic
  • tell clone types apart and pick a refactoring strategy
  • find every call site when extracting a function
  • decide which clones to refactor and which to leave
  • verify the result and confirm the clone is gone

Example prompts for the agent

  • Run jscpd with `--reporters ai` on the path, then help me refactor the clones it found
  • Show me how to parameterize two renamed copies of the tax-calculation function
  • Help me merge saveUser and saveAccount functions that differ in one line

How to install dry-refactoring

1Skills CLI (Vercel)

npx skills add kucherenko/jscpd --skill dry-refactoring -a claude-code

2Manually: put the files in place

~/.claude/skills/dry-refactoring/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/dry-refactoring/

From the same set2

The whole set →

Similar

redesign-existing-projects nexu-io Audits an existing interface and updates a website or app without changing its working product structure. Use it when the UI feels generic or lacks visual hierarchy. Claude Code CX Codex Cursor +1 1,800installs pr-feedback-quality-gate nexu-io Skill for tracking pull request feedback, resolving merge conflicts, and validating fixes. Runs read-only cross-review before commit to catch issues in follow-up changes. Claude Code CX Codex Cursor +1 1,800installs case-review zhaoxuya520 Audits a reverse engineering case package for handoff: verifies Evidence to Finding and Path traceability, checks artifact hash integrity, and generates a report without modifying the case. Claude Code CX Codex Cursor +1 926installs how cursor Explores codebase architecture and runtime execution paths before you edit code. Helps determine module ownership, layering, and placement for new features. Claude Code CX Codex Cursor +1 5,900installs build-graph tirth8205 Builds or updates a repository knowledge graph for code review. Use it during first setup, after a branch switch, or following a large refactor. Claude Code CX Codex Cursor +1 1,300installs hyperplan code-yeongyu A team of five agents with opposing roles stress-tests a plan. Only what survives the crossfire makes it to the final scheme. Claude Code CX Codex Cursor +1 576installs improve-codebase-architecture Matt Pocock This skill scans a codebase for architectural friction, then builds a visual HTML report with refactoring candidates. Use it before a deeper architecture review. Claude Code 1,163,035installs tdd Matt Pocock Guides the red-green TDD loop: ensures tests are written first, keep them at public interfaces, and avoid coupling to implementation details. Claude Code 1,141,062installs

Questions about dry-refactoring

How do I install dry-refactoring in Claude Code?

Run npx skills add kucherenko/jscpd --skill dry-refactoring -a claude-code, then open ~/.claude/skills/dry-refactoring/SKILL.md.

What does dry-refactoring do?

The skill helps you refactor copy-paste clones found by jscpd. It explains how to tell exact, renamed and near-miss clones apart, and offers strategies to eliminate them: extract a function, parameterize, create a constant or base class.

Which jscpd command should I run before using this skill?

Run jscpd with the --reporters ai flag: npx jscpd --reporters ai <path>. On large projects add --summary to see files with the highest duplicate percentage. For Type-2 clones use --ignore-identifiers --min-tokens 70; for Type-3 use --max-gap-lines 1 --similarity.

Source text

SKILL.md · MIT
---
name: dry-refactoring
description: 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.
---

# dry-refactoring

Guided workflow to eliminate copy-paste duplication in source code. Use after running [jscpd](../jscpd/SKILL.md) to detect clones.

## Prerequisites

First, run jscpd to identify duplications:

```bash
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:

```bash
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:

```bash
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:

```bash
# 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](../jscpd/SKILL.md)** skill for full option reference, including cross-format group syntax, the clone-kind suffixes and how to read the summary.

## Workflow

1. Run jscpd with `--reporters ai` on the target path (add `--summary` on larger codebases to pick a starting point: files with high `dup%` and high token counts pay off most)
2. 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
3. Read both code fragments from the source files
4. Understand what the duplicated code does, and for renamed and similar clones list exactly what differs between the two sides
5. **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 `switch` over 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 like `processData`; 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
6. Design a refactoring: extract a shared function, class, module, or constant; the kind decides the strategy (below)
7. Apply the refactoring — update both locations and all other usages
8. 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-identifiers` again
9. 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:
```ts
// 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:
```ts
// 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 differ become parameters:
```ts
// Before: computeCartTotal(items) and computeBasketTotal(entries), same body, other names;
//         limits-dev.js and limits-prod.js, same shape, other numbers
// After: one function whose parameters are the identifiers that differed,
//        or one function reading the values that differed from a config object
```
A renamed clone whose only difference is a literal is a missing constant or config entry, not a missing function.

**Unify near-miss copies** — for `[~N gap]` clones. Read the unmatched lines: the gap is the one place the copies diverged, typically a guard, a log call or an extra field. Extract the common body and pass the divergence in:
```ts
// Before: saveUser and saveAccount, identical except one inserted validation line
// After: one saveRecord(record, { validate }) with the inserted line behind the option,
//        or the inserted line moved to the caller before the shared call
```
If the gap changes the meaning rather than adding a step, keep two functions but extract the shared halves.

**Merge similar functions** — for `[~N ast]` clones. The structure matches but names, literals and some statements do not. Diff the two functions first; the `ast` score tells how much is shared (`0.9` is a copy with one edit, `0.75` a copy with a couple of added statements plus renames). Extract the shared skeleton and inject what differs, as arguments, a strategy object, or a callback:
```ts
// Before: buildInvoice(order, customer, taxRate) and buildCreditNote(refund, account, vatRate):
//         same loop, same rounding, one extra guard and one extra log call in the second
// After: buildDocument(source, party, rate, { filter, onBuilt }) used by both
```
Below about `0.8` the pair usually shares an idiom, not an implementation; leave those alone unless the summary shows the file is a hotspot anyway.

Always ensure:
- All call sites are updated, not just the two reported by jscpd
- Tests still pass after refactoring
- The extracted abstraction has a clear, descriptive name
- The re-run uses the same detection flags as the run that found the clone

## Tips

- Start with clones that have the highest line count — they have the most impact
- Use the summary's `dup%` column to order the work: a large file with a high share of duplicated lines pays back first
- A clone between test files may indicate a missing test helper
- Clones across unrelated modules may signal a missing shared utility
- A cross-format clone (same logic in a `.js` and a `.ts` file, found with `--cross-formats`) often means code was ported without deleting the original — consolidate into one implementation (usually the TypeScript one) and update imports, rather than extracting a third shared copy
- Many `(renamed)` clones in one file usually mean one abstraction is missing, not many: look for the shared shape before extracting pair by pair. Many `(renamed)` clones across *test* files usually mean nothing: test cases are supposed to look alike
- Do not gate CI (`--threshold`, `--fail-on-new-clones`) on the Type-2/Type-3 passes until the team has reviewed what they report on this codebase; gate on the exact run
- `--similarity` covers JavaScript, TypeScript, Python, Java, Kotlin, Scala, C#, Go, Rust, C, C++, PHP, Ruby, Swift and Clojure, also inside Markdown code blocks and Vue, Svelte and Astro scripts; for other languages rely on the exact and `--max-gap-lines` passes
- Use `--min-lines 10` to filter noise and focus on meaningful duplications
- Keep a separate `--baseline` per set of detection flags when gating CI: renamed and similar runs fingerprint clones differently from exact runs

© kucherenko · MIT · kucherenko/jscpd

See also

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor