Don't Copy-and-Paste Your Code

Written by: Reinder de Vries, February 5 2017, in App Development

Don't Copy-and-Paste Your Code

I just realized a trend exists among beginner app developers that’s growing at an alarming rate. In a landmark story about a Fraudulent Coder, author shakycode explains how he got tricked by an “aspiring” coder who literally copied-and-pasted his way into a job as a programmer.

Although I have yet to encounter a real fraudulent coder, I’ve seen plenty of aspiring coders copy-and-paste code here on the LearnAppMaking blog, on Quora, on StackOverflow, and in my own courses.

In this article we’ll explore the phenomenon of copying-and-pasting code and how you can use it and avoid it to become a more self-sufficient coder.

Let’s dive in!

The Copy-Paste Scenario…

Let’s look at a scenario. Say, you’re learning how to code and you’re following along with a code guide or tutorial.

The tutorial explains you what you need to do, and hopefully why as well. You’re asked to add a new function to a particular class, like this one:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])  
{
    if let newLocation = locations.last  
    {
        print(newLocation)  
    }
}

And snap!, this is what you do:

  1. You select all the code in the guide
  2. You hit Cmd + C and switch over to Xcode
  3. You find the right spot in your code and hit Cmd + V
  4. PROFIT!

You’ve just copied-and-pasted code.

You’re learning at a disadvantage now….

Get 5 of my best practices

Get 5 of my best practices

Learn how to build better iOS apps

I’ll show you exactly how I built a dozen professional iOS apps,
write extensible Swift code, and turn coffee into code.
Wait, what? Yup – into Swift code.

The Code You’ve Missed Out On

Why is it wrong?

Well, let’s look at what you’re trying to learn: coding. If you keep copying-and-pasting your code, you’re not going to learn anything. It’s like learning how to become a furniture maker by only putting together IKEA furniture. You’re not learning anything like that!

Take the example above, for instance. I could point out a few dozen learning opportunities from that one snippet above, such as:

  • The method locationManager(_:didUpdateLocations:) is a delegate method from the protocol CLLocationManagerDelegate. You could have learned about protocols and delegation.
  • The last property on locations is part of working with arrays, and it automatically gives you the last element of the array. You could have learned about arrays, right then and there.
  • The syntax if let ... is part of working with optionals and optional binding. It’s an important part of Swift, but you missed it!

Not all of this is your responsibility to learn. In many cases, the coding guides and tutorials we write, read, and work with are at fault. Many of them don’t sufficiently explain programming concepts, syntax, why it’s done like that, and what alternatives are available.

Nevertheless, you shouldn’t copy your code.

How Do You Know How Good A Coder You Are?

Self-assessing your own progress when learning how to code is hard. How are you supposed to know what percentage of all programming topics you don’t know, if you don’t know what you don’t know? Is knowledge of programming topics even an indicator of skill?

Programming is one of those skills that are easy to pick up, but hard to master. You can pull the thread further and further on any topic, only increasing the amount topics you know nothing about. Learning is as much about knowing which threads to pull – what to learn – as it is about learning the stuff itself.

As a self-taught developer, I have always assessed my expertise in programming by truthfully looking about how many lines of code I can write before I check that what I wrote actually works.

Say you’re working on your app. You are building one particular feature, a view controller with a few buttons for instance. You want to respond to a button tap and your plan is roughly as follows:

  1. Create the button in Interface Builder
  2. Create an outlet and a weak property in the view controller Swift class
  3. Connect the outlet to the UI element in Interface Builder
  4. Call the method addTarget(_:action:for:) on the button instance, with the right selector (a function)
  5. Write that function somewhere else in the file, and print out something like print(#function) to check that the button tap works

The point is this: you can hit Cmd + R, to build your app, after any of these steps to assess if you made any mistakes. When you’ve slipped up, Xcode will return a warning or an error and you can investigate the issue.

When you’re learning how to code you want to save yourself from frustration. Making a lot of progress and then realizing you made a mistake half an hour ago is frustrating. You’ve made so much progress that you don’t know for sure which new part resulted in the error. So, you try to limit the work you do between checking for mistakes as much as possible.

Consequently, the amount of work you do without making mistakes and without checking your progress says something about your skill and level of expertise as a programmer.

If you build your app after step 1, in the example above, instead of step 5, I’d say you’re a better coder if you can get to step 5 without making a mistake and without checking for errors, than you are if you pause after step 1.

Build better iOS apps by mastering best practices and app architecture » Find out how

5 Levels Of Programmer Expertise

Let’s define a list of consequtive increasing levels of expertise for programming:

  1. You copy-and-paste large blocks of code you found on the internet straight into your app
  2. You copy-and-paste, but you can improve the code sample to fit your own needs and you know what code you can leave out
  3. You copy-and-paste smaller blocks of code, perhaps single lines, because you can assess from the found example which lines you need exactly, without needing to copy-and-paste the entire code block
  4. You don’t copy-and-paste anything, but you can assess the code you find online and then replicate the same results in your code on your own
  5. You don’t need code samples, but instead you can figure out what you need to know from official documentation, looking up specific classes and their relationships for instance

(Note how level 2 differs from level 3. With the former you copy-and-paste first, and eliminate second, and with the latter you eliminate first, and copy-and-paste second.)

Throughout these five levels of expertise you can see two recurring themes: the amount of abstractions you can figure out on your own, without actually coding, and the amount of code you copy-and-paste.

Logically, you’re a better coder if you can write code yourself as opposed to copying the code without writing. Also, a better coder is someone who understands what code does without running it in Xcode or a compiler. 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.

If you always copy-and-paste your code, you’ll never move up this list of levels of expertise. You’ll always keep making IKEA furniture, instead of crafting your own.

Conclusion

Copying-and-pasting code you found online, into your own app, is inevitable if you’re starting out as an aspiring coder. You don’t, however, have to continue doing it just because you once started with it.

I think it’s important to know a bit about the road ahead, when you’re learning something new. Simply knowing that proficient coders don’t typically copy-and-paste code is a realization that can put you on the track of unlearning to copy-and-paste.

The levels of expertise, as defined earlier, can help you with moving past the copy-and-paste phase. You realize that learning really means internalizing a skill, instead of relying on external factors to get to an end-result.

Happy coding!

Don’t copy-and-paste your code!Click To Tweet

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.

Comments & Questions

Got a comment or question? Let me know! I read and respond to every one. Thanks!

  • Good idea. I’ve emailed you!

  • Nur Hidayat

    Hi Reinder,

    Great article. I wonder if I could translate it to Indonesian and publish it on my blog since we have similar problems here in Indonesia.

    Cheers.