Rule
Eliminate obvious within-file duplication.
Duplicated code blocks increase maintenance burden and the risk of inconsistent updates.
Supported languages: 45+Introduction
Copy-pasted code within a single file creates maintenance nightmares that compound over time. When the same logic appears in multiple places, bug fixes and feature updates must be applied to every occurrence. Developers inevitably miss one of the duplicates, leading to inconsistent behavior where the same operation produces different results depending on which code path executes. This inconsistency is difficult to debug because the duplicated logic looks identical at first glance, and differences only emerge after careful comparison.
Why it matters
Bug propagation: When a bug exists in duplicated code, fixing it in one location doesn't fix it everywhere. Developers fix the first occurrence without realizing copies exist elsewhere, leaving the bug active under different conditions.
Maintenance burden: Every duplicated block doubles maintenance cost. Changing logic requires finding and updating every copy, and as files grow, tracking duplicates becomes harder.
Code examples
❌ Non-compliant:
class OrderProcessor {
    async processStandardOrder(order) {
        if (!order.items || order.items.length === 0) {
            throw new Error('Order must have items');
        }
        const total = order.items.reduce((sum, item) => 
            sum + (item.price * item.quantity), 0);
        const tax = total * 0.08;
        const finalAmount = total + tax;
        return { total: finalAmount, tax };
    }
    
    async processExpressOrder(order) {
        if (!order.items || order.items.length === 0) {
            throw new Error('Order must have items');
        }
        const total = order.items.reduce((sum, item) => 
            sum + (item.price * item.quantity), 0);
        const tax = total * 0.08;
        const expressfee = 15.99;
        const finalAmount = total + tax + expressFee;
        return { total: finalAmount, tax, expressFee };
    }
}Why it's wrong: The validation logic and total calculation are duplicated. If the tax rate changes or the validation needs enhancement, both methods require updates. A developer might update the tax calculation in one method but forget the other, causing inconsistent pricing.
✅ Compliant:
class OrderProcessor {
    validateOrder(order) {
        if (!order.items || order.items.length === 0) {
            throw new Error('Order must have items');
        }
    }
    
    calculateSubtotal(items) {
        return items.reduce((sum, item) => 
            sum + (item.price * item.quantity), 0);
    }
    
    calculateTax(amount) {
        return amount * 0.08;
    }
    
    async processStandardOrder(order) {
        this.validateOrder(order);
        const subtotal = this.calculateSubtotal(order.items);
        const tax = this.calculateTax(subtotal);
        return { total: subtotal + tax, tax };
    }
    
    async processExpressOrder(order) {
        this.validateOrder(order);
        const subtotal = this.calculateSubtotal(order.items);
        const tax = this.calculateTax(subtotal);
        const expressFee = 15.99;
        return { total: subtotal + tax + expressFee, tax, expressFee };
    }
}Why this matters: Validation, calculation, and tax logic are centralized in single methods. Changing the tax rate means modifying one method, not hunting through the file for duplicates. Each helper method can be tested independently, and both order types automatically inherit any improvements or bug fixes.
Conclusion
Within-file duplication is often the easiest to fix and provides immediate benefits. Extract duplicated logic into helper functions or methods as soon as you notice the pattern. The rule of three suggests that once code appears three times, it's time to refactor. Don't wait for duplication to spread across the entire file before addressing it.
.avif)
