Kōkiri Learn

Background reading · Technology

Bugs, and how to squash them

Why every programmer writes bugs, where the word comes from, and five strategies for finding and fixing them.

A bug is a mistake in a program that makes it do something you did not expect. Your sprite walks through walls. The score goes up by two instead of one. The game never ends. Every programmer, even the best in the world, writes bugs. What makes a good programmer is how calmly they find and fix them. Finding and fixing bugs is called debugging.

A real bug

People used 'bug' for faults in machines long before computers. But there is a famous story. On 9 September 1947, operators working on the Harvard Mark II computer in the United States found a moth stuck in one of its parts. They taped it into their logbook with the note 'first actual case of bug being found'. The team included computer pioneer Grace Hopper, who loved telling the story.

Three common kinds of bugs

  • Sequence bug: the right steps in the wrong order, like showing 'You win!' before checking the score. - Logic bug: a condition that is wrong, like 'if score > 10' when you meant 'if score = 10'. - Input bug: the program gets something it did not expect, like a player pressing two keys at once.
  • Five ways to hunt a bug

  • Reproduce it: find the exact steps that make it happen every time. - Read it aloud: explain your code line by line to a partner, or even a rubber duck. Often you hear the mistake yourself. - Change one thing at a time: if you change three things and it works, you won't know which one fixed it. - Show your values: make the score or position visible on screen while you test. - Save versions: keep a copy that works before you try something big.
  • Test like a player

    You know how your game is supposed to work, so you play it the 'right' way. Real players don't. Younger children click everywhere, hold keys down and ignore instructions. That is why testing with real players finds bugs you never imagined.

    Keep a bug log

    Write down each bug: what happened, what should have happened, the cause and the fix. Over time you will notice you make the same kinds of mistakes, and you will start catching them before they happen.

    Sources and further reading

    Written for Kōkiri Learn students in our own words. Check facts against the sources.

    Used in: Code a Game: Algorithms and Debugging