It’s so easy to use copy and paste these days. It’s in your IDE (integrated development environment). It’s in your browser. It’s everywhere. While it is a handy feature in many cases, it can be problematic for a software engineer. Software engineers should avoid using copy/paste. That’s a bold statement. So let me tell you why.
It Creates Bad Habits
Duplicated code within a single code base is probably one of the most basic anti-patterns in software engineering, and something you should avoid like the plague.
When you force yourself to type out everything, you will look for ways to avoid duplication. I mean, who really wants to write the same thing over and over? Generally, if you find yourself reaching for the Ctrl+C, that is an indicator that there’s a better way to do what you’re trying to accomplish.
By avoiding the urge, you will encourage code reuse in what you are writing; a much better pattern to follow. There are so many ways to reuse your code. Even with similar code that needs to alter a few things here and there, in almost every case, it only needs to be written once.
Additionally, when you need to fix a problem, if you are fixing something that was copied. You’ll not only have to fix it in multiple places, you’ll have to find all those places as well, if you can. But if you’ve written it only once. The bug only has to be fixed in one place.
The same goes for writing unit tests. Why write multiple unit tests that are testing for the same thing? Write it once, and be done with it.
It Prevents You From Learning
Copying code, whether it be from other places within the same code base, or from external sources, tends to stop critical thinking in its tracks.
When you copy code, you may have a rough idea of what it does, but you will likely have very little knowledge of how it does it. Most of the time you probably won’t even think to care about it. When you type out every line of code, you think step-by-step how a problem needs to be solved. This helps with your future knowledge.
Maybe you’ll have a similar problem or pattern to solve in the future. You’ll make those connections in your brain, so that you’ll have an easier time tackling the similar issue when it comes up again. Or perhaps, you’ll remember you solved an exact problem in the past, and avoid unknowingly duplicating a piece of code.
If you find yourself copying code because you don’t fully understand the language or framework you are using. There’s no better way to solidify it in your head, than to manually type everything out. Even if you need to use an external source as a reference, type it out. Repetition is key. You’ll have it in no time.
Finally, on the topic of learning, copying code may perhaps be an indicator that you need to step back from the code altogether. Maybe you have something complex that requires more design or discussion. There may be more things you need to discover about the code you are writing.
It Causes Bugs and Other Problems
You may think you are saving yourself time, but there are so many things that can go wrong which will set you back.
You may have similar code, but with subtle differences. What happens when those differences aren’t caught? You’ll have a bug on your hands. It may be caught almost immediately, or it could be years. Do yourself a favor and find a way to reuse the code.
If there are differences in how your code needs to work, especially when referencing things found using our good friend Google, such as Stack Overflow and the like, be sure to type it out.
Firstly, those sources may not be solving your exact problem, or it may be difficult to understand what they did. Secondly, they could be buggy and you may very well unknowingly introduce a bug or security flaw into your code base. Thirdly, and this goes back to a previous point, you should type it out to solidify it in your mind, and customize the code for the specific challenge in front of you.
A Brief Story
One more thing to consider… let me tell you a long story, but in a very short way, from a hard experience.
I once destroyed my development environment by copying a command into the command line after a quick search for a solution to an issue. It was a partial command, not fully completed yet. It would have been a fairly innocent command had I manually typed it, but due to a stray character at the end of what I had copied, it came with dire consequences that changed all the permissions recursively throughout the entire development box.
I learned my lesson. Please don’t be that person.
Stop using Ctrl+C before it’s too late!
Why don’t you give this all a try. Take the plunge. Treat it as a challenge. You’ve got nothing to lose, and so much to gain. If you’ve made a habit of copying and pasting code, make the decision to stop now. You will almost certainly grow a lot as a software engineer when you make the decision.
As always, leave me some comments. Let me know if you run into any challenges along the way. And overall, let me know how it goes for you.