Syntax Error! How to avoid the most common trap for new programmers.

The most common and frustrating types of error students make when learning to code are syntax (and related) errors. In fact, they account for a vast majority of the incorrect code students run as they attempt to solve simple questions.

Syntax (and related) errors often trip up new programmers.

So what are syntax errors?

To understand syntax errors in programming, it helps to think about syntax errors in a natural (human) language like English. Syntax is the part of grammar that deals with how how the words in a language are arranged to create sentences.

Say a student writes one of the following sentences as part of a story about their weekend:

Examples of spelling and grammar mistakes in a simple English sentence.

Each of these is incorrect in a small way, but to a reader, the meaning of all of the sentences is clear. We humans have an amazing capacity to correct spelling or grammar mistakes based on the context of the sentence or paragraph. In verbal communication we are even more flexible, which is why transcripts of casual conversations are riddled with grammatical errors that you only notice when they are written down.

Of course it is possible to create sentences that have so many grammatical and spelling mistakes that the meaning is no longer clear, for example:

Examples of English sentences that have so many errors they no longer make sense.

but in general we humans are pretty robust. In contrast, computers are completely literal. To understand why we need to know a bit about how programming languages are different to human languages.

How do programming languages work?

Programming languages such as Python or Java are very simple compared to natural languages such as English, Spanish or Japanese. However, they do have a lot of things in common:

  • it matters what order the words are in;
  • they both have syntax (that is, a grammar) that define how the words can be combined together;
  • you have to spell words correctly;
  • you can translate between one programming language and another;
  • they both use punctuation to structure and organise words and sentences;
  • there are multiple ways of writing the same code or “paragraph” to describe the same thing.

However, there are many ways that programming languages and human languages are different. One really important one is that human languages (and humans) can cope with ambiguity, but programming languages (and computers) generally can’t.

Computers are completely literal

Say a student is attempting the simplest programming question — printing a message “Hello, World”. Some of the incorrect attempts we’ve seen for this question are shown below.

Examples of incorrect solutions to “Hello, World”.

These are all incorrect in small ways, causing the computer’s compiler or interpreter to return an error. They are all examples of syntax errors** — the idea of the program is correct, but there are “spelling” and “grammar” mistakes in the code that stop the computer from understanding it.

Why do students make syntax errors?

Just like when you are writing in a natural language, it is easy to make spelling errors and typos through carelessness. However, when coding there is a larger conceptual issue at play. A lot of new programmers don’t realise that computers are completely literal and that they have no ability to guess what we mean. To a human, the meaning of:

A “Hello, World” solution with an error.

is obvious because it is very close to the correct statement. But to a computer this may as well be a different language. Once students realise this it can make a big difference to how quickly they learn to code.

How can you help students avoid syntax errors?

So how can you help your students avoid these errors when they first start coding? We have a few suggestions:

  1. Try an unplugged activity that demonstrates how literal computers are. For example, one fun activity is getting students to explain an algorithm for “making a sandwich” to their friend, who has to act out their instructions literally. Discuss how computers are different to humans!
  2. When students are learning to code it often helps if they can look up the syntax for the main statements and function calls so that they don’t get stuck on syntax errors. For Python 3 programming you could print our Cheat Sheet to use as a reference.
  3. Practice really simple (1–3 line) programs, checking each line of code really carefully. We have some example questions in our coding gym.
  4. Encourage students to pair program. One student uses the keyboard and types in their code. The other student watches everything they are doing, checking for errors as they type. After 5 minutes the students switch roles, to make sure both get a go at each. Lots of professional programmers use pair programming as there is evidence it reduces errors and leads to a better understanding of the code.
  5. For younger students you can use a visual language like Scratch or Blockly. These languages don’t allow students to make syntax errors, by controlling what structures are available. This avoids the frustration of syntax errors for new programmers. For example, try our free “Frozen Fractals” activity.

Obviously checking all your code really really carefully is critical, but understanding why you have to do so can help motivate students to do it.

For the pedants!

  • * The exact definition of a syntax error varies from language to language, and is often not defined at all. For example, a spelling mistake may cause a syntax or name error in Python because it is a dynamically-typed language, but may always cause a syntax error in statically-typed languages, like C++ or Java. We call them syntax and related errors here for simplicity.

How do you help new programmers avoid syntax errors? Please share your tips in the comments below!