One of the key hacks to efficiently learning how to code is to copy others’ code.
But this does not mean copying-and-pasting. It means typing each line, character, and indentation yourself. On the surface, this seems tedious. But if you don’t know how to program, you have to start somewhere. Even as competent programmer, I often start the day by doing verbatim copying of someone’s code. It’s a leisurely activity when the coffee hasn’t quite kicked in yet, and I almost always notice something about the code that I didn’t while just reading it.
However, I admit to having the bad habit of copying-and-pasting my own code, because I’ve told myself “Well, I’ve already written it once before…just because I can’t remember it off hand doesn’t mean I need to re-type it.” So I’m trying to get better at kicking that habit.
Here are a few ways to practice copying code while getting better at your operating system:
Open adjacent windows
This is pretty straightforward. Open two windows, code-to-copy on one side, new file on the other, and then type away. It may seem brainless but it is still better than copy-pasting.
TK: Post an animated GIF
One window at a time
I prefer opening the code-to-copy in one full window, and the new file in another window, and having the two windows lie on top of each other so that I can only see one at a time. In the new file, I type as much as I can remember. And when I need to, I hit Cmd + ~ to quickly flip to and glance at the code-to-copy, and the Cmd + ~ again to type what I’ve just seen. That extra step seems to make my brain work harder at focusing on the details.
TK: Post an animated GIF
via the Intro to Zed Shaw’s Learn Python the Hard Way (emphasis added):
The one skill that separates bad programmers from good programmers is attention to detail. In fact, it’s what separates the good from the bad in any profession. You must pay attention to the tiniest details of your work or you will miss important elements of what you create. In programming, this is how you end up with bugs and difficult-to-use systems
By going through this book, and copying each example exactly, you will be training your brain to focus on the details of what you are doing, as you are doing it…
You must type each of these exercises in, manually. If you copy and paste, you might as well not even do them. The point of these exercises is to train your hands, your brain, and your mind in how to read, write, and see code. If you copy-paste, you are cheating yourself out of the effectiveness of the lessons.