When it comes to teaching kids problem-solving, few methods are as powerful as learning to debug code. The process of finding bugs and implementing debugging strategies is great practice for problem-solving in other areas of a child’s life.
These simple guidelines will make it easy to introduce debugging to kids, setting them on the path to becoming better coders and problem solvers.
Before we get to the debugging process itself, we need to talk about finding bugs. The best way to do this is through a process called incremental testing.
No programmer wants to deal with a dozen or more bugs all at once. Moreover, they don’t want to have to search hundreds of lines of code to find the source of a bug. This is why — instead of finishing a large project and then testing — programmers test the code as they go, looking at only a few lines of code at a time.
Even the most complicated computer program, video game, or algorithm can be decomposed into tiny steps. Coders take these individual steps, create them one at a time, and test each as they go. By following this best practice, even beginner coders can manage debugging with ease.
What to Do When a Bug Is Introduced
Once your student has found a bug, they should try to figure out exactly where and why it is occurring. The best way to start on this is to just take a look at what the code is doing and compare that to what the code is supposed to do.
Every computer programming language (Scratch, HTML, Python, etc.) has a way of printing or displaying variables. Using this with the variables affected by a bug can help narrow the source of a bug down to a single line of code.
▶ Practice debugging: Step-by-Step Coding Tutorials for Kids
Here’s how it works.
- Go to the section of code where the bug was found.
- Print the affected variables at the very beginning of the section.
- Compare the printed value to what the variable’s value was expected to be.
- If the values match what is expected, try printing them at the next line of code and check again.
- Keep doing steps 3 and 4 until they find the first line of code where the actual and expected values of the variable don’t match.
In some cases, the bug may be obvious now that the child has figured out where to look in the code. In other cases, they may find more variables that are impacted and have to repeat the 5 steps until they find and fix the root cause.
The process described above may look complicated and tedious at first, but a methodical approach is the best way to find bugs. When a child is frustrated, it can be tempting for them to try random ideas hoping that trial and error provides a solution. Avoiding this impulse is important, but it takes time to learn.
Patience, focus, and determination may not be technical skills, but they are still practicable skills just like coding and logical thinking. One of the most difficult parts of debugging is remaining calm and working at the problem with a linear approach — one step at a time — until a solution is found.
The Rubber Duck Method
If the cause of a bug is not readily apparent, there are other methods that can aid in the process.
One of the best ways to master debugging is by teaching debugging. Young children can turn themselves into educators using what might be the cutest name of any process in all of computer science: the rubber duck method.
Here’s how it works.
- Have your student find their rubber duck. It doesn’t have to be a rubber duck — any favorite toy will do.
- Have them explain their code to the toy one line and even one symbol and syntax choice at a time. Ducks are slow to understand, so the more detail in the descriptions the better.
- Watch the eureka moment happen!
Thinking out loud is great practice. Explaining those thoughts to someone else is better. Having to make those explanations simple enough for a rubber duck to understand is even better.
When a kid comes across a piece of code that they can’t explain, they are likely to find that is where the bug is occurring. On top of finding bugs, this process also cements the child’s understanding of their computer coding.
Advanced Debugging Tools
As your student advances to more complicated coding activities, they will need more advanced tools. Most coding languages, aside from Scratch and other block-based languages, have debugging tools that are far more powerful than the methods that we have talked about here.
These tools do, however, require more advanced computational thinking and coding skills to use properly. There is no need to rush into these tools. Even advanced coders that have access to more complicated tools still use printed variables and variants of the rubber ducky method. It never hurts to master the simple methods! This article originally appeared on junilearning.com.