DEV Community

Cover image for Embrace the Copy/Paste
Molly Struve (she/her)
Molly Struve (she/her)

Posted on

Embrace the Copy/Paste

I have been asked more than a few times if it is OK to copy and paste code from someone else. My response is usually, "Of course!" Especially when you are starting out, finding code snippets to help get your brain going and get you familiar with a language is a great way to learn. Even if the code is not doing exactly what you want, sometimes it can help point you in the right direction.

However, when you find a code snippet, whether it be from stackoverflow or your own company's code base, before you use it, make sure to understand what it is doing!

Blindly copying and pasting code is not really going to help you learn. You need to be reading the code and figuring out what it is doing. When you are starting out, reading other's code can help you a lot with learning syntax. I still learn new Ruby tricks from reading other's code.

Not all code is created equal

It is also important to keep in mind that not all code out on the internet is "good code."

Shocking, I know! But seriously, when I say "good" I mean a range of different things. Code might be super cleanly written, but has horrible performance. Another piece of code might look like a hacked together mess, but damn does it work well. If you can understand how a piece of code works that will better help you determine if it is the right piece of code for you. If you are not sure, ask someone! Find a peer, or even better, a more senior dev than yourself, and have them look at the code snippet with you.

If you need to determine for yourself, whether a piece of code is one you want to use, here are some things to consider based on the source of the code snippet.

  • Stackoverflow:
    • How old is the question? How old is the answer?
    • How many up votes does the answer have?
    • Does the question or answer mention any specific software versions this code was used or written for?
  • Github Gist or Repo:
    • How many stars does the gist or repo have?
    • What was the original purpose of the piece of code?
    • Are there any comments on the gist or repo explaining its use?
    • What kind of experience does the maintainer/author have?
  • Blog Posts:
    • In what context was the code used? Does it match how you will use it?
    • When was the blog post written?
    • Who is the author of the blog?

As you can see, there is a bit of a trend when it comes to determining if a piece of code is right for you.

  1. How old is it?
  2. Does the original use align with what you want to do?
  3. What credentials does the author have?
  4. Are your software versions similar?

Keep these questions in mind when you are seeking out 3rd party code and you just might find some useful snippets out there 😃

Copy/Pasting is not just for beginners

I have been coding since 2012 and I still copy and paste code. I find one of the most useful times to copy and paste code is when you are working with a language or technology that you are less familiar with. I do this A LOT for front-end technologies like HTML, CSS, and Javascript. I don't work with them on a normal basis, so my syntax knowledge is weak. It is a lot easier for me to find a code snippet and tweak it, rather than write my own from scratch.

One of my most recent copy and paste successes was actually a bash script. I needed a bash script to reindex some Elasticsearch indexes. I could have cranked out an entire Ruby reindex script in my sleep, but I do not write a lot of bash. So I Googled, "Elasticsearch reindex bash script" and low and behold, the internet delivered! I found this handy reindex script which I was able to use to do the reindexing. I made a couple of changes to the script to fit my needs, but otherwise, basically used it as is. I fully understood what the script was doing, but if you had asked me to write that from scratch in bash it probably would have taken me a full day of fighting formatting errors to figure it out.

TL;DR

Don't ever be afraid to copy and paste! Embrace it as another tool you can use when you are writing code. Like any other tool, be smart about how you use it. When used correctly, it can teach you a lot and make you so much more productive!

Top comments (8)

Collapse
 
ld00d profile image
Brian Lampe

I would suggest typing it out in lieu of straight copy/paste. This exercises your muscle memory with the new stuff you're learning, and makes you pay attention to the details when it's not new stuff.

Collapse
 
cookavich profile image
Paul Cook

Just wanted to say this is good advice, and basically the approach of Zed Shaw in his book Learn Python the Hard Way.

If you want to take the muscle memory to the next level, delete the code after you've typed it out, and then try to write it out from memory. It's kind of an ad hoc test of yourself, and that is one of the absolute best ways to learn.

psychologytoday.com/us/blog/ulteri...

Collapse
 
qm3ster profile image
Mihail Malo

I find that "thoughtfully typing" it is the ideal solution.
In other words, using your own function/variable names, refactoring to use functional methods like .map instead of for loops, etc.
By the time you reach the end, you almost always understand how it works.

Collapse
 
ssmusoke profile image
Stephen Senkomago Musoke

And to make sure that others learn from you too - ensure that in your usage you add a link and attribution to the original source of the code

I now do this as a personal way of driving attribution, even within closed source usage

Collapse
 
qm3ster profile image
Mihail Malo

I find adding a comment like this is perfect so that I know where to go and "complain" if I find any edge cases, even after changing the code style/variable names to my convention or even refactoring altogether (which makes search useless).

Collapse
 
ben profile image
Ben Halpern

10000% agree.

This post from 2016 had a big affect on how I think about these things.

More supporting material loosely supportive of this concept:

Collapse
 
bedmison profile image
Bob Edmison

I can’t emphasize the notion of understanding what you are copying enough. A colleague’s research group just had a paper accepted for publication that shows that a significant percentage of non-trivial code samples on coding sites including StackOverflow have security issues. We all do it, but we just have to be aware of what we are copying.

Collapse
 
daveskull81 profile image
dAVE Inden

I think it is a great reminder for everyone to remember that even experienced developers do many of the things beginners do when writing code. This stuff is hard and that’s okay.