Copy and Paste Is Evil

Okay, so there’s really not anything inherently wrong with copying-and-pasting code, but the problem is with how most Software Developers treat the code after it’s pasted.

On more than one occasion I’ve been working on a code review for a fellow Game Developer and I’ll see code that I don’t quite understand or I’ll have a suggestion for a better way to achieve the same result. Invariably, at some point that Developer will attempt to defend their code with a comment along the lines of

“I got this from MyClass::MyFunction so I’m just doing it the same way.”

There are several things wrong with that statement. Where to begin…

1. They’re Duplicating Code

This one is pretty obvious, but warrants mentioning. If the code is being duplicated, should it be turned into a function (perhaps inlined)? If not, at least a comment citing the other location of that code so that some future developer will know to check the other location if a change needs to be made. Oh, and don’t forget to put a comment at the other location to cite the location you just pasted…. Ya, or just make it a function.

2. They Didn’t Address My Concern

No seriously, I have gotten that exact quote from earlier (function name aside): “I got this from MyClass::MyFunction so I’m just doing it the same way.” I believe I had asked them what the code was supposed to do. Or maybe I was just making a suggestion. Either way, they didn’t address my concern. Instead, they gave me a new one…

3. They Don’t Understand

So now that I know they blindly pasted the code without understanding what it was doing, I don’t really have a lot of confidence in the rest of their changes and will likely spend some extra cycles double-checking the rest of their modifications. If this is how I feel as their manager or lead, then we might have a problem on our hands.

4. And Also, Did They Test This?

If they didn’t understand the code they were pasting, then how could they possibly know how to test it? On large-scale software projects in particular, it can be near impossible to understand all of the ramifications of any particular change, much less one being blindly pasted into the code.

5. I Really Wanted a Fifth Point

Alas, I only made it to my fourth point before getting too sad to continue on. I’m joking of course! I know for a fact that I have done this very thing myself while I was still growing as an engineer. While this topic isn’t one of the cardinal sins of the software world, it is still worth considering if you find yourself getting ready to press Ctrl-V. Just stop and think – are you ready to own that code?


Advertisements