CreatiCode XO (AI Assistant) - Debugging
-
Introduction to Debugging
One area where every Scratch programmer needs help is debugging. When we say a Scratch program has a “bug”, we are really saying the program is doing something unexpected. This usually happens because what you expected to see isn’t what actually appears on the screen. Most of the time, the project is doing exactly what you told it to do through your code—it’s just not what you wanted.Think of debugging as being a detective in your own computer game. Your mission is to find the clue—a specific part of your code or a single piece of information you’ve entered—that’s causing the unexpected behavior. You can look at each part of your code closely, or you can play detective by watching what the project does and making smart guesses about what might be wrong.
Now, you might be wondering, how does XO fit into all of this? Think of XO as your trusty sidekick in this detective game. In this tutorial, you’ll discover several strategies for debugging with XO’s support.
Important Note: Do not rely solely on XO to detect all problems in your code. XO does not execute your code but reviews it statically, which is challenging even for experienced programmers. Additionally, XO’s capabilities in logical reasoning and error detection are limited. Consequently, the primary responsibility for identifying issues rests with you, and XO should serve merely as a supporting tool.
Explain “Expected vs Actual”
The biggest mistake in using XO for debugging is asking questions that do not clearly explain the problem, such as “it is not working!”, “what’s wrong?” or “help me debug!”.
XO doesn’t know what you are expecting to see. Also, XO can only look at your code, and it can not run it, so it does not know what you observe when the program runs. When it is not clear what’s the issue, XO will ask for clarification:
The recommended format to ask XO to debug is to include these 2 parts:
- What I expect the program to do
- What I observe when the program runs
Here are some good examples:
- I expect the Dog to say 1 to 10, but it only says 1 to 8
- I am trying to move the clone to a random position, but I see that it always appears in the same position
- I thought the list would contain 10 numbers, but when I ran it, I only got 1 number
Sometimes, the expected behavior is obvious from the description, so you may omit it. For example:
- The chatbot never responds to my question --> implying we expect the chatbot would respond in a short time
- The sprite doesn’t move, why? --> implying we expect it to move
- The 3D table’s top is not connected with its legs --> implying we expect the table top to connect with the legs.
Common Coding Mistakes
For commonly made coding errors, XO can spot them right away. It will only report one or two such problems each time to avoid overwhelming you. Also, the problem it reports may not directly resolve the issue you are facing. Yet, you should still fix the reported problem first, then ask XO for more suggestions.
For example, here is a simple program with a variable initialization bug. XO can not only point out the issue, but also suggest how to fix it.
Note that XO understands all the new blocks used in CreatiCode, and can point out issues associated with their usage. For example, here is a common issue in 3D programs about initializing 3D scenes:
Narrow Down the Problem’s Scope
For more complex issues and larger projects, XO may not have the immediate answer. In these situations, it’s important to first figure out where the problem might be coming from by focusing on specific parts of your project. You can often determine the most important parts to look at by thinking about how your program is supposed to work. For instance, if your character isn’t moving when you press a button, you should check the parts of your code that are supposed to respond to button presses and make your character move.
If you’re not sure which parts of your code to look at first, you can get XO’s help. If you explain the problem well, XO can help figure out the best place to start looking.
As a practice, please open the following project and remix it:
https://play.creaticode.com/projects/6616a1a7564fc287b0e9b879
Next, modify this block to set the y speed to -450 instead of 450, which will be the bug we try to find.
Then you can ask XO to help locate the issue, and it will suggest a few relevant places:
Use XO as a “Rubber Duck”
Another commonly used debugging technique is called “Rubber Duck Debugging”. The idea is to describe your code to a rubber duck toy that will listen to you. This process often helps us rethink the logic of the program and identify flaws. In addition, it forces us to pay attention to every block and every input as we describe the code. It sometimes helps us spot small mistakes we have overlooked.
XO can serve a similar role as a rubber duck. You can simply tell XO to serve as your rubber duck, or you can click the question mark button directly:
After this, XO will enter a special mode: it will respond with short sentences, assist you in walking through your code, and point out any potential mistakes as you talk.For example, with the issue of wong y speed above, XO will suggest that issue as we discuss with it:
To exit this “rubber duck” mode, you can simply clear the chat to restart the conversation: -