Don't Copy-and-Paste Code (Unless You Understand It)

Written by Reinder de Vries on April 8 2020 in Dev Careers

Don't Copy-and-Paste Code (Unless You Understand It)

We all do it: copy-and-pasting code found on Stack Overflow or a website. Guilty as charged! And that’s OK – there’s nothing wrong with sharing code, and using it to your advantage. There’s one caveat though: you can’t learn from code you don’t understand.

The inspiration for this article came from The Story of The Fraudulent Coder from 2017. An aspiring developer faked their way into a developer job by copying complete projects from GitHub.

That’s a far cry from copying the odd utility function from Stack Overflow, but even that’s beyond the point. Imagine the learning opportunities, growth and experience this developer has missed out on. If only he had completed those projects by hand!

In this article we’ll explore the phenomenon of copy-and-pasting code, and how you can avoid it to become a more self-sufficient coder. Let’s dive in!

  1. How Copy-and-Pasting Just Happens
  2. Don’t Miss Out On Experience & Growth
  3. How To Get Better At Coding
  4. Conclusion

How Copy-and-Pasting Just Happens

Let’s look at a hypothetical scenario. You’re learning how to code and you’re following along with a tutorial to build a game.

The tutorial explains you what you need to do to build a game app. You’re instructed to add a new function to your project, like this one:

extension String
{
    static func randomNumber(length: Int) -> String
    {
        return Array(0..<length).map { _ in String(Int.random(in: 0...9)) }.reduce("", +)
    }
}

And KERPOW! — fast as the wind — here’s what you do:

  1. You select all that code
  2. You hit Command + C, and switch to Xcode
  3. You find the right place in your project and press Command + V
  4. DONE! BOOM! PROFIT!

Congratulations, you’ve just learned to copy and paste code. You missed out on learning a whole lot of juicy Swift syntax. And you’re learning at a disadvantage now, because next time… you’ll hit that Command + C and Command + V again.

Don’t Miss Out On Experience & Growth

Why is it “wrong” to copy-and-paste code? It’s not necessarily wrong from a right-or-wrong standpoint, but rather, one approach to learning code is more effective than the other. You want to use an effective approach, if you want to learn how to code.

Copy-and-pasting is a suboptimal way to learn coding. It’s like learning how to become a carpenter by only putting together IKEA furniture. Would you call yourself a chef if all you’ve ever done is put TV dinner in the microwave? You’re not learning anything like that!

Let’s take a look at that Swift code sample again:

extension String
{
    static func randomNumber(length: Int) -> String
    {
        return Array(0..<length).map { _ in String(Int.random(in: 0...9)) }.reduce("", +)
    }
}

You’ve got a ton of opportunities to learn more about Swift here. A quick pick:

  • That extension String part is an extension, a way to add functions to existing types. Which makes you wonder: “What are functions and types?”
  • A static function is a function that’s available on a class, as opposed to a class instance. We can call the function with String.randomNumber(···) but not with "a string".randomNumber(···).
  • The Array(0..<length) constructs an array from a range, so now we’ve got an array with the numbers 0 to length. (The array of numbers is also unused. But why?)
  • The map(_:) and reduce(_:) functions are called higher-order functions, because they take a function as parameters and return a transformed collection. They’re super powerful, and concise, which makes them all the more exciting!

Should you dive deep into each of these topics? You could, of course, but doing so 100% of the time is unreasonable. Your learning progress would grind to a halt, because every bit of code would lead you into an infinite rabbit hole of more new topics.

A simpler approach you can take is:

  1. Make sure you understand what the code does. Learn to read the code, and grasp how every part of it functions. You can step through every line or statement, so you get an understanding of how it works globally.
  2. Pick one or two bits of syntax and learn more about them. That way you’ll gradually introduce yourself to new concepts, syntax and techniques. In the above code, extensions or map(_:) are good examples.
  3. Adjust the code so it works better with your codebase. You’ll need to understand (parts of) the code before you can change it. Don’t just jury rig it, but seek to create a deeper integration.
  4. Rewrite parts of the code in your own “words”. Change the approach or technique, or even just variable names. You can rewrite the above code to use a for loop instead of map(_:), for example. This is a double-edged sword, because you learn about the code snippet and about the new code you’re writing yourself.

