dry-refactoring
Skill này làm gì
Skill đưa ra phương pháp từng bước để tìm mã trùng lặp và gộp nó thành một phiên bản dùng chung. Dành cho lập trình viên muốn giữ một nguồn duy nhất cho logic lặp lại.
Khi nào nên dùng
- ✓Khi lập trình viên thấy cùng một đoạn mã được dán ở nhiều nơi và muốn gộp lại.
- ✓Khi một đội muốn loại bỏ các bản trùng lặp sau một lần sửa nhanh bằng cách sao chép-dán.
- ✓Khi lập trình viên cần phân biệt đâu là bản trùng lặp thật và đâu chỉ trông giống nhau.
Liên kết skill
https://github.com/kucherenko/jscpd/blob/master/skills/dry-refactoring/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Tìm đoạn mã sao chép-dán trong dự án này và gộp những đoạn lặp lại nhiều nhất thành hàm dùng chung.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Skill chạy công cụ tìm trùng lặp trước, rồi hướng dẫn AI sửa các bản sao chính xác trước khi xem các bản gần giống. Skill dùng các chiến lược như tách thành hàm hoặc hằng số.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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
Câu hỏi thường gặp
Vì sao sửa bản sao chính xác trước?
Một bản sao chính xác hầu như luôn là trùng lặp thật, nên đó là chỗ an toàn nhất để bắt đầu.
Bản gần giống có luôn là trùng lặp không?
Không. Skill cảnh báo rằng bản gần giống có thể là mô hình, thiết lập kiểm thử hoặc mã sinh tự động chỉ trông giống nhau.
Công cụ nào tìm các bản trùng lặp?
Nó dùng công cụ phát hiện trùng lặp jscpd.
Chi tiết
Mô tả gốc của tác giả
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
Độ phổ biến
- Sao GitHub
- 6.5k
- Vị trí xếp hạng
- #14190
- Giấy phép
- MIT
- Cập nhật
- 2/11/2026
Bình luận
- …
Thêm skill từ kho mã này
code-migration
6.5kGiúp agent của bạn chuyển mã từ ngôn ngữ hoặc framework này sang cái khác, từng hàm một, có kiểm thử trước và đo tiến độ so với bản gốc.
codebase-refactoring
6.5kGiúp agent của bạn cải thiện sức khỏe mã nguồn bằng cách sửa mã trùng lặp, xóa mã chết và đơn giản hóa các file phức tạp nhất, theo đúng thứ tự đó.
compare-codebases
6.5kGiúp agent của bạn so sánh hai thư mục mã từng hàm một, kể cả khác ngôn ngữ, để xem hàm nào khớp và hàm nào còn thiếu.
jscpd
6.5kGiúp agent của bạn tìm mã sao chép-dán trong dự án ở dạng chính xác, đổi tên hoặc gần giống, và đo mức độ trùng lặp của mã.
Skill tương tự
diffs
394kGiúp agent của bạn tạo một diff có thể chia sẻ về các chỉnh sửa, dưới dạng liên kết xem hoặc file ảnh, thay vì một bản tóm tắt viết tay.
coding-agent
394kGiúp agent của bạn giao một việc lập trình lớn cho Codex, Claude Code hoặc OpenCode chạy nền, rồi theo dõi tiến độ của nó.
gh-issues
394kGiúp agent của bạn tìm issue trên GitHub, chọn những issue cần xử lý và mở pull request với bản sửa, kể cả theo dõi nhận xét đánh giá.