4 ms·
Sounds more like too little care rather than too much. Of course it also depends on how much maintenance is going to occur on that code. I think in production c
by cjfd 7y ago
Sounds more like too little care rather than too much. Of course it also depends on how much maintenance is going to occur on that code. I think in production code one should already look for an abstraction if something is done twice. The goal is not to create a work of art or an abstraction that stands the test of time. It is just to remove some duplication, distribute code a little better over functions, give things names that are somewhat better and so on. If one does a bit of that every day one is never going to be in a horrible mess. One problem is that people sometimes think that refactoring is all or nothing. You either refactor nothing or you go all the way to adhere exactly to design patterns. Neither of these two extremes produces very good code. The middle way is where the good code is.
- pure-awesome 7y agoI used to think that if something is done twice, it should be abstracted. Now, I'm a bit more careful. There's a pretty famous quote by Sandi Mentz: "Duplication is far cheaper than the wrong abstraction." https://www.sandimetz.com/blog/2016/1/20/the-wrong-abstraction https://www.sandimetz.com/blog/2016/1/20/the-wrong-abstracti... Make sure that you're picking the write way to think about the task at hand, rather than blindly following DRY. There are times when even a single instance of a code call would be made clearer with abstraction, and there are times where having the same piece of code duplicated multiple times (or duplicated with one piece changed) is far clearer than trying to abstract it. This Reddit Comment also has an interesting take: https://www.reddit.com/r/programming/comments/5txp5t/duplication_is_far_cheaper_than_the_wrong/ddq5ykf https://www.reddit.com/r/programming/comments/5txp5t/duplica... > The main purpose of abstractions is not to remove or reduce duplication, and not even to make code "reusable"; it is to make semantic patterns and assumptions explicit and, if possible, first-class. The further comments provide more discussion. --- I agree with the rest of your comment that refactoring and code-cleanup should be done in pieces and that, as with everything, striking the right balance is key.
- cjfd 7y agoIf there is any article that I absolutely hate it is 'Duplication is far cheaper than the wrong abstraction'. A somewhat minimal abstraction has very little chance of being wrong. And if it is wrong, is it really that difficult to know? It has a chance of needing some improvement, but what code does not have a chance of needing some improvement? If one goes the full monty and introduces three design patterns every time that two lines are duplicated sometimes one will certainly end up with, perhaps, not so much the wrong abstraction as an overly convoluted one. This acticle is the excuse for programmers everywhere not to fix their messes. It is 100% opposite to what programmers need to hear.
- mcphage 7y ago> It is 100% opposite to what programmers need to hear. Maybe you know very different programmers than I have known, but if I had to compare what has been a bigger source of problems—missed abstractions, or abstractions that make things more difficult for no benefit, it's definitely bad abstractions almost every time.
- cjfd 7y agoWell, I worked for quite some time in a code base that was basically one big ball of missed abstractions. Maybe I was traumatized a bit by that. I also have seen wrong abstractions, but not because there should not be an abstraction. If people had actually written abstractions that removed duplication instead of the ones that they half-understood from the design pattern book, they would have had come up with better ones. Duplication is in fact the best way to find the correct abstraction. The article makes this sound suspect but it really is not. The whole literature of design patterns is basically unneeded. If one just follows the path of deduplication one will discover all of them one by one and one will have applied the right one in the right place. Maybe there is some value in noticing that one still has to keep thinking and keep an eye on the likely future and not blindly do this but that is no more than a small footnote to the main message which is that you will find all the right abstractions by removing duplication. It is quite right that in TDD the refactor step is often described as the removal of duplication.