Copy and Pasting Code Should Be Illegal

I don't know how many times in my 3 decades of coding that I have been bitten by someone's copying and pasting of code. Sometimes it's even been me.

Copy and paste is such a wonderful idea, you take a little something and duplicate it elsewhere. Massive productivity gains, like getting something for nothing, sprinkling little nuggets throughout your work.

That is, until it blows up in your face. Those nuggets always wind up dark and smelly. If it doesn't explode immediately it will get you or the next programmer after you are gone. This easy practice has to die.

Let's say you have some working section of code and discover to your joy that you need something identical or similar in another place. What could be easier than to just slide in another copy or several elsewhere, maybe with just a few tweaks? It works, and everyone says that code reuse is a good thing.

Do not fall for this, that's the code devil speaking. When you replicate code with copy and paste you are spreading potential evil into your nice carefully designed codebase. It likely will not be immediately apparent that something is wrong; the shit will hit the fan at some future point. But, you say, what is the real harm, since the original code worked fine?

The problem is that even if the code was perfect and remained identical throughout all the copies it won't remain that way. Someday someone will need to make changes and likely will not realize the code is replicated and only change one copy. Later changes might affect a different copy. Over time the identical code becomes unique and perhaps adapted to its new home.

Now I come along and have to make some more significant change and code hell begins. I have multiple copies of similar code which is different and instead of making a single change I have to make numerous different changes, somehow interpreting the altered but similar copies in my new design and keeping everything working. Or I can bite the bullet and do a major refactoring job and somehow not cause even more havoc by simultaneously refactoring and rewriting code to a new spec.

In short I will curse your original existence and wish for a time machine to come back and let you know what I think of your shortcut.

An even worse scenario (which I have witnessed a lot) is when the original code that was replicated was buggy. Now you have pushed the bug all around the code. So later on you or your angry descendent will be stuck refactoring, debugging and redesigning at the same time. Your quick satisfying productivity enhancement has lead to a trail of tears.

Copying and pasting code is dumb for many reasons, including the difficulty of making changes later, of duplicating code into new environments where it doesn't exactly fit, of inserting bugs where they should not be, and of course ignoring the basics of good design or even common sense.

Many years ago I came to the conclusion that I should never copy and paste anything. Ideally if I find an urge to do it, I paste it into a separate location, and then type it in manually. This usually gives me time to ponder if this makes any sense. Often it doesn't and I look to refactor into some kind of common method or class or even a different design. Even if I decide that the duplicated code still makes sense to keep, typing it in manually lets me make it fit where it lives instead of pasting it in and then chopping it up.

After all we write code, we don't drag bits of code from here and there and string them together. People having been trying to create that since the day I started programming and it still doesn't work.

Copy and paste is a mindless way to write more code but that's the real problem, you don't use your mind when you do it. Looking at a codebase I need to work on and finding the brown nuggets inside is the worst kind of code smell. Finding a critical bug caused by this kind of simpleton coding can make you wish ill on people or at least become ill yourself.

The next time you feel an urge to copy and paste some code please recall the dark nuggets of doom metaphor. It might save some future programmers from giving up their craft and becoming something useless, like lawyers. It might save a future you from cursing the past you (which is never a good thing).

Copy and pasting code should be illegal. Oh crap.