Tutelage

Lessons › coding and computer science · taught by Leo

Debugging basics: find bugs step by step

Learn how to debug by testing one small change, reading error messages, and tracing variables to find the first bad step.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app

What debugging really means

Debugging is not guessing. It is a careful search for what the program is really doing.

You compare the code, the input, and the output.

Then you change one thing at a time until the bug appears or disappears.

Big idea: find the first bad step

Most bugs are not random. They come from one wrong step in a chain.

Your job is to find the first place where the program's logic stops matching your goal.

Once you find that place, the fix gets much easier.

Predict the next move

A program gives the wrong answer. What should you check first?

  1. Everything at once
  2. The smallest recent change
  3. Only the final output
Show answer

The smallest recent change: Good. Start with the smallest recent change, because that is often where the new bug came from.

A tiny bug hunt example

Suppose the goal is to double four and get eight.

The code says x is four, then adds one. That gives five, so the bug is the wrong operation.

Changing plus one to times two matches the goal.

What changed the result?

In the example, what caused the wrong output?

  1. Using + instead of ×
  2. Using the number 4
  3. Printing the result
Show answer

Using + instead of ×: Yes. The operation was wrong, not the number itself.

Why one test at a time works

This works because one change gives you one clue.

If you change many things, you do not know which one helped or hurt.

So keep the experiment small and clear.

Read the error message first

Error messages are hints, not insults.

They often point to the line where the program noticed the problem, or to the kind of problem you made.

Read the message before changing code.

What should you read first?

When you see an error message, what is the best first step?

  1. Ignore it and guess
  2. Read the message carefully
  3. Delete random lines
Show answer

Read the message carefully: Exactly. The message is a clue, so read it first.

Watch variables like clues

Variables are like labeled boxes holding values. Watch how they change.

If one value becomes strange, follow where it came from.

That trail often leads right to the bug.

Watch out: random changes

A common mistake is changing code until it seems to work, without understanding why.

That can hide the bug for today and bring it back tomorrow.

A good fix is one you can explain.

Your turn: choose the best habit

You find a bug. What is the best habit?

  1. Change many lines and hope
  2. Test one small change and check the result
  3. Never use error messages
Show answer

Test one small change and check the result: Yes. Small change, clear test, clear result. That is real debugging.

Debugging checklist

Here is the checklist to use on your own.

First make the bug happen again. Then read the clues and change one thing.

Finally test again and see what changed.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app