본문으로 건너뛰기
Skillpedia
일반 코딩6.5k · #14190

dry-refactoring

하는 일

중복 코드를 찾아 하나의 공용 버전으로 합치는 단계별 방법을 설명합니다. 반복되는 로직의 기준을 하나로 유지하려는 개발자를 위한 스킬입니다.

사용하는 경우

  • ✓개발자가 같은 코드가 여러 곳에 붙어 있는 것을 발견하고 이를 합치려 할 때.
  • ✓팀이 빠른 복사-붙여넣기 수정 후 중복을 제거하려 할 때.
  • ✓개발자가 실제 중복과 비슷해 보이기만 하는 코드를 구분해야 할 때.

스킬 링크

https://github.com/kucherenko/jscpd/blob/master/skills/dry-refactoring/SKILL.md

복사해서 AI(Claude, ChatGPT, Cursor)에 붙여넣고 지침을 따르도록 요청하세요.

사용 방법

가장 쉬운 방법입니다. 메시지를 복사해 AI에 붙여넣으세요. AI가 스킬을 읽고 지침을 따릅니다.

바로 붙여넣을 수 있는 메시지

Use this skill to help me: https://github.com/kucherenko/jscpd/blob/master/skills/dry-refactoring/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
AI가 이런 스킬을 알아서 찾아 쓰길 원하시나요?무료 Skillpedia MCP를 설치하세요. 한 줄이면 작업마다 스킬 15,000+개 중에서 검색합니다.1분 만에 설치 →

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

  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:

// 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.

이 스킬이 유용한가요?

0

로그인 투표하려면 로그인하세요.

댓글

  • …

이 저장소의 다른 스킬

비슷한 스킬