Writing Cleaner Code with Purpose
When starting out in development, it's easy to repeat the same code without realizing how much it's costing you in the long run. Two key principles stand out in clean code philosophy: DRY (Don't Repeat Yourself) and WET (Write Everything Twice).
In this post, I'll break down the differences between DRY and WET, why they matter, and how to apply them in real-world projects. If you're a junior developer or self-taught coder, mastering these concepts will instantly level up your codebase quality.
🧠 What Is DRY Code?
DRY (Don't Repeat Yourself) is a principle that encourages reducing code duplication. It means putting repeated logic into reusable functions, components, or modules.
🔁 Example of DRY Code
function calculateDiscount(price, discountRate) {
return price - (price * discountRate);
}
// Usage
const product1 = calculateDiscount(100, 0.2);
const product2 = calculateDiscount(50, 0.1);
In this example, instead of repeating discount logic everywhere, we abstract it into a single function.
🔥 What Is WET Code?
WET (Write Everything Twice) is the anti-pattern of DRY. It involves repeating logic in multiple places, making maintenance harder and more error-prone.
⚠️ Example of WET Code
// Discount for product 1
const discounted1 = 100 - (100 * 0.2);
// Discount for product 2
const discounted2 = 50 - (50 * 0.1);
Now imagine you need to change the discount logic later. You’ll have to update every instance manually—easy to forget one and introduce bugs!
💡 Why DRY Matters in Software Development
- Easier to maintain: Change one place instead of dozens
- Improved readability: Your functions have clear responsibilities
- Better testing: You can unit test reusable logic
- Team collaboration: DRY code reduces misunderstandings in larger codebases
🧩 Where DRY Can Be Misused
While DRY is powerful, over-abstraction can hurt too. Don’t create a function just for the sake of reusing one line. Sometimes duplication improves clarity (especially in early-stage prototyping).
The key is to extract logic only when:
- It's used in 2 or more places
- It's logically grouped (e.g., formatting, calculations, API calls)
- You expect changes to it in the future
🔗 Internal Link: Learn Clean Project Structures
Want to keep your entire codebase clean and scalable? Don’t miss my detailed post on How I Organize My Code Projects Like a Pro.
🛠 Real-World Applications
Here are some places where DRY is especially useful:
- Validation logic (e.g., input checks)
- API request wrappers
- Reusable components (React/Angular/Vue)
- Utility/helper functions
🛠 Sample DRY Helper
export const formatDate = (dateString) => {
return new Date(dateString).toLocaleDateString('en-US');
};
📚 Tools & Resources
- Refactoring Guru: DRY Principle
- The 12-Factor App (Principles for modern apps)
- VS Code extensions like SonarLint help catch repeated logic
✅ Conclusion
DRY vs WET isn't just a code pattern — it reflects your mindset as a developer. Clean, maintainable code helps future-proof your apps, reduces bugs, and boosts collaboration.
- ✔️ DRY: Reduce repetition, abstract repeated logic
- ❌ WET: Avoid copy-paste logic that breaks easily
- ⚖️ Balance: Don’t over-abstract — keep things readable
💬 What’s your approach to clean coding? Let me know your thoughts in the comments!
🚀 Try using the DRY principle in your next project and feel the difference!
Hi Everyone, please do not spam in comments.