How to: Debug code

View all subject updates

Failing

It is rare to write a working program the first time you try. Do not worry about failing. To fail is to make your First Attempt In Learning. Mistakes in your code show that you are trying.

When we fail, we have an opportunity to learn from fixing the problem and this helps us to understand how things went wrong in the first place. If we make no mistakes, we may not learn about all the different ways to tackle a problem and may actually miss a better solution.

If you or your students are feeling a little deflated about code not working, remember to have a look at Failing is good video.

If we make no mistakes, we may not learn about all the different ways to tackle a problem and may actually miss a better solution.

Debugging

Debugging is a systematic way for your students to iron out their mistakes. It is highly likely that your students will get something wrong and their code may not work the first time. At this stage, it might be tempting to leap in and try to solve the problem. Instead of giving them the answers, you should encourage your students to work through potential problems step-by-step, starting from basic issues and working up to more complex issues.

Here's a top tip, before checking code, always check hardware:

  • Are the batteries working?
  • Is everything connected properly?
  • Did the code upload correctly?

Once you have established these basic things are in place, you can start looking at the code line by line:

  • Are there any statements not inside the correct bracket?
  • Are there any sets of instructions that contradict another set of instructions?

Remember to tell your students not to get disheartened by code not working the first time - in the coding world, failing is good!

Code Smart Computing 7 11 Thumb

Computing | Ages 7-11

Code Smart

Code Smart is a computing and robotics education programme based on driverless car technologies. Code Smart takes students on a journey from complete beginners to code creators.

Code Smart Computing 11 14 Thumb

Computing | Ages 11-14

Code Smart

Code Smart is a computing and robotics education programme based on driverless car technologies. Students will develop their ability to code as well as their design thinking as they tackle smart city challenges.