Let's write a cache in JavaScript and see what it means to keep things simple.
We always hear software developers saying things like we should kee...
For further actions, you may consider blocking this person and/or reporting abuse
Even after reading the article, I have to disagree and think the DRY-principle still should be applied and in fact, you should be afraid of copying code for all the reasons, one wants to apply DRY and keep maintainability.
You're already showing it by the last example, where you dependency inject a cache. The expire-Cache is basically a wrapper to an original object which might be perfectly suitable. However, calling it "copy code" might be misleading, especially for new devs.
To be more precise:
Every time you implement a feature by simply adding one more IF statement, you help growing it further – the big ball of mud.
I think this is the exact point, where you need to immediatly stop your work(!) for the moment to think about what you're doing and why you're doing it!
In your example, you had a simple feature, which had one purpose and it served it well. After a while, you got already 2 times additional feature requests, that should be implemented for your feature.
You're "simple" feature has now outgrown it's state of "simple" to a crucial part of your application and needs refactoring!
The "IF-Statement" addition is just patch for the bullet wound you've just identified.
Instead you should now think of treating it well, with (non-exclusively) the following thoughts:
Refactoring
For Object-oriented languages:
Overall Design/ architecture Thoughts
Adding an IF-Statement is a quick-fix and sometimes necessary when there are close deadlines (combined with a TODO:-Comment to refactor it later) - and I fully understand that it sometimes needs to be done.
But real duplication of code is like seeding and watering our challenge, causing the future developer (who might be you!) to face a real problem!
Edit:
Added a sentence to make it sound less offending ;)
The main problem there is that you are not using semicolons 😭
😆
Thanks for sharing that. That's a way of handling this type of problem that I hadn't seen before. Can't wait to try it out myself.
Great post!