Categories: softare development

The DRY Concept (Don’t Repeat Yourself)

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.

What DRY Really Means

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.)

What This Means

If you’re writing the same idea twice, you’re doing it wrong.
Put it in one place, and refer to it everywhere else.

Why Should You Care?

  1. The Bugpocalypse:
    Imagine you copy-pasted a calculation 10 times. Then you find a mistake. Now you must fix it… in all 10 places. Miss one? Congrats, your app just charged $10,000 for a $10 item.
    DRY Fix: Fix it ONCE. Save hours. Avoid bankruptcy.
  2. The “What Does This Even Do?” Confusion:
    Duplicated code lies. You tweak one version but forget another. Now two pieces of code that lookthe same behave differently. Debugging becomes a Sherlock Holmes mystery with less fun and more tears.
    DRY Fix: One source of truth = no contradictions.
  3. The “Why Is This App So Bloated?” Phase:
    Repeated code = bigger apps = slower loads = grumpy users.
    DRY Fix: Leaner code, happier users, happier you.

How to Apply DRY Like a Pro (Without Overdoing It)

1. Functions/Methods:

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);

2. Classes/Components:

Reusing UI? Make a reusable component.
(React, SwiftUI, Flutter – they all beg you to do this.)

3. Constants/Configs:

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. 

4. Helpers/Utils:

That fancy date formatter? That validation regex? Put it in a utils folder. Share the love.

But… Don’t Be That DRY Guy

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.

The Ultimate DRY Test

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.

Summary

  • Copy-paste is tech debt. You will pay it back with interest.
  • Write it once, use it everywhere.
  • Your future self is lazy. Be kind to them.

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.

Recent Posts

What Truly Makes a Great Software Developer

We've all seen them. The developers who seem to effortlessly untangle complex problems, whose code…

3 days ago

How to Filter Vulgar Words in React Native

If you're building a social, chat, or comment-based mobile app using React Native, protecting your…

1 week ago

How to Build Faster Mobile Apps With Native Wind Library

The Cross-Platform ImperativeLet's face it: building separate iOS and Android apps wastes resources. React Native…

2 weeks ago

The Surprisingly Simple Secret to Getting Things Done

We live in an age of infinite distraction and overwhelming ambition. Grand goals shimmer on…

2 weeks ago

How to Create Reusable Components in React JS

Reusable components are modular UI building blocks designed for versatility. Instead of writing duplicate code…

2 weeks ago

Check if Number, Word or Phrase is a Palindrome in JavaScript

What is a Palindrome? A palindrome is any word, phrase, number, or sequence that reads…

3 weeks ago