Chapter 05

Debugging & Secret Codes

Primary
At a glance
Core ideaA bug is a mistake in the instructions, not in the machine.
Key termDebugging — compare what you expected with what actually happened to find the slip.
You can…Encode a message with a shift cipher and decode it back.
Watch outA 25-key shift cipher is cracked by simply trying every shift.
Theory

A 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".

Explanation

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.

Practical

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.

Q&A
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.

Concept mind map

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.

bugexpected vs actualfind the slipshift cipherencodedecodeDebugging and ciphers
Infographic

The process, step by step

Step 1Run itFollow the instructions and watch what actually happens.
Step 2CompareLine up what you expected against what really happened.
Step 3LocateFind the exact step where the two first disagree.
Step 4Fix and retryCorrect that step and run again to confirm.
Solved examples

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).

  1. H moves forward one to I.
  2. I moves forward one to J.

Example 2Decode the message KB, shifted by +1. Move each letter back one.

  1. K back one is J.
  2. B back one is A.
Practice problem set

Now you try

Work each one out first, then tap to reveal the worked answer.

1What is a bug?
A bug is a mistake in the instructions, not a fault in the machine.
2How do you debug?
Compare what you expected with what actually happened, find the step where they differ, then fix it.
3What is a shift cipher?
A secret code that moves each letter forward by a fixed number, for example +3 turns A into D.
4Encode CAT with a shift of +2.
C to E, A to C, T to V, giving ECV.
5How do you decode a shift cipher?
Move every letter backward by the same shift that was used to encode it.
6Why do programmers expect bugs?
Because it is easy to leave out or mis-order a step, so testing and fixing is a normal part of the work.