Copy and paste coders
May 31, 2011 Leave a Comment
Or cowboys as I like to call them
Actually, that’s a little bit harsh. Just because you have a tendancy to use your clipboard a little bit more than most doesn’t necessarily make you a bad programmer, although it might shift the odds a little.
That said, copy and paste coding is fast becoming one of the things that annoy me the most during my day-to-day software development job. I’m not going to pretend that I’m whiter than white and I can’t say that I’ve never done it myself, but that was a long time ago and I wasn’t as experienced as I am now. Plus I’ve been caught out by it, and I’m pretty sure that I don’t want it to happen again.
As more and more development libraries and APIs become available, Java developers are fast moving from being a skilled programmer, able to delicately craft and sculpt classes and methods, to a semi-skilled plumber who knows where to find the right libraries and knows a minimal amount of code to connect it all together. The problem is that these plumbergrammers don’t always create those pipe connections to the highest quality, instead choosing to copy the example from the API website or some newsgroup. No big deal, well not until the server room ends up getting flooded, the copy and paste coder doesn’t understand enough to know where the leak could be coming from.
It’s not only when using new libraries do copy and paste coders run into problems. In my experience, copy and paste coding is usually done for a couple of reasons. In the first instance, the coder is asked to extend a class or method to do something new, maybe for a new content type. The new functionality works in pretty much the same way as the existing code, so the exisitng functionality is copied into the new location. Maybe we get a little bit of refactoring at this point, but don’t expect to see too many variable names change and worse still, you might only see content types change so that the variable name is no longer consistent with the underlying content type. The code runs and does what it’s supposed to do. Job done.
The other main reason I’ve come across for using the copy-and-paste pattern is to add a piece of functionality to code using a “tried and tested” method. I put quotes around tried and tested, as in most cases it’s not been tested that much and it’s more “seemed to work last time” code. Reuse of code, or a developers library, isn’t necessarily a bad thing but in most cases the code in question isn’t exactly bullet proof and might not be suitable in this instance.
And this brings me to my biggest bug-bear with copy and paste coders. It seems that some copy and paste coders (and I’m not going to say all) don’t really think about what they are doing. Just because something worked last time, or worked in a particular scenario doesn’t mean that it’ll work again and doesn’t mean that it’s applicable to the current situation. Everytime you hit that Ctrl-C Ctrl-V keystroke it’s likely that you’re doing something wrong and the stuff that you’re producing isn’t really the standard that it should be.
I’d like to extend this to code found on the internet. Google and StackOverflow are pretty much the Utimate development tools if used correctly, however copying code from the internet usually comes with a few downsides. Jeff Atwood looked at this problem on his blog and though I’m not really sure how viable adding a GUID to each code snippet on the web is the right way to go, I agree with the points that he makes. Code found on the internet has usually created to demonstrate a particular purpose and might not be production ready. If you take it, you won’t get any updates that the original developer makes and you might not even be copying the most recent version.
However on the flip side, whatever you’re doing it’s likely that somebody else has already done the hard thinking and got something up and running before you and fixed all the niggly issues you’d find developing it yourself. And chances are that using StackOverflow or Google and the right search terms that this code is going to be easy to find. If the site that you’re taking the code from has a particularly large audience then it’s probably going to get a more thorough code review than anything you write. If it’s taken from a blog, or other social platform, the comments around the code will give you an indicator of quality.
So the next time you head for that copy and paste keystroke, just think about what you are doing. Is it really the best option? Should you be refactoring this into a new class? Is there a better way of acheiving your goal? I’ve found that the majority of the time the answers to questions should lead you to another solution and stop you hitting Ctrl-V. If you still think that you must, make sure that you understand what the code that you are copying is doing. There’s not much worse than a developer that doesn’t understand their own code.