Rant: Copy and Paste Programming

Let me forwarn you that you may be offended by this post… it may come off as a bit hostile and derogatory, but it’s just something I need to get off my chest. When someone thinks it’s entirely acceptable to write a 300 line method and call it “the quickest and simplest thing to do that works” and then copy and paste those 300 lines 10 times and modify maybe one or two variables to cover other scenarios, you don’t deserve to even have a job programming.

I’m sorry, but I don’t care what your time constraints are. I don’t care that you just learned the language. And I don’t care you sat down and thought “OMGZ! I need to get this done yesterday!” The fact that you did not just try to use that thing you call a brain and THINK about what you were doing and just reduced yourself to a CTRL+C and CTRL+V keyboard monkey reduces any possiblity that I could respect you as a programmer. Honestly, did you get through school by copying homework assignments? Did you get the genius next door to do your work for you while you were out partying, and luckily had awesome memorization skills to pass exams?

A skilled craftsman is one who devotes their time and energy to honing their skill. They may start out knowing very little, but through dedication and at least using their brain they improve, and even if their code isn’t perfect, at least they gave it effort. By just mindlessly copy and pasting you didn’t even make an effort… you’re just lazy.

When you need to do something similar to something else, maybe it’s okay to copy and paste the first time, change a few things, then look at what you can do to consilidate both tasks and make the same code useable in both situations. But doing the whole copy and paste and saying “I’ll refactor it later” is idiotic… you know that “later” is never going to come, and with that attitude you’re just going to keep doing it over and over to the point that “refactoring it later” will take you 30 days.

