DRY Principle Debate
Comments debate the trade-offs of the DRY (Don't Repeat Yourself) principle versus accepting code duplication to avoid premature or poor abstractions that reduce readability and maintainability.
Activity Over Time
Top Contributors
Keywords
Sample Comments
You are violating DRY principle. Copied code, with minor variation, spread out through a codebase is, seriously, the root of all evil.
duplicate code is not that bad. reduce duplication over time as you find the common patterns/abstractions, instead of trying to build abstractions too early
Can you expand om what of code duplication you deem reasonable?
This is a trap junior devs fall into DRY isn't free it can be premature optimization since in order to avoid copying code you often add both an abstraction AND couple components together that are logically separate. The issues are at some point they may have slightly different requirements and if done repeatedly you can get to a point that you have all these small layers of abstraction that are cross cutting concerns and making changes have a bigger blast radius than you can intuit easily.
Couldn’t agree more. I can agree that premature abstraction is bad. But unnecessarily duplicated code outweighs this by orders of magnitude, in my experience. So stay DRY, and later when you hit that 1 case in 100 where the code needs to diverge, it’s easy enough to copy/paste.
New devs start with doing DRY everywhere. Over time they learn to be more thoughtful. Sometimes duplication is good. In my experience the priority should always be dev readability. If duplication helps you read the code better (as its not hidden away), thats fine.
It's easy to duplicate code. But hard to delete shared code.
The idea of 'don't repeat yourself' is an ok guideline unless, like most proverbs/sayings/mottos/slogans it gets used as a absolute rule.feel free to ignore it, mock it and toss it aside if it leads to bad abstractions, highly convoluted structures or write-only code.anyone that has had to do maintenance or adding features to a large OO codebase from the early 2000s will have seen vastly massive class hierarchies where following the path of execution is a roll
Duplication is always more extensible and maintainable than a bad abstraction.
Sort of falls under the DRY (Don't Repeat Yourself) principle.