Finding bugs like a detective

Programming guides

A bug is a mistake that makes a program behave differently from what you intended. Debugging means investigating and fixing it. Experienced programmers do this too; finding a bug is part of making something work.

Gather clues

A detective starts with evidence. Write down what you expected, what happened instead, and the steps that made it happen. Try to reproduce the same problem before changing the code.

An error message may point to a spelling or syntax problem. A program can also run successfully and still calculate the wrong answer. That is a logic error.

A program with a clue

We want three stars worth two points each. This example deliberately uses the wrong calculation, but it is valid Pliro code.

# language: en
let stars = 3
let points = stars + 2
say points

Predict, then run

Calculate the expected score yourself: three groups of two make six. What does the written code actually calculate?

5

The + adds only two points once. It does not mean “two for each star.” We can test one specific fix:

# language: en
let stars = 3
let points = stars * 2
say points

The corrected result is:

6

We changed the operation, not the number of stars. Changing stars to 4 in the broken version would accidentally print six while leaving the rule wrong.

Your challenge

Try zero stars and five stars with the corrected version. Explain why their scores should be zero and ten. These extra checks help show that the rule works beyond one lucky example.

A common mistake

Changing several unrelated lines at once hides which change helped. Keep changes small. If a message points to a line, inspect that line and the one just before it: a missing quote or bracket can affect the next line too.

When asking for help, share the smallest example that shows the problem, your expected result, and the exact message. Use pretend data rather than personal information.

Where next?

Following a program · Testing · Understanding error messages