Your code doesn’t always run as you intend it to—and that’s okay! One of the most important parts of programming is figuring out why your code isn’t working and how you can fix these problems (we call them bugs).. This process is referred to as “debugging”.
- Accidental mistakes: We all make mistakes! Making mistakes is an important part of learning. Here’s a list of common problems that you can use to watch out for:
- Typos — putting the wrong number or wrong variable
- Moving code around by accident
- Logic errors: These are bugs that are caused by not understanding how something will work. For example, you might have planned out your code to solve a problem by using one particular approach. But then find out that approach doesn’t actually work correctly, or doesn’t work under particular circumstances.
Here are general tips with debugging:
- Work out what you want to happen, and compare it to what is actually happening when you play the project.
- When is the problem happening? Does it happen after you do a specific action? Try narrowing it down.
- Once you have a better idea of where the problem is, go through your code, block by block, and predict exactly what you expect the block will do. Is it the same as what you want to happen?
- Check the order of your blocks. They will run exactly in the order that they are placed.
- A tip with variables: it can help to be able to check if a variable has the value that you would expect. You can do this by using a Text object, and making it Set Text to the variable in a Repeat Forever block. (For example, if you expect it a variable to increase by 10 when the iPad is tapped, you will be able to see the variable increase by 10.)
Sometimes it might be easier to try making a workaround if you can’t fix the exact cause of the problem directly.