re: Coding practices your future self will love you for VIEW POST


It is a good idea to copy-paste the same function a minimum two times

No. I'm steadfast against this. The first copy-paste is already reason to abstract the code. If you don't you'll have already duplicated bugs, and limited the functionality of the app. You'll also be encouraging yourself, and team-mates to copy it a third time -- since realistically, how will you know somebody has copied it once before?

Abstracting code in a clean way can be challenging. This is not a reason to avoid it. If you constantly avoid doing this you will never learn how. Creating an abstract form from two functions is easier than from three related functions. Do it right away. There's absolutely no reason this should result in less clean code.


This article explains it well why WET is better than DRY:


I had to go through some WET code in a project I'm working on today (code by a peer) and nearly threw up. Copied and pasted an entire method from one place to another. The code was tripe in both places and needed to be fixed twice. And you know what? I had to spend time going through the code to make sure it was exactly the same before I could be sure that the fix would work in both places. WET does not save you time if you follow it blindly. That's why it isn't better than DRY and that article does not say it's better than DRY. It says that sometimes (with small bits of code) it's fine to repeat it.

I think you are 100% correct; following principles blindly does not sound like a good plan. The only absolute rule in programming is that there are no absolute rules


That's the whole point of premature abstraction. In my experience, just because 2 things look the same initially doesn't necessarily mean that they will evolve to be the same. Obviously there are tiny functions such as "add 2 numbers" which will still remain the same and hence can be abstracted early.
As with any rule, apply with care and don't over-stretch it. The answer to most questions in programming (and life) are "It depends" :D


I'm also strongly against the WET principle. Having good unit tests and code coverage is important. If you go with the WET principle you are making it harder to write tests for your code. If you go with DRY and then you later find a function doesn't meet your requirement, don't use that function and rewrite the code - simple!

Code of Conduct Report abuse