3 ms·
> Code duplication is better than the wrong abstraction. The explanations of this statement that I’ve read make no sense to me. They amount to “using an abstr
by ken 7y ago
> Code duplication is better than the wrong abstraction.
The explanations of this statement that I’ve read make no sense to me. They amount to “using an abstraction for two things which just happen to be the same today” is bad. Well, obviously.
It’s like seeing that your tire pressure is 35 psi, and the speed limit is 35 mph, therefore #define speed pressure.
It’s really not difficult to see when A=B by definition or by chance. I no longer follow the Rule of Three, and I can’t recall any situation where I thought I’d wasted any time on an abstraction that didn’t end up working. I have spent a lot of time dealing with duplication when someone copy/pasted a second case (presumably a follower of the Ro3), even though it was obvious from the start that the two cases would always be identical.
- marcosdumay 7y ago> It’s really not difficult to see when A=B by definition or by chance. Some times it is. On those times it's better not to abstract prematurely. Some other times it isn't hard to see the equality is by definition, but it's really hard to see what is or isn't covered by the definition. On both cases, if you get more examples, your life gets easier. That's why people took this wisdom and compressed it into an easily digested pill inexperienced developers can use to treat all their duplication needs, either it helping or harming.
- earthboundkid 7y agoLiterally an example from today: updateDescription(image) { let description = window.prompt( "Update description", image.description ); if (description !== null && description !== image.description) { actions.doUpdate(image, { description }); } }, updateCredit(image) { let credit = window.prompt("Update credit", image.credit); if (credit !== null && credit !== image.credit) { actions.doUpdate(image, { credit }); } }, I waffled on whether to deduplicate it or let it be. What was the Obvious Right Choice?
- KajMagnus 7y ago> I waffled on whether to deduplicate it or let it be. What was the Obvious Right Choice? What about adding a one line comment that this code can be refactored and improved, and how? But not doing anything immediately. If you run into this annoying code repeatedly, then, it can be worth taking the time to refactor it.
- earthboundkid 7y agoHmm, maybe I should wait until there are N examples of the pattern. Maybe 4 > N > 2 ??? :-D
- KajMagnus 7y agoWhat if N becomes 4 o.O then the weird code will be there forever? :- )