Brendan Enrick

Daily Software Development

Copy Paste Programming

One of the most well known anti-patterns is copy paste programming. I am sure that every programmer has done some copying and pasting of code. It’s easy to do and can make writing code a lot faster than just writing new lines for everything.

You might have done something very similar to what you need now. All you have to do is copy the previous code and update it to fit your current needs. What could possibly go wrong?

I don’t think I need to explain why this one is bad. I think it’s easiest to just say that more code means more maintenance. It makes things messier, harder to read, and harder to change.

The month of September 2012 is the Copy Paste Programming month in the NimblePros Software Craftsmanship calendar, so consider this your reminder to stop copying and pasting code. You should know better already.

CopyPasteProgramming

I loved how well this image turned out. I’m sorry though, it’s not OK to copy and paste your code. No, it doesn’t matter that you’re making slight changes each time you copy it. Just don’t.

Comments (7) -

  • James Curran

    9/27/2012 3:08:19 PM | Reply

    I remember once maintaining some application, where I found the exactly same block of code (with the exact same syntax error (*)) 25 times throughout the codebase.

    (*) The error was a function call with missing the parans, which in C is a legal expression, but the function is not actually called.

  • itoctopus blog

    9/29/2012 10:53:32 AM | Reply

    Hi Brendan,

    Copy and paste programming is one of the worst programming style ever - especially when it's about copying some code, pasting it somewhere, and then using find and replace on that code. 50% of the times this will not work as it should, and in 50% of these 50% it takes much more time to fix the problem in the code than to write the code from scratch.

    I am not against copying a line or two and then testing the immediately, but copying a whole logic from one place to another is asking for trouble and for project delays!

  • William Payne

    9/29/2012 11:32:11 AM | Reply

    I recently found a couple of functions in out code base that were duplicated exactly, but with a small number of very minor differences, mostly cosmetic. The duplicates had been around for a while, and had been maintained in parallel, so most of the differences between them were comments, spacing, order of statements and so on. There were a couple of differences, however, that were significant. But which copy was correct? What did the differences mean? Were they deliberate? The functions were supposed to do more or less the same thing, but there were differences when each was called ... the overwhelming feeling of panic and confusion started rising...

  • James Curran

    9/29/2012 12:13:35 PM | Reply

    William,

    Move them all into the same source file. Refactor out all insignificant differences.  Create one parameterized function which handles all variations.  Reduce the existing functions to just calls to the new one:

    Really Simple Example
    Old:
    funcA()
    {
         COnsole.WriteLine("Hello World");
    }

    funcB()
    {
         Console.WriteLine("Greeting Earthings");
    }

    New:
    Output(string message)
    {
         Console.WriteLine(message);
    }
    funcA()
    {
         Output("Hello World");
    }

    funcB()
    {
         Output("Greeting Earthings");
    }

    With way, when someone goes to maintain funcA(), they can see all the option already available, so instead of creating yet another one, they can consolidate one one or two.

  • William Payne

    9/29/2012 12:51:33 PM | Reply

    We did. I was just surprised to see that the duplication had come about in the first place.

  • Hui Zhou

    9/30/2012 11:50:37 PM | Reply

    Often the situation goes like this: the two places uses very similar logic and much of the identical code. However, one place need do some extra initialization and some extra cleanup which refers to some extra parameters. So to factor them into a function or class, we often faced with the dilemma of creating and maintaining extra interfaces of either the extra callbacks or extra parameters, which have no use and puzzling by studying in one applications. And study the function and class is extra difficult as both applications need be considered. And as features creeps in, we'll quickly find not two, but three and more such situations. As we often end up in delivery critical situation, every time concerned with update and debug an interfaces that serves many faces are not quite fun. Copy and pasting is bad, bloated interfaces are not good either. We need better programming facility than functions and classes.  

Loading