6 ms·
This advice just _feels_ very wrong. After thinking about it and seeing the other comments, some remarks: 1) It's fine to go back and duplicate code after you
by gm 6y ago
This advice just _feels_ very wrong. After thinking about it and seeing the other comments, some remarks:
1) It's fine to go back and duplicate code after you correct the abstraction. But it should be the _first_ phase in doing a larger pass to refactor code to fit the current business requirements. If you forgo the _second_ step, which should be to search for suitable abstractions again, you are absolutely guaranteed to be left with shit code that breaks in this situation, but not that other one, and no one knows why. I would absolutely only duplicate code as the prequel to deduplicating it again with updated abstractions.
2) If you do any of this without thorough unit tests you're insane. Keep the wrongly-abstracted code unless you have time to thoroughly fix the mess you will have made when you duplicate code again and introduce bugs (you're human, after all).
2a) If you are going to do this and there are no unit tests, create those unit tests before you touch the code initially (before the duplication).
3) Some of the comments saying you should wait until you implement something two or three times before creating an abstraction seem like comp sci 101 rules of thumb. It's way too simplistic a rule, way too general. Prematurely abstracted (haha!). The type of project and the type of company/industry will tell you what the right tradeoff is.
That is all.
- haolez 6y agoYou are assuming that the code is a moving target. Not every software project behaves that way. Sometimes, the software gets done as is.
- gm 6y agoIn that case, then the original problem (incorrect abstraction) does not exist, or at least does not get worse over time, and thus does not need fixing.
- roryokane 6y agoThe article already agrees with you on point 1: > Once you completely remove the old abstraction you can start anew, re-isolating duplication and re-extracting abstractions.