Computer science / 3 MIN READ
DRY concept
DRY concept
From the original Fervor library. Examples may use older package versions.
DRY in Coding: A Simple & Fun Tutorial
What is DRY?
DRY stands for Don’t Repeat Yourself, a principle in programming that helps keep your code clean, efficient, and maintainable.
Imagine writing a book where you copy-paste the same paragraph in ten different chapters. Every time you want to update that paragraph, you must go to all ten places and make the same change. What a headache, right? DRY solves this problem in coding by ensuring that you write something once and reuse it whenever needed.
Why Does DRY Matter?
✅ Easier Maintenance
If you find a bug in one place, you don’t have to fix it in multiple locations.
✅ Better Readability
Less duplicate code means it’s easier to understand what your program does.
✅ Saves Time
You write less code and can reuse existing functions, classes, or modules.
How to Apply DRY in Your Code
Let’s break it down with examples!
1. Use Functions to Avoid Duplication
❌ Without DRY (Repeating Code)
print("Hello, Alice! Welcome!")
print("Hello, Bob! Welcome!")
print("Hello, Charlie! Welcome!")
🔧 With DRY (Using a Function)
def greet(name):
print(f"Hello, {name}! Welcome!")
greet("Alice")
greet("Bob")
greet("Charlie")
Now, if you need to change the greeting, you only update one function, not three print statements!
2. Use Loops Instead of Copy-Pasting
❌ Without DRY (Repeating Code)
print("Processing item 1")
print("Processing item 2")
print("Processing item 3")
🔧 With DRY (Using a Loop)
for i in range(1, 4):
print(f"Processing item {i}")
Less typing, same result!
3. Use Constants or Variables for Reusability
❌ Without DRY (Hardcoded Values Everywhere)
price1 = 10 * 0.08 # 8% tax
price2 = 20 * 0.08
price3 = 30 * 0.08
🔧 With DRY (Using a Constant)
TAX_RATE = 0.08
price1 = 10 * TAX_RATE
price2 = 20 * TAX_RATE
price3 = 30 * TAX_RATE
If the tax rate changes, you only update TAX_RATE, not every calculation.
4. Use Classes to Avoid Redundant Code
If you’re working with objects, classes help prevent duplicate code.
❌ Without DRY (Repeating Attributes for Each Object)
alice_name = "Alice"
alice_age = 25
bob_name = "Bob"
bob_age = 30
🔧 With DRY (Using a Class)
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
alice = Person("Alice", 25)
bob = Person("Bob", 30)
Now, if you want to add more attributes (like email or phone number), you only modify the Person class instead of updating every individual person.
Common Pitfalls (When Not to DRY Too Much!)
While DRY is great, overusing it can lead to overcomplicated code (a.k.a. “too clever” code).
❌ Bad Example (Too DRY, Hard to Read)
def math_magic(a, b, c):
return (a * b) + c - (a / b)
result1 = math_magic(10, 2, 5)
result2 = math_magic(20, 4, 10)
🤯 What is math_magic even doing?! Instead, name your functions properly and keep your code readable.
Conclusion
- DRY means Don’t Repeat Yourself – avoid duplicate code by using functions, loops, variables, and classes.
- DRY makes your code easier to maintain, read, and update.
- Don’t overdo DRY—balance reusability with readability!
Now go forth and write clean, DRY code! 🚀 Happy coding!