How to convince a colleague that code duplication is bad?

vitaut picture vitaut · Nov 19, 2010 · Viewed 8.8k times · Source

A colleague of mine was implementing a new feature in a project we work on together and he did it by taking a file containing the implementation of a similar feature from the same project, creating a copy of it renaming all the global declarations and slightly modifying the implementation. So we ended up with two large files that are almost identical apart from renaming.

I tried to explain that it makes our project more difficult to maintain but he doesn't want to change anything saying that it is easier for him to program in such way and that there is no reason to fix the code if it "ain't broke".

How can I convince him that such code duplication is a bad thing?

It is related to this questions, but I am more interested in the answers targeted to a technical person (another programmer), for example a reference to an authoritative source like a book would be great. I have already tried simple arguments and haven't succeeded.

Answer

Oded picture Oded · Nov 19, 2010

Ask him what will he do when he finds a bug in his code. How many places will he now need to fix it in?

You can also show him the answers to this question (Why is “copy and paste” of code dangerous?).