Duplication in your code. Also, duplication. And duplication.

» Posted by on Sep 24, 2009 in Blog, Coding, Technology thoughts | 0 comments

From the department of redundancy department, IT coder division:

EXTREME code duplication

Duplication is probably the single, most important thing to banish from your code. Duplication can take several forms:



This is the simplest and often the easiest to find. This is when you have sections of code that are identical or nearly so. This is often a result of copy & paste programming. Several techniques can be used to eliminate it, including: extracting methods and reusing them, making a method into a template method, pulling it up into a superclass, and providing the variations in subclasses, and other basic refactorings. See Fowler’s “Refactoring” for more detail.


This is a special case of textual duplication and is present when you have multiple functions/methods that serve the same purspose. This is often seen in projects that are split up between groups that don’t communicate enough. This could be a result of copy & paste, or it could be the result of ignorance of existing code or libraries. Practices and policies that encourage and promote knowledge of the entire codebase and all libraries and frameworks being used can help avoid functional duplication.


This is a bit odder, more obscure, and not as easy to find. You have temporal duplication when the same work is done repeatedly when it doesn’t have to be. This isn’t a problem in the same way as the previous two forms of duplication since it doesn’t involve having extra code laying about. It can become a problem as the system scales and the repeated work starts taking too much time. Various caching techniques can be used to address this problem.

Of the three types I’ve identified above, textual duplication is the worst. It is the quickest to bite you as the system grows. It has a nasty habit of being viral: when you see code duplicated in the system, it can be tempted to think “Oh, well then, it’s not a problem if I just nick this code and put a copy of it over here and fiddle it a bit. Hey, everyone else is doing it!” It’s a lot like the Broken Window that Dave Thomas & Andy Hunt talk about. If you don’t keep on top of the little things as they appear, the whole thing will soon go down the toilet.

Submit a Comment

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


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>