AgentSkillsCN

duplicate-detection-rules

代码重复检测规则。通过模式与阈值,精准识别克隆代码、角色重复,以及变量重复。提供重构手段,帮助用户高效去除重复代码。

SKILL.md
--- frontmatter
description: Code duplication detection rules. Patterns and thresholds for identifying clone code, role duplication, and variable duplication. Guide for removing duplicates through refactoring.
name: duplicate-detection-rules
allowed-tools: ["Read", "Grep", "Glob", "Task"]

Duplicate Detection Rules

Core Principle

Duplication is the enemy of change. When you change one, you must change all copies. Miss one, and you have a bug.


Detection Categories

1. Clone Code

TypeDefinitionSimilaritySeverity
Type-1Exact clone (whitespace ignored)100%CRITICAL
Type-2Same structure, different names/literals>90%HIGH
Type-3Similar with some modifications>70%MEDIUM

Details: Read("references/clone-patterns.md")

2. Role Duplication

Detection Signals:

SignalDescriptionProbability
Similar names*Manager, *Handler, *ServiceHigh
Same interfaceImplements same interfaceVery High
Similar methods3+ same method namesHigh

Synonym Groups to Watch:

  • Retrieve: get, fetch, retrieve, find, load
  • Create: create, make, build, generate, new
  • Validate: validate, check, verify, ensure

3. Variable/Constant Duplication

TypeDetection Criteria
Magic numbersSame value used 2+ times
Duplicate stringsSame string (5+ chars) used 3+ times
Config valuesSame config in multiple places

Detection Thresholds

TypeThresholdReport
Type-1 Clone5 linesAlways
Type-2 Clone90% similarAlways
Type-3 Clone70% similar10+ lines
Role duplication3 same methodsAlways
Magic numbers2+ usagesAlways

Exceptions (Allowed Duplication)

SituationReason
Test codeMaintain test independence
Generated codeAuto-generation tool responsibility
External interfaceContract compliance required
Performance codeIntentional inlining

Not Considered Duplication:

  • Code 2 lines or less
  • Idiomatic patterns (e.g., if (err) return;)
  • Language boilerplate

Fix Strategies

Detailed patterns: Read("references/fix-patterns.md")

Duplication TypeStrategy
Clone codeExtract common function
Role duplicationConsolidate classes
ConstantsCentralize config