- Scott H Young - https://www.scotthyoung.com/blog -

The Beginner’s Guide to Learning to Program

One of the most frequent questions I get asked in emails is what’s the best way to learn how to program. In the past I’ve shied away from this question, since the internet is populated by much better programmers than myself.

However, while there are many guides to learning a specific programming languages or tool, I’ve seen relatively few dealing with managing the learning process itself. After writing code for ten years and learning over a dozen of languages to various levels of sophistication, I’ve seen it’s this process that matters more than anything else.

Even if you’re not interested in programming, I think it forms an interesting case study for learning any technical skill, so much of what I’ll say will be applicable to non-programmers as well.

I’m also hoping there’s a chance I can convince a few people to give coding a try. It’s an incredibly fun hobby, and the fact that many people do it for a living shouldn’t detract from that.

Don’t Be Afraid of Syntax

A major reason many people are scared off from trying to learn to code is well… all that code. Like reading a paragraph in a foreign language, you can sometimes make out a few words, but the rest looks like gibberish. How can someone possibly read and understand it?

Luckily syntax is the easy part! Although each language has its own quirks, the basic concepts of computer programming don’t change much from language to language, so syntax is merely a shorthand for those basic ideas.

As an example, a common idea in programming is a loop. The idea is that you want to repeat some section of instructions until some stopping criteria is reached. Once you understand the basic idea of a loop, figuring out how to do it in your language of choice is as simple as googling, “How do I use a loop in…” and you’ll get thousands of examples.

Loops are a very simple idea, but this applies for less common and more difficult programming techniques as well. When I had to learn C++ to build a graphics ray-tracing [1] program, I wanted to create multiple shading routines separate from the rendering, so I quickly googled, “C++ function pointers” and got the exact syntax.

Syntax, just like mathematical symbols and formulas, are often the reason people without a self-professed love of nerdy things avoid learning the subject. But once you realize this is simply a short-hand for other concepts, it is easy to figure out what the syntax is if you know the concept.

Start with a Tutorial Book

The best place to start is always with a careful walkthrough. For programming, I like O’Reilly [2] as a guide whenever learning a new language. Generally the assumed programming skill prior to starting is low, so they are a great start.

Walking through examples avoids a lot of frustration because you can type the syntax out exactly and avoid causing unintentional bugs. Getting anything working in the beginning can give you the confidence to continue forward with projects of your own design.

If you’re learning your first language, you may want to take an intro class on programming which can teach the basic concepts. Semantics are more important than syntax, so understanding the basic ideas is essential, while memorizing the exact spelling of a command usually isn’t (if you’re wrong, you’ll get an error and a quick Google search will inform you of your mistake).

Here’s some free courses which cover the basics:

The goal in the initial phase should be to get something working. Once you get that started, it usually isn’t hard to modify existing programs to get something original before moving onto projects of your own.

Which Language Should You Start With?

There are a lot of answers to this, so I don’t presume to have a universally correct one. My feeling is that, if you aren’t already a computer wizard, getting your computer prepared to start programming can be a frustrating task if you’re not used to it, and can turn many people off before they write a single line of code.

If you’re using a Mac, many programming languages come preinstalled, so you need to do very little to get started. In that case, I’d suggest Python [6] as a starting language because it is easier to read and can start running immediately (just get a text-editor like TextMate [7]). Ruby [8] is also good, particularly if you eventually want to use Ruby on Rails [9].

If you’re using Windows, things can be a bit trickier, but you should still be able to download the compiler/interpreter for most languages and begin writing code. Google’s Code Academy tutorials [5] provide setup instructions for Python in various platforms.

For first languages, I’d generally avoid C++ and C, because they tend to be a little less forgiving than Python. I’d also avoid web languages like PHP because they can be tricky to set up and require you to learn more at once (HTML, SQL, XML). And only take on LISP if you’re a masochist or just really like prefix notation [10].

Of course, the first language you should use, should be the one you want to use. So overrule my suggestions if you want to do something specific with your programming knowledge (such as smart phone apps, video games or websites), just keep in mind there may be a bit more to learn in the beginning as you familiarize yourself.

Learning by Project

Although I’ve been following the classic classroom-style curriculum for my MIT Challenge, that doesn’t mean I feel it’s the best model for self-education. The best way to learn is often by making a project that interests you and trying to build it.

Learning projects are great for programming, but it’s easy to make mistakes when designing one. Here’s the criteria I follow when setting up a project designed to teach me a new skill:

  1. It should be short. Don’t try to make a new Facebook, since you probably won’t finish and most your time will be doing repeat tasks rather than learning.
  2. It should force you to learn the technology you want to know. If you’re a beginner that could simply mean basic programming. If you’re a novice, it could mean learning multithreading, databases, GUIs or graphics APIs.
  3. It should be fun. I’ve learned more from making fun programs and games than from trying to replicate dry textbook assignments. Fun means you’ll be more motivated to work on it.

I’ve used this process extensively. When learning 2D graphics programming, I made a Space Invaders clone. When trying to learn Python’s regular expressions, I made a program that could go through all my business receipts and figure out the sales taxes I owed.

Debugging Frustration

The key barrier in any learning effort is to surpass the frustration barrier. If you do any programming, a lot of time will be spent debugging. The wrong way to approach this is to get frustrated and quit. The right way is to use debugging as an essential part of the learning process.

I’ve found there are two main types of bugs for programs that aren’t of significant complexity. There’s simple errors, the kind where you misspell a variable name or put parameters in the wrong order. These also tend to be the easiest to fix.

The second type are caused because your model of how the computer works isn’t the same as reality. And while these can be the most frustrating, they are also the greatest opportunity to learn.

My biggest mistake I made in my early days of programming was to only try to get the program working. I would try different approaches and when one worked, I would stick with it, even if I didn’t understand why my previous approach had failed.

Understanding every error can be beyond the ability of a beginner programmer. However, if you endeavor to understand why certain expressions won’t work, or certain algorithms run afoul, you’ll learn much more about the language than the band-aid method I had used in my early days.

Doing this means when you get an error, search for it online and chances are you’ll find a forum posting where someone made the same mistake. Look for the explanations given as follow-up and you can usually see an explanation into the nature of the error.

If the explanation references computing concepts which are new to you, then you can begin trying to learn about those new concepts. This way you can slowly build up your vocabulary of programming problems and designs so that you can avoid such problems in the future.

Thoughts on Learning any Technical Skill

Much of this advice is programming-specific, but the basic ideas are true for learning any technical skill, be it calculus, statistics, mechanics, chemistry, drafting or design:

  1. Don’t sweat the syntax.
  2. Start with a tutorial book (or class).
  3. Learn with projects.
  4. Use errors as a chance to learn new concepts.

Programming has to be one of the best skills I’ve learned, not only because it’s fun and rewarding, but because it teaches a lot of life lessons as well. It taught me how to build things, organize projects and set goals for myself. And, as computers become more integrated into our lives, it’s nice to know a little of the language they speak.