There is much conflicting advice out there when you start with coding. Should I copy code I find on the internet? How do I learn when I copy code constantly? Especially how do I get independent from the internet when I constantly copy my code? I don’t want to be a copy/paste coder my whole career!

On the other side, everybody tells you not to reinvent the wheel and waste time writing solutions to problems already solved.

The solution is highly dependent on the situation, in which you are writing your code. Are you learning for an assignment? If yes, I would not copy the code. Are you working on a project and searching for a solution? Excellent take over and adjust it to fit your project.

But there are further details to look out for. Let’s look at these two scenarios in-depth now.

When learning to code

I would not copy code when you need to learn something new. You have not done your job after you’ve pasted a solution from the internet. As you still need to understand the code. And like with everything in life, the more you do it, the better you will get at it.

I mean, when you are fully stuck on an assignment, and you find an example on the internet fine. But take a piece of paper, start dissecting it, and try to understand how the computer is executing the algorithm until you know what it does. And then try to write the code again by yourself. You know you have fully understood the problem when you can do this.

There is an excellent trap with understanding foreign code. You look at it and think you have understood what it does. But until you have solved it yourself, you never can be 100% sure that you’ve seen all there is to see. And you create a false sense of understanding. Lying to yourself about how much you have understood. Solving the problems yourself, again and again, is the path to getting better and being an independent developer. There are no shortcuts to this process.

Working on a real-life project

The best solution to include external code in an actual project is to use a library. Ideally, you find a library directly solving your problem, with a license allowing you to integrate it. But you do not always want to add a separate dependency to your project, especially when it is just a single line or a short method.

When I find code to copy, I usually check if there is any mention of the license for the code. Some forums have general rules allowing you to use the code in your projects. But it is best to check. I always link back to the source in my code. I do this for two reasons:

  1. Make it clear that the code is from somebody else and not mine
  2. Helping me understand why the code is there and where I could find additional information when it does not work.

But as with the learning case, I also analyze the code and think it through. While I do this, I also adjust the style of the code to match the rest of my project. In most cases, it happens automatically as I seldom find a solution that directly solves all my requirements, so I need to change the copied code anyway.

Conclusion

Used sparingly copy and paste can help you. But you still need to invest some time in understanding the code. When did you last copy source code? Did you understand the code you added to your project?

I hope this post provided you with value and you liked reading it. I would appreciate it if you signed up for my email list never to miss another post.