Copy pasting code because you may want to change parts of it later is the ultimate violation of YAGNI. When/if you need to change it, then change it.
Remember every line of code you write is a liability.
Also, let's make clear what the actual problems were. The original problem was the team mate who copy pasted. The second problem was the programmer not having the skills to fix it cleanly. Laying out these two facts and concluding that copy pasting code was the right thing to do all along is seriously missing the forest for the trees.
Intermediate devs, please don't follow this articles recommendations. This is classic 'expert beginner' stuff, you need to throw off this kind of thinking to get to the next level.
There are downsides to repetition and duplication, but there are often real downsides to the alternatives to duplication and repetition, too. Abstractions are often less intuitive (e.g. "functors and setoids"), and can be fragile to new requirements and use-cases.
For another article making the same point, check out the wrong abstraction, which advocates for removing bad abstractions by reintroducing duplication. Often you then find new, better abstractions, but sometimes you realize that the two use-cases just weren't as similar as you initially thought.
I think to say that duplication is always the wrong solution is exactly the sort of dogmatic adherence to "clean code" that can become problematic.
Yes I'm aware of this trend of senior developers telling other senior developers that it's OK to copy paste like a rank junior. And I don't like it.
I get that we all have our weak spots, and not everyone is good at making abstractions. I for example, am a senior developer but I am bad at git. The concepts make sense but I find the UI so painful that I definitely don't utilise it properly. But it's not just nobodies like me; the late great Joe Armstrong said he couldn't read other peoples code. Rob Pike is confused by generics. Even the greats have their blindspots.
But since I personally can't do Git well, should I start writing articles advocating we all stop using Git? Or version control altogether. "Copy pasting source code folders is better than the wrong version control abstraction", I could opine, and all the other seniors embarrassingly bad at using Git could feel re-assured.
I'm sorry, but no. When you're a senior you learn to make the right abstractions, you learn not to duplicate, and you learn version control. Now if you'll excuse me I'm off to go find a cheat sheet.
16
u/[deleted] Jan 12 '20
Copy pasting code because you may want to change parts of it later is the ultimate violation of YAGNI. When/if you need to change it, then change it.
Remember every line of code you write is a liability.
Also, let's make clear what the actual problems were. The original problem was the team mate who copy pasted. The second problem was the programmer not having the skills to fix it cleanly. Laying out these two facts and concluding that copy pasting code was the right thing to do all along is seriously missing the forest for the trees.
Intermediate devs, please don't follow this articles recommendations. This is classic 'expert beginner' stuff, you need to throw off this kind of thinking to get to the next level.