We all do it.  There is a chunk of code that almost does what we need.  So, we copy and paste it where we want it then make some modifications.  When we do this in our systems instead of proper code re-use than we are implementing the Copy Paste AntiPattern.  Now let’s look closer at why we should avoid this.

Defining the Copy Paste AntiPattern

I want to use the Wikipedia definition for this anti-pattern to help us get a better feel for when and how it may occur. [Click Here to See The Page]

Copy-and-paste programming, sometimes referred to as just pasting, is the production of highly repetitive computer programming code, as produced by copy and paste operations. It is primarily a pejorative term; those who use the term are often implying a lack of programming competence. It may also be the result of technology limitations (e.g., an insufficiently expressive development environment) as subroutines or libraries would normally be used instead. However, there are occasions when copy and paste programming is considered acceptable or necessary, such as for boilerplateloop unrolling (when not supported automatically by the compiler), or certain programming idioms, and it is supported by some source code editors in the form of snippets.

Note that there are some situations where it makes sense to use this anti-pattern.  However, those are due to limitations and not ideal.  That makes this an anti-pattern and sometimes a workaround but never the best solution.  Think of it as an incremental step towards a solution.  Therefore, while it may work, for now, we want to abstract or automate it in the future to provide the best approach.  Even templates follow this recommendation.  They are useful but are best when we automate the creation of that code instead of copying and pasting templates.

Epidemic Bugs

The biggest issue this anti-pattern can create is an infection of your source code with bugs from the source material.  If you catch the bug early then you need to change the code everywhere it was copied.  If you are not that lucky, then you may find yourself facing the same bug over and over as it appears in the various copies.  The result comes from two problems in the copy paste antipattern.  We are not sure the source s perfect code, and we do not have a way to track all of the copies of the source quickly.

For example, we can copy A to B and C, then copy B to D and E, C to G, etc.  We have copies of copies and quickly have no idea have pervasive that bug is.  Even worse, we may fix the bug several times only to have QA point it out to us again not realizing they found a new bug.

Fixing The Problem

The fix for the copy paste antipattern is not a one and done solution.  Once you see a code block appearing multiple times, you should abstract each appearance into a single function or method.  Then you will be able to refer to that new function as you find other instances of the code block.  That means you may have this anti-pattern in your source for a while.  However, eventually, you will roll all those pasted instances into a properly designed and tested function or method.  You also gain the benefit of testing that section of code over and over as you roll in code from across the expanse of your application.

Rob Broadhead

Rob is a founder of, and frequent contributor to, Develpreneur. This includes the Building Better Developers podcast. He is also a longtime student of technology as a developer, designer, and manager of software solutions. Rob is a founder and principle of RB Consulting and has managed to author a book about his family experiences. In his free time, he stays busy raising five children (although a few have grown into adults). When he has a chance to breathe, he is on the ice playing hockey to relax.

Leave a Reply