Top 7 debugging tips for beginners

During our last Grok competition, over 50,000 messages were exchanged between students learning to code and tutors providing online support. We’ve seen all the ways new programmers get stuck. Here are our top 7 tips for getting students unstuck.

1. Break the problem down in words

When confronted with a question that isn’t a clear sequence of instructions and a blank code editor many students don’t know how to get started. Asking them to describe the problem in their own words helps them to break it down. For example:

Breaking a problem down into steps.

My program needs to:

  1. Print out “Let’s go to the movies.”
  2. Print out “What movie would you like to see?”
  3. Read in a movie as input from the user
  4. Decide if the movie is “WALL-E”
  5. It it is, print out “Great, let’s go”
  6. Otherwise, print out “Actually, I need to polish my transistors.”

Once they’ve understood and described the problem, you can ask “What’s the first thing you need to do?” to encourage them to find a starting point. This is often a piece of code that they’ve written before. For example, most beginners Python problems on Grok require reading a piece of input, so it’s a line of code that students write over and over again, and is an approachable starting point.

2. Run the code often

We often see students try to write the whole solution in one go, and then get stuck because they’ve got a number of different errors in their program at the same time.

Encourage students to build their solution step by step, running their program each time they add a new piece. This way, if their program stops working, they know the culprit was the last piece that was added and where to look for issues in the code.

3. Read the error message

The most common source of errors for new programmers is a syntax error (you can read more about these in our previous blog post). To teach students to debug their own syntax errors, encourage them to read the error message — it includes the line number on which the program failed.

Student is missing a closing quotation mark on Line 5.

Reading the error message is useful for spotting all kinds of errors. However, error messages can be confusing for new programmers. Encouraging students to read and try to understand these error messages will help them start a mental catalogue of which bugs produce the different error messages.

4. Check the line before

If you’ve scoured the line number reported in the error message and can’t see anything wrong with it, check the previous line. In some cases, for example a missing bracket at the end of a line, Python will go on to the next line of the program before it realises something is wrong and the error is reported.

Student is missing a closing bracket on Line 5, but the error is reported on Line 6.

5. Use print statements

It’s possible for a program to run without throwing an obvious error, but still contain logic errors which cause it to behave differently than expected. Often these kinds of errors are more subtle and harder to identify. One key strategy is to insert extra debugging print statements at various points in the program to find out what the program is doing at each point.

6. Structure the code so it’s easy to understand

Another problem students often have, especially when debugging logic errors, is that their code is written in a way that makes it hard for them (and anyone else!) to understand it.

For new programmers writing smaller programs, improving code legibility often just means giving variables meaningful names, using blank lines to visually separate blocks of code that do different things, and adding comments.

For more advanced programmers, structure also means making the code more modular by splitting or abstracting it into different functions, methods, or files, and commenting these appropriately.

7. Talk to the bear

Also called rubber duck debugging, this technique is similar to #1 but students have now written their program. Ask them to trace their program (ACTDIP029) — explain what it is doing line-by-line — perhaps to another student but even to an inanimate object (like a teddy bear or a rubber duck).

The idea is that the process of describing each line of the program explicitly helps the describer pay closer attention to small details and assumptions, and can help them see solutions that they missed before.

Rubber duck debugging.

Remember, when it comes to programming, everyone writes bugs in their code — even Margaret Hamilton. That’s why debugging is an important skill to develop for students learning to code.

Do you have any other tips to help students with debugging? Share them in the comments below!