Can You Cure the Copy/Paste Disease?
It's a wacky idea - but sometimes I'm inclined to agree.
Maybe it shouldn't be completely illegal - but it could be harder to do (in specific circumstances), or tracked more closely.
At one point (perhaps long ago, perhaps today...) I stumbled onto this chunk of code [at right], and I thought "Hmm, interesting lack of reuse..."
Why use a data structure when you can copy and paste your code?
It's only a little code smell, right?
Not really, because you know that where people have the 'copy/paste' bug... it's only going to get worse.
Let's zoom out and see what we find...
The code sits inside this method... -->
(There's other issues there too, but let's leave them aside today, please.)
And the very next method down is almost identical -->
Then - I look at the next class - it's an almost identical class, and it has two methods of its own with identical names to the ones above and only very slightly different code...
So we're talking about a serious case of "Achieves reuse through the cunning application of copy and paste"
And you know it doesn't stop there.
It just keeps going - thousands of lines of copy/pasted code. File after file. Bugs here and there (notice the 'harmless' bug in the first snippet?) quirks abound.
It's a fractal! The more you zoom out, the more you see the same pattern copied, pasted, modified, and reapplied over and over at greater and greater levels of scale. The author's entire career could be composed of the same programs, copied, pasted, and modified, retrofitted to slightly different problems.
Copy/Pasting is like the weather. Everyone complains about it, but nobody does anything about it.
If copying and pasting were illegal (or more difficult) - would the implementer have thought about achieving reuse through other methods?
Illegal is taking it a bit far. So: how could the copy/paste virus be reduced?
Maybe clippy could pop up and say: "It looks like you're copying and pasting code. Do you know that you can achieve re-use in other ways?" (Maybe we could use Scott Bellware's face on clippy? eh?)
How about this... every time you use the "paste" function inside your IDE, a little counter is incremented. And that counter is displayed on a gigantic, glowing LED panel suspended from the ceiling above your desk.
Or: Every time you use the "paste" function inside your IDE, a loud buzzer sounds for ten minutes, and a flashing red siren pops up over your desk.
Or: Every time you use the "paste" function inside your IDE, your screen is locked out for ten seconds.
I think you'd only need to apply a technique like that for a few minutes a week and you'd soon be permanently cured of copy/pastitis.
My friend OJ suggests...
"an enterprise Copy and Paste server, which acts like a petrol tank. Hence you only get to use your copy and pastes wisely before you run out."
Perhaps a plugin would need to detect when code is copied and pasted from within the ide. It would only punish you when the copied code (and subsequently pasted code) contains some degree of logic.
And before you say it -- I know there's a tool called Simian that you can plug in to your continuous integration server, to detect this sort of nonsense. I haven't tried it, but I'd like to know more. (They're Australian by the way)
Also -- an apology
Let me be crystal clear about one thing... writing this kind of code doesn't make you a bad programmer.
This particular code was written by a very successful programmer, who does an excellent job of delivering value to the business.
It is definitely a better example of programming than a hell of a lot of code I've seen in the business world.
I'm just decrying the fact that sometimes we work harder not smarter, and if we trained ourselves into the right coding habits, we could achieve both: work hard and smart.
Tools for Tackling Copy-Pastitis
- Clone Detective (free Visual Studio plugin)
- Simian (similarity analyser)
- Axivion Bauhaus Suite (Stopping Software Erosion)
I'm currently writing a book about how to build your first product. If you want to build your first product, please sign up to be notified when the book is available.