Why is "copy and paste" of code dangerous?

Yigang Wu picture Yigang Wu · Mar 22, 2010 · Viewed 17.7k times · Source

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?

Answer

Oded picture Oded · Mar 22, 2010

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.