14 ms·
In my case when I was junior I tried to be very smart and try to DRY a lot, but I found that most of the times is better to write "dumb" code and repeat yoursel
by norman784 4y ago
In my case when I was junior I tried to be very smart and try to DRY a lot, but I found that most of the times is better to write "dumb" code and repeat yourself if the complexity is not worth, also as you stated if your function is used in a lot of places for slightly different things is just so easy to break something without noticing and also harder to test.
So I agree with you, as developer you should know when do duplicate code and when DRY, but overall try to maintain your code as simple as possible, that makes also easier to maintain.
- nicbou 4y agoThis works until someone updates code in one place, but not the other, and subtle bugs are introduced. DRY / single source of truth offers a certain protection against such bugs.
- philliphaydon 4y agoOnly experience teaches you where to apply DRY and where not to. Sometimes just because something looks the same or is similar does not mean it’s the same. Applying DRY just because it looks the same can have the unwanted consequence of changing in 1 place changing in another too when that’s not the desired affect. Then you add another parameter and conditional logic just because you don’t want 2 similar looking things.
- chrismarlow9 4y agoOut of all the comments on this post, this is the one I agree with the most. I have been bitten by "not enough DRY" as well as "too much DRY". I think experience as well as the maturity of the code are most relevant when deciding things like this. If you do this too early you will back yourself into a corner or eventually end up with a "god function" (usually the outcome of what OP mentions about conditional logic and more params). If you wait too late you will inevitably have a giant codebase with lots of duplicates everywhere. In my experience it's easier to clean up duplicated code than it is to break apart a "master function" that nobody has touched in 3 years out of fear.