Skip to content

skillCode quality

Code Duplication Removal

Detects copy-pasted blocks and merges them into one abstraction only when the copies change together, leaving look-alikes that evolve apart untouched.

npx workspacetemplates add code-duplication-removal --tool claude-code

Writes 1 file to .claude/skills/code-duplication-removal/

.claude/skills/code-duplication-removal/SKILL.md28 lines · 1.9 KBFile
---
name: code-duplication-removal
description: Detects copy-pasted blocks and merges them into one abstraction only when the copies change together, leaving look-alikes that evolve apart untouched.
---

# Code duplication removal

Merge copies that share a reason to change. Leave the rest.

## Steps

1. Detect candidates with a tool if the project has one (`npx jscpd`, `pmd cpd`, or the linter's duplicate rule); otherwise search for repeated distinctive lines with grep. Ignore generated files, tests and migrations unless asked.
2. For each pair or group, read all copies side by side. Write down exactly what differs: constants, types, one branch, a name.
3. Check how they evolved: `git log -L<start>,<end>:<file>` or `git log --oneline -- <file>` for each copy. If a past fix was applied to all copies together, they change together. If they were edited at different times for different reasons, they only look alike.
4. Decide with this rule: merge when copies change together and the differences can be expressed as parameters without adding more than two flags. Wait (the rule of three) when there are only two copies and the intent is unclear. Leave alone when the copies belong to different features, even if the code is the same today.
5. When merging, extract the shared part into a function or component with a name from the domain, pass the differences as arguments or callbacks, and put it where both callers can import it without a cycle.
6. Replace one copy at a time and run the tests after each. If tests do not cover them, add one per copy first.
7. Delete nothing until all callers use the shared version.

## Rules

- No boolean flag parameters that switch behavior; make two functions.
- No shared helper with more than four parameters; reconsider the split.
- Do not touch public signatures in the same change.

## Report

Groups found, which were merged and which left apart with the reason, lines removed and tests run.

What it does

Duplicated code is not always a problem, and a wrong abstraction costs more than a copy. This skill finds repeated blocks, then checks the history to see whether the copies were edited together or drifted apart.

Only duplicates that move as a group are merged. Others are left alone and listed with the reason, so the cleanup never ties together things that have separate reasons to change.

Example

you › Remove duplication in src/exports/

agent › jscpd found 4 groups. Merged 2: csv and tsv writers (changed together in 5 commits) into writeDelimited(rows, sep); the two status badge builders into one. Left 2 alone: invoiceTotals and quoteTotals look alike but diverged over tax rules in 3 commits. Net 118 lines removed, all tests pass.

Questions

Why not merge every duplicate it finds?
Code that looks alike but changes for different reasons gets tangled when merged. Each later edit then needs another flag, and the helper becomes worse than the copy.
How does it know copies change together?
It reads the git history of each copy. Fixes applied to all of them in the same commit are the signal.
  • API Deprecation Handling

    SkillCode quality

    Free
  • Code Smell Detection

    SkillCode quality

    Free
  • Concurrency Bug Analysis

    SkillCode quality

    Free
  • Cyclomatic Complexity Reduction

    SkillCode quality

    Free