Improve Code Quality by Disabling Copy and Paste

Integrated Development Environments (IDEs) should ship with Copy and Paste disabled by default in an effort to improve code quality. I acknowledge this is an extreme position that will not be popular by the majority of those who happen to read it.

Reasons why Copy and Paste should be disabled by default.

  1. It promotes what I have to believe is the most frequent code smell; duplicated code.

    Recently I was upgrading some commercial libraries and it was found to cause problems in numerous areas of the application. This is par for the course whenever you wait two years to upgrade libraries. I was appalled to learn that nearly character for character code had been repeated no less than 5 times within a single file. To better put this into perspective, the source file was 1800 physical lines when I checked it out and 1100 lines when I checked it back in; nearly a 40% reduction.

    I imagine the development went something like this. The programmer started by making the initial segment of code and got that working to their satisfaction. Once it was finished they went down to the next set of requirements and found that it was nearly identical, but with a few variations to the code they had just written. They took the code from before, duplicated it, and then modified it slightly to fulfill the second requirement. This repeated 5 or more times and allowed the author to finish their code quickly, which naturally makes most programmer’s bosses happy.

    I believe it’s natural for all people to want to do the least amount of work possible to accomplish a given goal. I jokingly call this being lazy and genuinely believe it’s a positive thing for a wide variety of careers; software developers included.

    The problem here is that it is easier to copy, paste, and then alter the already working code than it is to break it out into reusable functions, classes or extension methods. Imagine for a second that Copy has been disabled.

    It’s clear the author already recognized that the problems were similar enough to use the previous code as a starting point. Since they would be unable to duplicate the code via copy and paste it would be advantageous to them, at that very moment, to move that code into a common area.

    This is the main reason why disabling copy and paste in IDEs would encourage better quality code. It eliminates the requirement to have foresight to the future maintainability of the code or a desire to produce high quality code and transitions it to being the easiest way to do it right now.

  2. It would promote writing reusable code from the very first time you start to program.

    I think anyone who has been programming for any length of time can remember a time where design decisions they made have come back to bite them hard. This is frequently how I learn new programming techniques. I run into a problem that’s going to be difficult to fix due to poor design and I find a new technique that makes it easier. Mistakes are essential to learning.

    With that in mind it may seem contradictory that I want to discourage beginners from making this very common mistake. The thing with code smells, in particular duplicated code, is that the code will work perfectly fine and it is only when it is revisited that the problem becomes apparent. For beginning developers this may never happen until long after the bad habit has been learned.

I’m not suggesting that all ability of pasting code should be removed, just the repeated pasting of code. Cut and paste should remain as it’s a convenient way to move code around. I would just like to see the paste buffer cleared after code is successfully inserted.

I realize that even with the measure described here it’s still going to be possible to duplicate code via a plain jane text editor that you use as a scratch pad or simply by typing the same thing over and over again. My suggestion is that by making it inconvenient it would encourage the better practice and improve code quality.

2 thoughts on “Improve Code Quality by Disabling Copy and Paste

  1. Josh

    I like the idea in principle, but you are right, its a bit too extreme. What would be perhaps just as useful, would be an IDE that identifies high levels of code similarity in functions or other scopes and then suggests breaking it out.

    A friend of mine from grad school has a list of other “programming sins” that matches up with this nicely and includes the horrors of copy and paste.

    1. Devin Post author

      It’s very convenient that you mention that. I was reading in MSDN a couple weeks ago that Visual Studio 2012 implemented a cloned code detector feature.

      Unfortunately I fear that Microsoft probably limits this feature to the highest versions of Visual Studio which many programmers simply don’t have access to. Just like their Code Analysis feature.

      Even with this feature however I fear that unless it runs automatically people either won’t know it’s there or won’t care. 🙁

      Thanks for the comment and the link, it was good reading.


Leave a Reply

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