You'll be hard pressed to find a developer alive that hasn't copied and pasted code at some point. It might be code from an old project or a snippet from Stack Overflow. And you know what? There's nothing wrong with that.
Knowing how to copy and paste code can save you hours of time. Although there is a subtle technique to copying and pasting code and it catches new developers every time. You have to watch out for a few things before you decide to use a snippet from somewhere else.
You need to understand the code
Hear me out. Even if the code you used worked, you need to know why. Say you find a bug later on and the call stack leads you back to that chunk of code (which will likely happen). What do you do? Why is it not working?
That's why you need to understand exactly what that code is doing and why. Most snippets need a little editing or some understanding of how the project is set up. There's a lot of good code out there that will work for you, but you need to know how to integrate it with your native code.
Be on the look out for potential errors or incompatibilities
Make sure the code you copy and paste meets the requirements for your project. Check that it's the most recent version and that it works with your version of code. This is especially true in frameworks like React and Angular. Getting the wrong version of code can break an entire project if you're not careful.
Also look out for those weird errors that let you build and run the project anyways. They are going to cause some chaos somewhere unexpected. When those happen it takes a good amount of digging through documentation to fix. If you run into one of those issues, you might want to look at some other options before you dive too deep. Unless you have to use that piece of code.
Don't be afraid to use the code
Sometimes when you get in the zone and the code just flows out of your fingers, you forget to use the code that was supposed to make this easier! It's cool to write your own functionality. I think it helps stretch you as a developer. It's just when you have deadlines and clients to meet with you won't have as much time to dedicate to making your own functionality unless it's a super custom application.
Go ahead and take the shortcut. If you've already checked the two points above, then you won't have any problems with it. And if something better comes along don't hesitate to test it out. A different piece of code could help your application run better or faster. Part of learning how to code is knowing when and where to use it. Don't be afraid.
So keep doing the copy and paste thing. An added benefit is that when you copy and paste code you're learning how to read other people's code and how they write. That will help you grow as a developer. Usually if a code snippet has been used a lot it's a pretty solid snippet. That means a pretty solid developer wrote it. That's who you want to learn from.
How do you feel about using copied code? Some people hate when other developers just copy and paste code.
Hey! You should follow me on Twitter because reasons: https://twitter.com/FlippedCoding
Top comments (13)
😂 That's pure gold!
yes, exactly 😊
I just copy/pasted the image 😂
Good job man! 😊
I do that all the time! 😸 I create my websites with an emphasis on accessibility. They all have colored themes that can make the site easier to use for people who are colorblind. The themes are also reversible to accommodate people who need light-on-dark contrast instead of dark text on a light background. 😸
The worst projects I've worked on are the ones where I'm required to copy and paste someone else's code with trivial modifications. It's demoralizing and depressing. Once in an emergency I can handle. But as a practice, if someone wants code copied and pasted they should find someone who doesn't care about writing their own code.
I've seen in code comment with just a link to StackOverflow(SO hereafter). When asked why the code was used, the dev didn't know how it worked.
But I could not bring that in a meeting because I was guilty of doing the same, also causing a bug and hindering the bug fix (as I had to understand the code first).
So I "feel" that it's quite bad practice because one might need to change the copy-pasted code not because of a bug but also due to a business logic change. It'd slow down the person who used the code without knowing but also others who have to take it over.
Copy Pasta
... This is one new Pasta flavor and seems to be an interesting oneYes, frequently. I usually leave a comment with the URL and some context so I can refer back to it if needed. I very rarely copy without making edits, though — if I needed code that already suited my use case perfectly, I'd import a library instead.
I'm personally on board with copying and pasting code with the caveat that you understand it. You could even argue that it's not a huge jump from that to using an OSS dependency. I mean, in both cases you're using someone else's code to accomplish something in your project. In both cases, if you don't understand the code you're using, you're opening yourself up to issues like incompatibility or malicious behavior (like the recent event-stream fiasco).
Reading other people's code is a great way to learn. Including unvetted and not-understood code is asking for trouble, imo.
Hi!
I come from sys-admin and I'm starting to get more deeply involved in the developer world and I'm fascinated by how many people contribute their repositories or code examples to help others. I just think that copying and pasting code is very useful to understand how a language works and to gain fluency programming.
Other hand, reading a lot of documentation in English its help to me to improve my English hahahaha
Typing it out really does help. Sometimes that's how I figure out what the code is actually doing. But you're right. It's usually never a good idea to just copy pasta. 🙂