Lesson 2: Finding Bugs Step by Step
Estimated time: 15-20 minutes | Screen-Free Activity
What You Will Learn
By the end of this lesson, your child will be able to:
- Use the "Debugging Detective" steps to find a problem
- Check one thing at a time instead of guessing
- Test after each change to see if the fix worked
- Narrow down where a bug is hiding
Think Like a Detective
When a detective is trying to solve a mystery, they do not just guess who did it. They look for clues, check the facts one at a time, and slowly figure out the answer. Debugging works the same way!
The worst thing you can do when something is not working is change a bunch of things all at once and hope for the best. That is like a detective arresting everyone in town and hoping the right person is among them. Instead, good debuggers check one thing at a time.
Systematic Debugging: Finding bugs by checking one thing at a time in a careful, organized way. Instead of guessing randomly, you follow a plan to narrow down where the problem is.
The Debugging Detective Steps
Here are four steps you can follow every time something is not working. These steps work for code bugs, broken toys, recipes that taste wrong, and almost any problem:
- What did you expect to happen? Before you can find the bug, you need to know what the right answer looks like. Say it out loud: "I expected the cat to move to the right."
- What actually happened? Look carefully at what went wrong. Be specific! Not "it does not work" but "the cat moved to the left instead of the right." The difference between what you expected and what happened is your biggest clue.
- Check one thing at a time. Look at each step or each block and ask: "Is this one correct?" Start from the beginning and go through each part. When you find something that looks wrong, stop.
- Change one thing, then test. Fix the one thing you found, then test to see if it works now. If it does, great! If not, undo your change and try the next possible problem. Never change two things at once -- you will not know which one fixed it.
Talk About It (Parent and Child)
Practice saying the four steps together. You might even make up a hand motion for each one: (1) Point up (what should happen), (2) Point down (what actually happened), (3) Hold up one finger (check one thing), (4) Give a thumbs up (test it). Having a physical memory trick makes the steps easier to remember.
Example: The Broken Sandwich
Let us practice the detective steps with a fun example. Imagine you gave someone these instructions to make a peanut butter sandwich, and the sandwich came out wrong -- the peanut butter is on the outside of the bread!
The Instructions (With a Bug!)
- Get two slices of bread
- Put peanut butter on the outside of one slice
- Press the two slices together
Let us use our detective steps:
Debugging the Sandwich
Step 1 -- What did I expect? A sandwich with peanut butter between the two slices of bread.
Step 2 -- What actually happened? The peanut butter is on the outside of the bread, making a mess.
Step 3 -- Check one thing at a time:
- Step 1: "Get two slices of bread." That seems fine.
- Step 2: "Put peanut butter on the outside of one slice." There it is! It should say "on top of one slice" or "on the inside."
Step 4 -- Fix and test: Change "outside" to "top" and make the sandwich again. Does it work now? Yes! Bug fixed.
Example: The Wrong Shape
Here is another example. You told a friend to draw a square, and they drew a triangle instead. Here were the instructions:
The Instructions (With a Bug!)
- Draw a line going right
- Turn right
- Draw a line going down
- Turn right
- Draw a line going left
Let us debug:
Debugging the Shape
Step 1 -- What did I expect? A square (4 sides).
Step 2 -- What actually happened? Only 3 sides were drawn -- it looks like an incomplete shape.
Step 3 -- Check one thing at a time: Count the sides: right, down, left. That is only 3 lines. A square needs 4 sides. The fourth side (going back up) is missing!
Step 4 -- Fix and test: Add a step 6: "Turn right. Draw a line going up." Now test -- does it make a square? Yes!
Talk About It (Parent and Child)
Ask your child: "What kind of bug was this -- was a step wrong, or was a step missing?" (It was a missing step.) Then ask: "Can you think of a time when you forgot a step in something and it did not turn out right?" Forgetting steps and getting steps wrong are the two most common bugs.
The Three Most Common Bugs
Most bugs fall into one of three categories. Knowing this helps you know what to look for:
1. Wrong Step: One of the steps has the wrong information. Like saying "turn left" when you meant "turn right," or using the number 5 when you meant 50.
2. Missing Step: A step is completely left out. Everything else is correct, but there is a gap. Like forgetting to say "pick up the pencil" before "draw a line."
3. Wrong Order: All the steps are there and correct, but they are in the wrong order. Like putting on your shoes before your socks.
When you are debugging, ask yourself: "Is a step wrong? Is a step missing? Are the steps in the wrong order?" One of those three is almost always the answer.
Activity: Bug Detective Challenge
Screen-Free Activity (10 minutes)
What you need: Paper and a pencil.
What to do: Each set of instructions below has a bug. Use the four detective steps to find and fix each one.
Bug Hunt 1: Brushing Your Teeth
- Pick up the toothbrush
- Brush your teeth for two minutes
- Put toothpaste on the brush
- Rinse your mouth
- Put the toothbrush away
What kind of bug is it? Find it and fix it!
Bug Hunt 2: Drawing a Happy Face
- Draw a big circle for the head
- Draw two small circles for the eyes
- Draw a curved line for the mouth (curve it downward)
Hint: If the mouth curves downward, is the face happy or sad?
Bug Hunt 3: Making Lemonade
- Get a glass
- Fill it with water
- Squeeze a lemon into it
- Stir well
- Drink and enjoy
Hint: Something is missing. Would this taste good?
Answers: (1) Wrong order -- step 3 should come before step 2 (toothpaste before brushing). (2) Wrong step -- the mouth should curve upward for a happy face, not downward. (3) Missing step -- you need to add sugar!
Check Your Understanding
1. What are the four Debugging Detective steps?
2. Why should you only change one thing at a time when debugging?
3. What are the three most common types of bugs?
Key Takeaways
- Debug like a detective: look for clues, check facts, and be organized
- Follow the four steps: expect, observe, check one thing, fix and test
- Never change more than one thing at a time -- then test to see if it helped
- Most bugs are a wrong step, a missing step, or steps in the wrong order
- Be specific about what went wrong -- "it does not work" is not specific enough
Ready for More?
Next Lesson
In Lesson 3, you will apply your debugging skills to everyday life -- troubleshooting things that go wrong around the house.
Start Lesson 3Module Progress
You have completed Lesson 2! Two more lessons to go in Module 6.