The Duplication Dilemma: A VECTR Guide to Repeating Yourself

Whats up?
From our programming 101 classes, we’re taught about DRY (Don't Repeat Yourself). We learn to hunt down duplicated lines of code and can inadvertently convince ourselves that if you see it twice... it must be bad. But is it? The VECTR philosophy offers a more pragmatic, perspective with its tenet: "Duplication is cheaper than the wrong abstraction." This doesn't mean all duplication is good; it means we must learn to distinguish between two very different kinds: the cheap kind and the dangerously expensive kind.
The "Safe" Kind: Accidental Duplication
This is the duplication VECTR tells you to pause and think about before abstracting. It’s when two pieces of code look similar by coincidence, but they represent different ideas that could change for different reasons.
safe.py
# in user_profile.py
def display_recent_orders(orders: list):
for order in orders[:5]: # Show the user their last 5 orders
print(order)
# in admin_dashboard.py
def show_flagged_orders(orders: list):
for order in orders[:5]: # Show admins the first 5 flagged orders for review
print(order)
Both snippets use [:5]. A naive application of DRY might lead you to create a MAX_ITEMS_TO_DISPLAY = 5
constant, or worse just turn this into one function. But this is the wrong abstraction. The user seeing "5" recent orders and the admin reviewing "5" flagged orders are two completely different business rules.
The two just happen to be the same value of 5 today.
The "Dangerous" Kind: Canonical Duplication
This is the duplication that creates technical debt and leads to bugs. It’s when two pieces of code are identical because they represent the exact same business rule. If one changes, the other must change with it.
dangerous.py
# in shopping_cart_api.py
def calculate_cart_total(items: list, destination_zip: str):
subtotal = sum(item.price for item in items)
# --- Start of dangerous duplication ---
shipping_cost = 10.00 # Base rate
if destination_zip.startswith("9"): # West coast surcharge
shipping_cost += 5.00
# --- End of dangerous duplication ---
return subtotal + shipping_cost
# in order_processor.py
def finalize_and_charge_order(order: Order):
# ... logic to get items and address ...
# --- Start of dangerous duplication ---
shipping_cost = 10.00 # Base rate
if order.zip_code.startswith("9"): # West coast surcharge
shipping_cost += 5.00
# --- End of dangerous duplication ---
charge_customer(order.id, order.subtotal + shipping_cost)
This isn't two separate rules that happen to be the same; it is one rule ("How do we calculate shipping?"). This should be de-duplicated.
The Litmus Test
So how do you decide? Before you abstract, ask yourself this simple question:
"If I change this line of code, is there another line somewhere else that must change in the exact same way for the system to be correct?"
If the answer is yes, you have dangerous, canonical duplication. Eliminate it. If the answer is no, you have cheap, accidental duplication. Leave it alone.