Code duplication detection rules. Patterns and thresholds for identifying clone code, role duplication, and variable duplication. Guide for removing duplicates through refactoring.
/plugin marketplace add chkim-su/serena-refactor-marketplace/plugin install serena-refactor@serena-refactor-marketplaceThis skill inherits all available tools. When active, it can use any tool Claude has access to.
Duplication is the enemy of change. When you change one, you must change all copies. Miss one, and you have a bug.
Definition: Exactly identical code excluding whitespace/comments
Detection Criteria:
Severity: CRITICAL
Fix Strategy:
Definition: Same structure with only different variable names/literals
// Clone A
const result = users.filter(u => u.age > 18);
return result.map(u => u.name);
// Clone B
const data = items.filter(i => i.price > 100);
return data.map(i => i.title);
Detection Criteria:
Severity: HIGH
Fix Strategy:
Definition: Similar code with some lines added/deleted/modified
Detection Criteria:
Severity: MEDIUM
Fix Strategy:
Detection Signals:
| Signal | Description | Duplication Probability |
|---|---|---|
| Similar names | *Manager, *Handler, *Service | High |
| Same interface | Implements same interface | Very High |
| Similar method set | 3+ same method names | High |
| Same dependencies | Depends on same classes | Medium |
Severity: HIGH
Fix Strategy:
Synonym pairs to watch:
| Group | Synonyms |
|---|---|
| Retrieve | get, fetch, retrieve, find, load |
| Create | create, make, build, generate, new |
| Validate | validate, check, verify, ensure |
| Process | process, handle, execute, run, do |
| Transform | convert, transform, parse, map, to |
Rules:
Prohibited Pattern:
// Bad
if (retryCount > 3) { ... }
setTimeout(() => {}, 5000);
if (users.length > 100) { ... }
Fix:
// Good
const MAX_RETRIES = 3;
const TIMEOUT_MS = 5000;
const MAX_USERS = 100;
Detection Criteria:
Exceptions:
""" ", "," etc.Detection Pattern:
// Duplicate config
const timeout1 = 30000;
const timeout2 = 30000;
const requestTimeout = 30000;
Fix:
// Single config
const CONFIG = {
TIMEOUT_MS: 30000
};
| Type | Threshold | Reporting Condition |
|---|---|---|
| Type-1 Clone | 5 lines | Always report |
| Type-2 Clone | 90% similarity | Always report |
| Type-3 Clone | 70% similarity | 10+ lines only |
| Role duplication | 3 same methods | Always report |
| Magic numbers | 2+ usages | Always report |
| Duplicate strings | 3+ usages | Always report |
| Severity | Definition | Action Deadline |
|---|---|---|
| CRITICAL | Immediate fix required | Current task |
| HIGH | Quick fix recommended | This sprint |
| MEDIUM | Planning required | Next sprint |
| LOW | Awareness only | When time permits |
| Situation | Reason |
|---|---|
| Test code | Maintain test independence |
| Generated code | Auto-generation tool responsibility |
| External interface implementation | Contract compliance required |
| Performance optimization code | Intentional inlining |
if (err) return;)1. Identify common parts
2. Parameterize differences
3. Extract common function/method
4. Replace originals with calls
5. Run tests
1. Analyze responsibilities of both classes
2. Determine consolidation target (by reference count)
3. Merge methods
4. Migrate references
5. Remove deletion target
1. Identify duplicate values
2. Assign meaningful names
3. Add constant definitions
4. Replace usages
5. Code review
Duplication Rate = (Duplicate Lines / Total Lines) × 100%
Target: < 5%
Warning: 5% ~ 10%
Danger: > 10%
DRY Score = 100 - Duplication Rate
Excellent: > 95
Good: 90 ~ 95
Needs Improvement: < 90
Build robust backtesting systems for trading strategies with proper handling of look-ahead bias, survivorship bias, and transaction costs. Use when developing trading algorithms, validating strategies, or building backtesting infrastructure.