You’ve got a shared responsibility with the teacher you’re learning from, that much is true. A tutorial, course or book should explain how the code works, otherwise you might as well copy the entire project as a whole. It’s smart to seek out learning resources and teachers that show you how the code works, and why it’s coded like that.

A good learning resource will help you make the code your own. That means you don’t only understand it, but you can also take it apart and assemble it again in a different, functioning way. Differently said, you need to practice and play with the code you’re learning!

Learn how to build iOS apps

Get started with iOS 13 and Swift 5

Sign up for my iOS development course, and learn how to build great iOS 13 apps with Swift 5 and Xcode 11.

How To Get Better At Coding

So far, we’ve discussed how copy-and-pasting code just happens. We’ve also discussed a few alternative approaches you can take. You learn more that way. Can we also define a step-by-step approach that helps you “graduate” from copy-and-pasting code?

Let’s see…

  1. Level 1: You copy-and-paste complete blocks of code you found on Stack Overflow or blogs straight into your app
  2. Level 2: You copy-and-paste, but you improve parts of the code in small ways, and you have a greater understanding about the code
  3. Level 3: You copy-and-paste, but less often, and you improve bigger parts of code to fit your own needs, and leave out parts altogether
  4. Level 4: You don’t copy-and-paste much, but you can steal the code you find online and replicate the same results in your own code
  5. Level 5: You rely very little on code samples, and instead you can figure out what you need from documentation, how-to’s, project samples, your own past code, etcetera

You can get to work with the above list. Figure out where you are, and try to move up to the next level. For example, if you’re copying large parts of code, see if you can learn to adjust the code to fit your own needs.

This list isn’t intended to categorize developers based on their skills. It’s more of a guideline you can use to get better. It’s up to you whether you use it or not, and how.

In these levels of copying code you can see two recurring themes: the amount of code you figure out on your own, and the amount of code you copy-and-paste. You’re a more productive developer if you write code yourself, as opposed to copy-and-pasting the code.

You also build your ability to read and interpret code, which is a valuable skill. You don’t need a brain that can “compile” code, but you do need to be able to understand the mechanics and key points of a code sample.

†: “Stealing” here comes from Pablo Picasso’s quote “Good artists copy, great artists steal.” You can explain that quote in different ways. Focus on the quote’s stealing; if you steal something, you make it your own. It’s not someone else’s code anymore if you’ve rewritten it, made innovations, and made it entirely your own.

Conclusion

Copy-and-pasting code you found on the internet into your own app is inevitable if you’re starting out as a beginner software developer. You don’t, however, have to keep doing it just because you once started with it. It’s a suboptimal way to learn more about programming! You miss out on so much experience, growth, and excitement.

Happy coding!

Want to learn more? Check out these resources:

Learn how to build iOS apps

Get started with iOS 13 and Swift 5

Sign up for my iOS development course, and learn how to build great iOS 13 apps with Swift 5 and Xcode 11.

Reinder de Vries

Hi, I'm Reinder.
I help developers play with code.

Get the Weekly

Get iOS/Swift tutorials and insights in your inbox, every Monday.
  • This field is for validation purposes and should be left unchanged.

Most Popular

Browse Topics

Swift Sandbox

Code Swift right in your browser!
Go to the Swift Sandbox

Reinder de Vries

Reinder de Vries

Reinder de Vries is a professional iOS developer. He teaches app developers how to build their own apps at LearnAppMaking.com. Since 2009 he has developed a few dozen apps for iOS, worked for global brands and lead development at several startups. When he’s not coding, he enjoys strong espresso and traveling.

×

Build great iOS apps
Learn how in my free 7-day course

  • This field is for validation purposes and should be left unchanged.

No spam, ever. Unsubscribe anytime. Privacy Policy