Copy and Paste programming – A bad habit


I usually stay away from religious discussions when it comes to programming habits and technology choices simply because they don’t usually make any sense at all and are most often just based on emotions. Arguing if VB.Net is better or worse than C# is just nonsense in my book since the differences between them are minor. They both target the same platforms, and use the same framework, of course there are differences but none of them are so great that you would say that this is a much better language. I personally think that XML literals in VB are great but that alone doesn’t make VB a better language. It is just easier to handle XML in VB, so if you do a lot of that VB is a better choice for that particular purpose. I also like the ease with which C# handles iterators, but again that doesn’t make C# a better language, it just make that particular code part easier.

imageAnother discussion I often see on the Web is if the Java J2EE framework is better or worse than the .Net framework. Again this just stir up emotions, if you’re a Java programmer you would of course say that Java is better than .Net and the other way around. It’s not true though… Java is not better than .Net and neither is .Net better than Java. It all comes down to which platform you’re targeting and what the requirements are for the project at hand.

Is an axe better than a saw? No, they are both exceptionally bad tools – if you’re trying to nail two pieces of wood together, that is. In that case you should use a hammer and not an axe and definitely not a saw.

Having said that, this post will still be a rant about a thing that really grinds my gears, copy and paste programming.

Why you should ban Copy and Paste 

I want to start by making one thing very clear, I do not think it’s a bad habit to copy a great piece of code you find somewhere on the web and then paste it into your project. As long as you test the code and perhaps refactor it to fit your naming convention then that’s just fine. What I talk about is the habit of copying and pasting the same code all around a project or even between different projects. Come on! If you’re reading this blog there’s a big chance that you’re a .Net developer and as such you use a modern OOP language. The whole purpose of object oriented programming is to create classes for code reuse and to preserve encapsulation. Even if you’re using a structured programming language you can still refactor your code so that you don’t have to repeat the same thing over and over.

Even though older code might be well used and therefore perhaps less buggy doesn’t mean that it will stay that way. Business rules changes and if you need to change how a particular piece of code works you should only need to do that in one place. Also, if the code does contain a bug, you have just introduced the same bug in another place. The next time you feel the urge to copy some code think again and cut it instead. Cutting the code and pasting it elsewhere means you’re refactoring your code and refactoring (to paraphrase Martha Stewart) is a good thing. Cut the code, paste it into a new method and call that method from the place the code used to reside.

If you’re working on a WPF or Silverlight project you don’t even have to copy and paste common XAML code, use a ResourceDictionary instead. But that’s design code, it’s much more important to not copy and paste business logic or data access layer code to different places.


What do you mean conclusion?
Haven’t you paid attention?
Stop copy and paste code! 🙂

Have fun!
kick it on

8 thoughts on “Copy and Paste programming – A bad habit

  1. I have done cut and pasting myself, but I general change it to suit my needs, in terms of functionality and also make sure it follows my coding standards.

    Although sometimes when you are in a rush you do tend to get lazy, and say you will come back to it, but you never do because other deadlines come into play or someone else cut/paste it and you don’t have time to bring it up to specs.

  2. I like to point out that I don’t think I’ve done any kind of underestimation when I call copy and paste programming “a bad habit”. There are many things I would call a bad habit:

    * Taking LSD and drink two bottles of Scotch and then take your SUV out for a spin at the playground of the local kinder garten, would be a bad habit.

    * If you, just for laughs, are in the habit of putting people that are claustrophobic in an atomic submarine and change the sign over the button that will launch all missiles at the same time to a new sign reading “Press here to exit”. I would say that that is a very bad habit.


  3. Cut/paste programming is one of my pet peeves. It just grinds my gears that in this day and age of agile/OOP/[insert favorite buzzword here] where everybody gives lip service to these methodologies, we are *ALL* too lazy to not resort to this very bad habit.

    Yes, I have done it myself. When I find myself doing it I try to go back, re-factor, and put the common logic into a generalized reusable component/method/whatever – so I don’t feel so bad about admitting it. But it does grind my gears when I see it done by other devs that should (and do) know better. It is just pure laziness and sloppiness. Sloppy, sloppy, sloppy.

    If there is any doubt everybody does it – take a look at


    in the JDK source code.

    Yes, the almost the same exact class, for the same purpose (read the JAF spec) in two different places in the JDK, with minor tweaks. And that is probably a fairly harmless example – the bad examples are where someone takes smaller chunks of code and sprinkles them throughout various modules/classes, making it harder to find, debug and test. At least with two classes that are almost the same (including the name) I would know where to find the code – much harder otherwise.

    Good luck getting people to stop doing this. They would rather rant their agile axioms (some of which I think actually contribute to this bad habit) than write plain vanilla modular DRY code. And no, I don’t hate ‘agile’, I try to practice what I think is the best of agile – but I do think some people have gone off the deep end and forgotten simple core first principles of software development that were around decades before ‘agile’ – like DRY.

Leave a Reply

Your email address will not be published. Required fields are marked *