Why is "copy and paste" of code dangerous?

18,590

Solution 1

If you find a bug in your copy-paste code, you will need to fix it every place you did and hope you can remember them all (this also holds for changed requirements).

If you keep logic in one place, it is easier to change when needed (so if you decide that the application needs updating, you only do it in one place).

Have your boss read about the DRY principle (Don't Repeat Yourself).

What you are describing sounds like the perfect use for libraries, where you share code and only keep it in one place.

I would only ever copy-paste code if I intended to refactor it soon after - making sure I later on extracted common code so I could reuse as much logic as possible. And by soon after, I mean minutes and hours later, not days and weeks.

Solution 2

You would be far better off sharing the code by building a library rather than copying the code using copy and paste.

You'll still gain a speed advantage over re-writing (look up DRY) but will only have one place to maintain the code.

Solution 3

The obvious reason is that you take on a 'debt' for the future: any change you ever need to make in the code (not just bugfixes, any change) will now be twice as expensive to do because you have to update two places - and more risky because you WILL forget one of them eventually. In other words, making it work faster now will make your work even slower in the future, which can be good business sense but usually isn't.

But the more important reason is that the assumption "this is the same as that" is more often than not subtly wrong. Whenever your code depends on unspoken assumptions to be correct, copying it into another place results in errors unless these assumptions also hold in the new place. Therefore, the pasted code is often wrong from the start and not just after the next change.

Solution 4

Design-wise, copy-pasted code is certainly a disaster, with the potential to cause lots of problems in the future. But you're asking why it takes you a lot of work right now, the answer is: because it's never just copying and pasting.

If the original code was written in order to be reused, as a fairly independent library, with flexibility and client use in mind - then great, but that's not copy-pasting, that's using a code library. Real code copy-pasting usually goes more like this:

  • "Sure, I've already got code that does exactly that!"
  • "Wait, which of these five versions of code is the one I want to use as my source?"
  • "Hmmm, what do all these 'util_func_023' functions do? Didn't I document them? Which of them do I need now?"
  • "Oh, yeah, this code uses Code Base Y. Guess I need to [choose one: copy all of Code Base Y into my new project / spend a day extricating the one function I want from Code Base Y / spend a week extricating the one function I want from Code Base Y]."
  • "I copied everything, yay!"
  • "Why isn't this working?"
  • This is the point where you spend hours/days/weeks debugging existing code that is similar to what you want, instead of writing the code you actually want to begin with.

In summary, existing code which can't be used directly can, at best, serve as a good reference for writing similar code. It certainly can't be lifted whole and expected to work in a completely different system. In general, it's a safe assumption that any code which has been written and completed, should be messed with as little as possible - even when it's a copy and not the original itself.

If you want to base your project on copy-pasting, you've got to code to begin with in a manner that will enable easy reuse, without copying that original code and messing around with it. That's worth doing, and if that's what your boss is expecting, then you both need to make sure that that's how you design and work in the first place.

Solution 5

If you have already implemented the features and you need to copy and paste to reuse them, it sounds like you have done something wrong. Can't you put these features in a library so you can reuse them without copy/paste?

Share:
18,590
Yigang Wu
Author by

Yigang Wu

Updated on June 29, 2022

Comments

  • Yigang Wu
    Yigang Wu almost 2 years

    Sometimes, my boss will complain to us:

    Why do we need such a long time to implement a feature?

    Actually, the feature has been implemented in another application before, you just need to copy and paste codes from there. The cost should be low.

    It's really a hard question, because copy and paste codes is not such a simple thing in my point of view.

    Do you have any good reasons to explain this to your non-technical boss?

  • Paolo
    Paolo about 14 years
    +1. The point is that copy and paste is cheap for solving the immediate problem. The real issue is that in the medium/long term the cost of maintaining duplicated code is far higher than well-factored code
  • David Thornley
    David Thornley about 14 years
    It isn't just a bug issue; program requirements can change. I've changed four out of five places where something needed to be changed before.
  • Ira Baxter
    Ira Baxter about 14 years
    If you can copy-n-paste on demand, and track where the duplicates are easily to either later abstract them or update them, then copy and paste isn't a bad thing to do. See discussion on clone detection at www.semanticdesigns.com/Products/Clone for further details and for tools than can do this.
  • Oded
    Oded about 14 years
    Lots of ifs there, and most tooling doesn't support clone detection at this point.
  • Ira Baxter
    Ira Baxter over 13 years
    @Oded: There are lots of clone detection tools: stackoverflow.com/questions/546487/…
  • Brad
    Brad over 13 years
    @Paolo, but isn't the here-and-now all that "business-types" are concerned with? They've learned to bank on the fact that they'll be moved on to some other position or department or job before "it" hits the fan.
  • SamB
    SamB about 12 years
    While I can see that there is little point in releasing an update to the other application just to use the library, it would probably be a good idea to at least make a stab at the necessary changes in a feature branch. This would give you some confidence that the library's interface was general enough for at least the two applications in question, and allow you to merge the change at an appropriate point in the release cycle.
  • Oded
    Oded over 11 years
    @Downvoter - care to comment?
  • Mike
    Mike over 11 years
    Just tell your boss that you're the software professional and as someone who wishes to upholds standards to make the codebase maintainable in the long-run, you're not prepared to cut corners unless there's a very good reason to do so. If you have to cut corners, make sure you add unit tests and then you simply must go back and refactor afterwards when the heat is off.
  • Hauleth
    Hauleth about 11 years
    Once upon a time there was a programmer who work for ESA. He was working on software for Ariane-5 rocket and he used copy-paste method. Then s... happens
  • kazinix
    kazinix almost 10 years
    I'm just curious, why would you "cut" the code if all you need to do is duplicate it?
  • CResults
    CResults almost 10 years
    Good point dpp. Edited!
  • mapto
    mapto about 5 years
    I find this article exceptionally badly written. The narrative stays abstract thus fails to shed light on the case beyond the fact that Fred is working iteratively. One obvious problem Fred has is that he has no good idea of the overall architecture. Unfortunately, this is very often the case when we work with undocumented legacy code where the know-how is lost. References to Design by Contract and Assertive Programming are quite good, but unfortunately even after them the examples/exercises are left unexplained.
  • MicroservicesOnDDD
    MicroservicesOnDDD over 4 years
    This is like saying, "never stick a knife in a man's throat", which sounds like a very good rule. Until you realize that the doctor MUST break this rule to perform the trachiotomy to save a man's life (if he can't breathe, perhaps due to anaphylaxis, extreme allergic reaction). Every rule has an exception (except, perhaps, for this one -- that every rule has an exception). Therefore, every rule must have a why and a when and an exceptions list, all attached, for the true "engineering" reality that the true answer is, it depends...
  • MicroservicesOnDDD
    MicroservicesOnDDD over 4 years
    So... when do you NOT follow DRY? I constantly wrestle with this at my current job, which has to do with firmware, and the answer is that we "unroll loops" and do other things because that "improves performance." We have a very shallow inheritance hierarchy, and we use most classes directly instead of subclassing them, and... ...we use copy-and-paste a lot. And I hate it, because it makes our code base harder to understand and harder to maintain. But we have our reasons, and they are acceptable reasons. We are not the only stakeholders. And finding the right balance is more of an art.