DRY — is good, but not always!

Wed, May 7, 2025 - 2 min read
dry is good, but not always

🔁 DRY — is good, but not always: when duplication saves the project

We’ve all had it drilled into us: Don’t Repeat Yourself!
Duplication is evil, they say, and all code must be reused.

The idea is valid. But like every principle in development, it doesn’t work every time.


🛠️ My experience: a popup component turned into a monster

On one project, we built a universal popup component.
We wanted it to work for every scenario: login, confirmation, errors, settings, invites, and more.

At first, everything was great. DRY. One component. Convenient. Elegant.

But over time it grew:

  • 30+ props
  • nested if/else statements
  • dozens of states
  • custom renders for various cases

At some point, this component became a 1000+ line file.
Testing became hard. Maintaining — terrifying. Using it — confusing.


💡 What should we have done?

We realized it would’ve been easier to create three separate components:

  • <LoginPopup />
  • <InvitePopup />
  • <ErrorPopup />

They would’ve been 80% identical, but:

  • Their names clearly indicated their purpose
  • Internal logic was linear and predictable
  • Easier to maintain, easier to read
  • No need to remember which props to pass

🤯 Why DRY can be harmful

  • DRY works well within the same layer (UI, logic, styles).
    But mixing layers just to reuse code leads to pain.
  • DRY creates abstractions, and they only work if changes happen in sync.
  • When components serve different scenarios, merging them leads to flags and exceptions.
  • Abstractions can become so universal, no one but the author dares touch them.

✅ Where DRY works well

  • Utilities: formatDate, getAgeFromDate, truncateText
  • Styles: shared buttons, spacing, fonts
  • Business logic with consistent operations

⚠️ Where DRY can backfire

  • UI components with different semantics and context
  • Merging multiple scenarios into one function
  • Trying to reuse what would be simpler to copy

🧠 What to remember

DRY isn’t a law. It’s a guideline.
Sometimes code duplication means readability and simplicity.

Three clear components are better than one universal monster.


🔚 Conclusion

Write code your future self and your team can read.
If that means repeating a few lines — so be it.

🤘 Don’t listen to dogma. Use your brain.
Sometimes DRY is hell. And sometimes — salvation.
Learn to feel the difference.


Subscribe to EasyAdvice — and don’t be afraid to write code that makes sense to you.