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!

activity age bookmark-fill icon bookmark boy-girl calendar chevron-down icon close icon compass cross icon download encounter-live facebook gallery icon instagram icon Instagram icon linkedin links icon location mail icon map next play previous professional-development right-arrow icon search share single image social-media speaker spinner icon students subject-update teacher-resources tick icon time twitter user video-lesson video youtube