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.
We've all seen them. The developers who seem to effortlessly untangle complex problems, whose code…
If you're building a social, chat, or comment-based mobile app using React Native, protecting your…
The Cross-Platform ImperativeLet's face it: building separate iOS and Android apps wastes resources. React Native…
We live in an age of infinite distraction and overwhelming ambition. Grand goals shimmer on…
Reusable components are modular UI building blocks designed for versatility. Instead of writing duplicate code…
What is a Palindrome? A palindrome is any word, phrase, number, or sequence that reads…