Skip to content

commandCode quality

Remove Duplication

Finds duplicated logic in one area and merges it only where the copies truly belong together, with tests proving behavior held.

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

Writes 1 file to .claude/commands/

.claude/commands/remove-duplication.md14 lines · 1.1 KBFile
---
description: Finds duplicated logic in one area and merges it only where the copies truly belong together, with tests proving behavior held.
argument-hint: "[folder or module]"
---

Find and remove duplication in: $ARGUMENTS

1. Search the area for repeated logic: similar function bodies, repeated conditionals, parallel switch statements, copied SQL or regexes. Use `jscpd` or a similar detector if installed, then verify by reading.
2. For each cluster, diff the copies and list what differs. Ask of each pair: if one needs to change tomorrow, must the other change too? If yes, they truly belong together. If not, it is coincidental.
3. Make sure tests cover each copy before merging. Add missing characterization tests first and run them green.
4. Merge only real duplicates: extract one function, pass the differences as parameters, name it for what it does. Keep it in the nearest shared module, not a new utils dump.
5. Never merge across package boundaries or change public signatures without asking.

Report clusters as merged, left alone (with the reason), or needs decision. Run all tests and show the net lines removed.

What it does

Not every repeated block should be merged: two pieces of code can look alike and still change for different reasons. This command finds the copies, compares them line by line and judges whether they share one reason to change.

Real duplicates are merged into one helper with a clear name. Coincidental ones stay separate, each with the reason noted, so you do not end up with an abstraction nobody can follow.

Example

you › /remove-duplication src/api/validators

agent › 3 clusters. Merged: email and phone normalization (4 copies) into normalizeContact(). Left alone: order and refund date checks, same shape but different rules. Needs decision: two pagination helpers in separate packages.
Tests pass, net -96 lines.

Questions

Does it merge anything that looks similar?
No. Each cluster is judged on whether the copies change for the same reason, and the rest stay separate.
What protects against breaking behavior?
It adds tests around each copy before merging and runs the full suite afterward.
  • Add Error Handling

    CommandCode quality

    Free
  • Add Logging

    CommandCode quality

    Free
  • Add Types

    CommandCode quality

    Free
  • Extract Function

    CommandCode quality

    Free