You know that thing you do? Where you copy a chunk of code, paste it somewhere else, tweak one tiny thing, and tell yourself, “Eh, it’s fine for now …”
You need to stop that.
Seriously. Your future self will send you hate mail.
But let’s get into our topic for today.
The DRY concept is just a coding tip – it’s a survival strategy.
DRY is an acronym for ‘Do Not Repeat Yourself’.
Every piece of knowledge or logic in your system should have a single, unambiguous representation.
(Sounds fancy, but stick with me.)
If you’re writing the same idea twice, you’re doing it wrong.
Put it in one place, and refer to it everywhere else.
Got logic used in multiple spots? Wrap it in a function.
// Wet (soggy and sad)
const total1 = price * 1.08; // Tax calc
const total2 = price * 1.08; // Same tax again?!
// DRY (ahhh, refreshing)
function addTax(price) {
return price * 1.08;
}
const total1 = addTax(price);
const total2 = addTax(price);
Reusing UI? Make a reusable component.
(React, SwiftUI, Flutter – they all beg you to do this.)
Hardcoded values (like tax rates, API URLs) repeated everywhere?
NO: const user = fetch("https://api.myapp.com/user") in 27 files.
YES: const API_URL = "https://api.myapp.com"; in one config file.
That fancy date formatter? That validation regex? Put it in a utils folder. Share the love.
Don’t force abstraction where it doesn’t fit.
If two pieces of code happen to look similar today but could change separately tomorrow? Leave them alone. DRY is about knowledge, not coincidental resemblance.
Readability is GREATER THAN Dogma:
If removing duplication makes code harder to understand, you’ve failed. DRY shouldn’t feel like solving a Rubik’s cube blindfolded.
Beware of Premature Optimization:
Is it a tiny, trivial string used twice? Maybe it’s okay. Don’t architect a NASA-grade helper for isActive: true.
Ask yourself:
If this rule/logic changes tomorrow, how many places do I have to update?”
If the answer isn’t “one”, you’ve got work to do.
DRY isn’t about being fancy – it’s about coding with self-respect. Less repetition, more vacation.
Now go delete some duplicates. I’ll wait.
Latest tech news and coding tips.
Here’s a comprehensive, clear differentiation between a Website and a Web App, from purpose all the…
Visual Studio Code (VS Code) is powerful out of the box, but its real strength…
1. What Is a Variable in JavaScript? A variable is a named container used to store data…
1. What Is a Queue? A Queue is a linear data structure that follows the principle: FIFO – First…
Angular is a full-featured frontend framework built by Google for creating large, maintainable, and high-performance web applications.…
What Is Responsive Web Design? Responsive Web Design (RWD) is an approach to building websites…