Debugging & Secret Codes
PrimaryA mistake in a set of instructions is called a bug, and finding and fixing that mistake is called debugging. Every programmer — even the most experienced ones — writes bugs all the time. Debugging is not a sign you did something wrong; it's a normal, expected part of building anything.
Computers can also follow special instructions for turning a message into a secret code. One of the oldest tricks is a shift cipher: swap every letter for the one a few places later in the alphabet. Shift by 1: A→B, B→C, C→D … so "CAB" becomes "DBC".
Debugging is detective work: you compare what you expected to happen with what actually happened, then hunt for the exact step where they first disagree. It is almost never the computer's fault — computers only follow instructions exactly — so the bug is really a mistake, gap, or wrong-order step in what we told it to do.
Secret codes are a fun early taste of a very serious grown-up topic. Keeping messages secret using clever rules is called cryptography, and it protects your family's messages, bank cards and passwords every single day. You'll return to real cryptography much later — all the way in college!
Big idea. A bug hides in the instructions, not in the machine. And a secret code is just a very exact set of instructions for swapping one thing for another — which is exactly why computers are so good at both making and breaking them.
Activity: Fix the Recipe. Here is a buggy recipe for a peanut butter sandwich: (1) Eat the sandwich. (2) Put peanut butter on the bread. (3) Put two pieces of bread together. (4) Take two pieces of bread out of the bag. Find the bug — the steps are in the wrong order! Rewrite the four steps in an order that actually works.
Activity: Secret Code Wheel. Using a shift of 1 (A→B, B→C, … Z→A), encode your first name letter by letter. Trade your secret code with a partner and have them decode it back by shifting each letter one place backward.
Q1 What is a bug?
A bug is a mistake in a set of instructions that makes it do the wrong thing, or get stuck. It could be a missing step, a step in the wrong order, or an unclear instruction.
Q2 Why isn't a bug the computer's fault?
Because a computer only ever follows instructions exactly as written. If the instructions themselves are wrong, the computer will still follow them perfectly — right into the mistake. Fixing a bug means fixing the instructions, not "fixing" the computer.
Q3 How could someone crack (break) your shift-by-1 secret code without being told the shift?
There are only 25 possible shifts in the whole alphabet, so someone could just try every single one until the message makes sense — this is called trying "all the possibilities," and it works quickly because there aren't very many. Much later, in Stage 5, you'll learn why some grown-up secret codes have so many possibilities that even a computer trying billions of guesses a second could never check them all.
How the ideas connect
Every key idea in this chapter, branching from the core concept — use it to see the whole picture at a glance.
The process, step by step
Worked problems, step by step
Follow each solution line by line, then try to reproduce it on paper before moving on.
Example 1Encode HI with a shift cipher of +1 (each letter moves forward one).
- H moves forward one to I.
- I moves forward one to J.
Example 2Decode the message KB, shifted by +1. Move each letter back one.
- K back one is J.
- B back one is A.
Now you try
Work each one out first, then tap to reveal the worked answer.