🐞 Debugging: Finding and Fixing Bugs

4th Grade 💻 Coding

Even the best programmers in the world write code with mistakes — every day. A mistake in software is called a bug, and the work of finding and fixing it is called debugging. It isn’t failure; it’s a normal and even fun part of programming, a lot like detective work. In this lesson we learn the kinds of bugs and the detective’s tools.

Free · US Letter · no sign-up
Google Classroom Save to Pinterest Email me

Where the name comes from

“Bug” means insect, and engineers were already using the word for glitches in machines before there were computers, back in the 1800s. But there’s a famous story: on September 9, 1947, at Harvard University, a team working on a giant computer called the Mark II found a moth stuck in one of the machine’s relays. They taped the moth into the work logbook and wrote next to it, jokingly, that it was the “first actual case of bug being found.”

The story is linked to Grace Hopper, a programmer and U.S. Navy officer who was part of the team and loved to tell it. She didn’t invent the word, and it isn’t certain that she was the one who found the moth, but thanks to her the story became famous. The logbook page with the moth is kept to this day at a museum in Washington, D.C.

Three kinds of bugs

The first kind is a syntax error — a mistake in the “grammar” of the programming language: a missing quotation mark, parentheses that were never closed, a colon you forgot. The computer can’t even understand the code and refuses to run it. For example, print("Hello) — the closing quotation mark is missing. The good news: the computer finds these bugs by itself and shows you where they are.

The second kind is a runtime error: the code is written correctly, but something goes wrong while it’s running — for example, dividing by zero, or using a variable that hasn’t been created yet. The program starts running and stops in the middle. The third kind, and the sneakiest, is a logic error: the program runs all the way to the end without any message, but it does something different from what we wanted. For example, we wrote score = score - 1 instead of plus, so the score goes down when you collect a coin. The computer won’t catch this by itself, because as far as it’s concerned, everything is fine.

Reading the error message

Lots of beginners get scared of red error messages, but they’re really clues the computer leaves for the detective. A typical Python message tells you which line had the problem and what kind of problem it was. SyntaxError means a syntax error. NameError means we used a name the computer doesn’t know — maybe a typo, like Print with a capital letter instead of print. ZeroDivisionError means trying to divide by zero.

Rule one: read the message all the way to the end, slowly. Rule two: go to the line it points to — and sometimes to the line before it, because the real mistake (like parentheses that were never closed) is sometimes a little earlier. Rule three: fix one thing at a time and run the program again, so you know what helped.

The detective’s toolbox

There are a few debugging methods every programmer uses. Printing: add print in different places to see the value of a variable at each step, and find out where it “goes wrong.” Tracing: go through the code in your head or on paper, line by line. Splitting: turn off half the code and check whether the bug is still there — that way you narrow down the search. There are also special tools that run the program slowly, one line at a time.

And there’s a famous method with a funny name: “rubber duck debugging.” You explain the code, line by line, to a rubber duck (or a friend, or your dog). Very often, halfway through the explanation, you hear yourself say something like “And here I add one… wait, why am I adding one here?” — and you’ve found the bug. Most important of all: patience. Every bug you find teaches you something, and over time you’ll spot bugs faster.

For example

  • Ava added print(score) inside the loop and discovered that the score was being reset to zero every time around.
  • Liam explained his code to his little sister, and halfway through he noticed he’d forgotten a parenthesis.
  • When the game froze only on level 3, Sofia checked just the code for level 3 — and narrowed down the search.

Key facts

  • A bug is a mistake in software, and debugging is finding and fixing it.
  • In 1947 a moth was found inside a computer at Harvard and taped into the logbook — but the word “bug” for a glitch is older than that.
  • A syntax error is a mistake in how the code is written; a logic error is code that runs but does something different from what we wanted.
  • An error message tells you the kind of problem and the line number.
  • In rubber duck debugging you explain your code out loud, and find the mistake along the way.

📌 Remember: Debugging = detective work: clues, a hunch, a test, a fix.

Quick quiz

  1. What is a bug?

  2. What is debugging?

  3. What was found inside a computer at Harvard in 1947?

  4. Which mistake is a syntax error?

  5. Why is a logic error especially sneaky?

  6. What should you do first when an error message appears?

  7. What does NameError mean in Python?

  8. What do you do in rubber duck debugging?

  9. Why add print while debugging?

💭 Think and talk about it

  1. Tell about a mistake you once made (not just on a computer) and how you found it. Which “detective method” did you use?
  2. Why do you think a computer can find a syntax error by itself, but not a logic error?
  3. Try explaining to a friend how to make a sandwich, step by step. Did you find a “bug” in your instructions?

Questions kids ask

Do professional programmers write bugs?

Yes, all of them. A big part of a programmer’s job is testing and fixing. That’s why software companies have people whose job is to test software, and special programs that check code automatically.

Who found the first bug?

There is no “first bug” — the word was used for glitches in machines back in the 1800s. The famous story is the moth found in 1947 in the Mark II computer at Harvard. It’s linked to Grace Hopper, who loved to tell it, but it isn’t certain she was the one who found it.

What do you do when you can’t find the bug?

Take a short break — lots of bugs get found after a rest. Then narrow it down: add print lines, turn off parts of the code, or explain it out loud to someone. And you can ask a teacher or a friend for help — that’s totally fine.

🔎 Ask Thinkapedia another question