Let’s just be honest to ourselves… there’s really no reason you should copy and paste your code. Period.

  • http://vlod.blogspot.com vlod

    I’ve been in this industry for over 12 years and I see this a *lot*.

    I share your sentiments. There are a substantial amount of people who don’t view their work as a craft and just want to move on to their next piece asap.

    I think one of the factors is that these people never seem to clean there crap up.
    It’s inevitably left to people who do care, to fix ‘just one little thing’ and it turns into one big refactoring project. Then the comments of how long it took to do such a small enhancement. grrrhhh..

  • neuro

    If you are java developer you can use some good tools: JetBrains IDEA contains smart duplicate detector (which will not be tricked by changed identifiers or expressions) and TeamCity 2 can provide you regular browsable reports for new dupes.

  • http://technology.amis.nl Jeroen van Wilgenburg

    This sounds so familiar. A while ago I wrote a blog about this subject after reading 15.000 lines of code. Those 15.000 lines should have been about 250 lines, but someone loved copying around:

  • http://www.onagile.com Ryan Cooper

    Great post. I couldn’t agree more. I don’t know how many times copy-paste (all in the name of meeting the deadline) has come back to bite us on the ass on my current project.

    I blogged something along a similar vein just yesterday: http://on-agile.blogspot.com/2007/04/why-you-wont-fix-it-later.html


  • Idetrorce

    very interesting, but I don’t agree with you

  • chilly

    i started programming not that long ago,im only 14 but i agree with you. There’s a lot more job satisfaction doing it the proper way.

  • Mike

    I can’t agree with you more. I too need to get something off my chest…

    We are a 3 man team making big websites.

    On Friday we got a call from a company we are thinking of partnering with. An urgent request for us to help fix their site css. Their client decided they do want to support IE6 after all. We go down to their office and I install their PHP based site on our machines. They have a team of about 20 people total, 4 of them working on CSS, and 3 developers.

    I found 3 stylesheet files containing 4000, yes four thousand lines of CSS between them – for the home page alone. This is not your normal CSS, nicely done with one attribute per line and spaces in between. This is 4000 lines with one CSS style rule and about 10 attributes on EACH line, and no spaces in between or comments. Almost every line is over 120 columns wide. All hand edited. Their main programmer told me it took him 3 days.

    I was breathless…

    They had a problem with their site PNGs, which are not displaying in IE6. So I installed sleight.js, a fair bet for solving this problem. I found it very difficult to get working and on inspection found they were using jQuery hacked together in various bad ways together with 7 other of their own scripts which equally looked like hacked up bits of other code.

    Now, judicious modification of other people’s code I can understand. However, hacking minified javascript I find challenging to my understanding of human ‘intelligence’… here is an excerpt:

    prototype={useExpressInstall:function(_d){this.xiSWFPath=!_d?”expressinstall.swf”:_d;this.setAttribute(“useExpressInstall”,true);},setAttribute:function(_e,_f){this.attributes[_e]=_f;},getAttribute:function(_10){return this.attributes[_10];},addParam:function(_11,_12){this.params[_11]=_12;},getParams:function(){return this.params;},addVariable:function(_13,_14){this.variables[_13]=_14;},getVariable:function(_15){return this.variables[_15];},getVariables:function(){return this.variables;},getVariablePairs:function(){var _16=new Array();var key;var _18=this.getVariables();for(key in _18){_16[_16.length]=key+”=”+_18[key];}return _16;},getSWFHTML:function(){var

    Yes, they were hacking this. It would be funny if it wasn’t so terrifying.

    Anyway, to ‘help’ me they decided to copy these 4000 lines of CSS in 3 files plus literally hundreds of other smaller CSS files into a IE6 svn ‘branch’ (actually just a new folder in their trunk – which they were all working on), for a total of 6000 new lines of unnecessary CSS. I told them not to bother because I would not touch them but the programmer had already written the browser sniffing code in PHP, and so I just carried on.

    His change broke the CSS altogether on the home page on my machine, so I had to fix it first. That done, and after getting the PNGs displaying properly, I did another svn update on my machine and checked the other pages. Some were working, some not. Not my fault, I haven’t even checked anything in at this point.

    I went to the live website and gasped as I saw that the same pages were broken. Their updates had been pushed to the live server. Bear in mind that all this is happening on their customer’s site, running LIVE, during the first week of their website launch.

    I then ran a find on my working copy for his changes. He had pasted his bad browser sniffing code into every single page in the site, turning a non-issue into a regression and then multiplying it 35 times over, creating hours of headaches for himself and the other team members, days before the deadline. In desperation he had then manually fixed each copy-pasting by hand, together with various other merged in workarounds from other developers, but not all of them were fixed in the same way, compounding it into a permanent brain tumour in the client facing parts of the system,

    Their lead technical guy had just taken a massive PHP flavoured dump right in the middle of the website source code.

    I wandered around the office and looked at people’s machines. None of them were running IE6, except for that one guy on a virtual machine, as they are all running Vista which does not support multiple copies of IE. Everyone else is attempting blind fixes or hacking the IE6 CSS. I then realised – they were all using the live server for testing their changes.

    The scariest part of this story, is that until we arrived and started asking WTF? it seemed to them that none of what they were doing was a problem. They all seemed quite confident in their website and the way they were making it.

    However, whether they realised it or not, they had worked together as a team to create their own fiery hell and were already roasting within it.

  • polyglot

    The scenario you describe is extreme, but as a general rule of thumb, programmers (including highly skilled and experienced programmers) duplicate code all the time. This is for many reasons, but one key reason is that the abstraction primitives (methods, classes, etc.) provided by your favorite programming language, just by their finite nature, can’t capture all the ways there are to abstract code. Moreover, some patterns simply cannot be abstracted and duplication is absolutely required. This type of duplication is often inapparent in the programming language and resides closer to the programmer’s mind.

    Yours is a classic argument about the tension between education and reality. Should we educate everyone to do things in a principled way, so that the problem can be avoided in the future? Or should we focus on the problem now? From the former perspective, we should invest more energy in figuring out what it means to abstract, and then teaching people how and why to abstract. From the latter perspective, we should provide support for copy & paste programming, such that you can copy/paste/change freely and underlying technology tracks all the copies and ensures that a change in one is propagated to the others, etcetera. Basically, do you care about fixing the distant future, or do you care about addressing the now?

    Either way, what you’re expressing here is one opinion among many, and I suppose the purpose of this post is to inform you that the issue is philosophical and not straightforward at all.

  • Mike

    Sorry, I don’t buy that at all.

    It’s a straightforward combination of human laziness and naivety, and it’s a lame excuse to blame such programmer faults on the language, or to dismiss it as a ‘philosophical issue’.

    Those coders weren’t doing their job properly.

    There is an old saying in English: ‘A bad workman always blames his tools’. Now, OK, you can’t dig up a road with a teaspoon, but with the vast array of tools which are immediately at the disposal of each and every programmer these days, nobody can really use this excuse any more.

    The developers in my anecdote would be the equivalent of carpenters who couldn’t use a saw, or mechanics who just fixed everything by bolting more metal on to it. Would anyone wish toemploy someone like that to put in a window or fix their car?

    The simple truth is that any language that is Turing-complete is capable of expressing anything that is expressible in any other Turing-complete language. In any language where you can define reusable code, code duplication is *never* required, the programmer simply hasn’t realised the abstraction.

    But my point is that, through programmer laziness (to learn proper techniques and understand how to write code), these developers were writing extra code to solve non-problems.

    This is something that irks me with so much software – it is sometimes totally obvious that the programmers have spent a lot of time and effort to make things worse, and then often call those things ‘features’.

    As an example: my old windows mobile phone, when it was running low on battery, would wake up to tell me that I should turn it off. When I did so, it would wake up to tell me again 15 seconds later, and so on until the battery was empty – braindead.

    So, to answer your question, I do care, deeply, about the distant future, for the simple and invariable reason that, by the time I have struggled through the present problems I and others created for me in the distant past, the distant future will be upon me with the problems being created now, and frankly, life is too short.

    Next is now. Now was then.

  • http://no Maxime

    I am currently studying computer science in Montreal, Canada and my C++ (data structure) course partner is a copy paste programmer, I even entered an argument with him about me actually thinking about resolving the issue and him trying hard every snipper of code he found , to fit in our project instead of putting effort into reflexion and care about the actual work. Always wanting the easy way out is one hell of a way to make it always harder.