Changing Your Mind (Debugging)
Do you ever change your mind? Are you always right or do you find that sometimes you've made a mistake? One of the greatest things about computer programming is that it's okay to make mistakes. It lets you try things out, fail, and try again, all the while giving you feedback about how you're doing so you can keep getting closer to the correct program.
Today, instead of starting from scratch to help our angry friend navigate the maze, we're going to start with an existing program that has a mistake. Just like a computer programmer, we're going to call this mistake a “bug”. When we fix it, we're going to say we “debugged” the program; we're squashing the bug!
Before we head over to Code.org, we're going to introduce an activity that will help us illustrate programming in the real world: cup stacking. This is just like programming me and making me walk around the room, but it's a little more contained and easier to study what's going on. We'll use cup stacking throughout the rest of the semester to help us get a feel for ideas in programming.
First, the ground rules. I have a stack of four cups. I only understand the instructions “up”, “down”, “right”, “left”, and “flip”, which we'll write as follows:
- U: up
- D: down
- R: right
- L: left
- F: flip
I'll start out with my hand on the stack of cups and take whatever instructions you call out. You'll find that we can make quite of variety of cup creations, for instance:
URRDLL, URRRRDLLLL, URRRD
URRDLL, URRRRDLLLL, URRRFDLLL, URRRD
URRDLL, URRRRDLLLL, URRFDLL, URRRRFD
But what happens if we forget an instruction? Let's say we forgot an R in the first list of instructions (see the x below). Since we don't move the final cup right enough, we wind up stacking it on top of the first cup!
URRDLL, URRRRDLLLL, URRxD
We know how to fix this bug since we introduced it: just add the R back to the list of instructions. In our exercises today, we may have missing instructions, too many instructions, or instructions that are simply wrong. To debug, we can add, remove, or replace instructions.
Thinkersmith's material is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.