Embrace the Copy/Paste

Molly Struve (she/her) - Jan 23 '19 - - Dev Community

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!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player