Finding and debugging the top 5 errors in Scratch programs
Sean McManus offers some top tips for finding and fixing the most common errors in Scratch
Whether you program as a hobby, or are a professional programmer, you'll spend at least some of your time trying to find and fix errors in your programs (debugging). Scratch is no exception. Its use of interlocking blocks for commands eliminates a lot of typing and logic errors that you might get in other languages, but it's still easy to make programs that don't behave as you might expect.
Here are the top 5 Scratch programming errors I've come across while I've been running a Code Club:
- Creating variables for one sprite or all sprites when it should be the other way around. This is hard to fix. You have to delete the variable, create a new one and start over with adding the right blocks to the program. This probably accounts for a third of the errors I've seen in my Code Club, even though we remind the group often about this. It's quite a confusing concept, and it's not easy to change.
- Using the wrong (lookalike) block: This happens most often when somebody uses a block like [set x to 10] instead of [change x by 10]. Also common around blocks for changing variables. This probably accounts for about 20% of the bugs in my Code Club group.
- Putting the wrong blocks in brackets. It makes a huge difference whether a block is inside or outside a [repeat 10] block's bracket, or inside or outside an [if] block's bracket. It's particularly important to check the blocks near the end of the bracket to make sure the right blocks are inside.
- Putting scripts on the wrong sprite. You can copy a script to a sprite by dragging it into the right sprite in the Sprite List. Remember to delete it on the sprite where it doesn't belong, too. That last bit is the kicker that tends to cause ongoing problems.
- Putting blocks in the wrong order. You often have a lot of flexibility over the order that blocks go in, but sometimes it really matters. In particular, I've seen my group initialising variables (setting their starting value) after they've started using them, or inside a loop so they never seem to change.
- BONUS: Relatively uncommon but potentially mystifying error - Data races: This usually only happens when you're making your own programs, rather than following instructions to make other people's from books or the Code Club materials. It happens when two scripts are programmed to start at the same time, but you don't determine which will actually run first. If you're using several green flag blocks, especially on the same sprite, this might cause unpredictable behaviour. See my blog on Scratch data races for more insight.
Whether you're a Scratcher, a teacher or a Code Club volunteer, I hope that will help you to quickly find and squash the most common bugs. For more tips on using Scratch, see my books Raspberry Pi For Dummies, Scratch Programming in Easy Steps and Cool Scratch Projects in Easy Steps.