← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Types of Code Duplication (Clone vs Semantic)

quality Intermediate

TL;DR

Not all duplication is equal — clone duplication (copy-paste) always warrants extraction, but semantic duplication (similar logic, different context) may be acceptable.

Explanation

Clone duplication: identical or near-identical code — always extract. Semantic duplication: two code sections do similar things with different data or context — requires judgement. Rule of Three: tolerate one duplicate, refactor on the third occurrence. Types: Type 1 (exact clone), Type 2 (renamed variables), Type 3 (modified statements), Type 4 (same algorithm, different structure). Tools: phpcpd (PHP Copy/Paste Detector), jscpd, SonarQube. The wrong abstraction is worse than duplication — don't prematurely abstract code that merely looks similar but has diverging requirements.

Common Misconception

All duplication must be eliminated immediately — premature abstraction creates worse problems than duplication. Wait for the third occurrence before extracting.

Why It Matters

Clone duplication creates maintenance traps — fix a bug in one copy and forget the other. But wrong abstractions create coupling that's harder to undo than the original duplication.

Common Mistakes

  • Extracting two pieces of code that look similar but have different reasons to change — they'll diverge and the shared abstraction becomes a problem.
  • Not using phpcpd/jscpd in CI to detect growing duplication.
  • Applying Rule of Three too rigidly — sometimes the second occurrence warrants extraction if the pattern is clearly stable.

Code Examples

✗ Vulnerable
// Same validation logic in 3 controllers — copy-paste:
if (strlen($name) < 2 || strlen($name) > 50) {
    throw new ValidationException('Invalid name');
}
✓ Fixed
// Extracted to validator on third occurrence:
class NameValidator {
    public function validate(string $name): void {
        if (strlen($name) < 2 || strlen($name) > 50) {
            throw new ValidationException('Invalid name');
        }
    }
}

Added 23 Mar 2026
Views 22
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 2 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 2 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 8 Unknown AI 4 Google 4 Perplexity 3 ChatGPT 1 Meta AI 1 Ahrefs 1
crawler 19 crawler_json 1 pre-tracking 2
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
Run phpcpd in CI. Extract on third occurrence. Use shared base classes, traits, or services for validated duplicate patterns. Be cautious — not all similar code should be unified.
📦 Applies To
web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
phpcpd|jscpd
Auto-detectable: ✓ Yes phpcpd jscpd sonarqube
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: High Context: File Tests: Update

✓ schema.org compliant