What is debugging?
Programmers write computer programs in a language we call code. However, because no one is perfect, computer programmers sometimes make mistakes when writing their code. These mistakes are known as ‘bugs’.
A complex program can be made up of lots of lines of code and it is normal for new programs to have some bugs.
An important part of programming is testing your program and 'debugging' (which means removing the bugs).
What is a bug?
In real life there are many types of bugs, like beetles, moths or houseflies, and in computing there are also different types of bugs, for example:
A syntax bug is an error caused by something the programmer has typed – it could be a spelling mistake or a command that the computer doesn’t understand.
A logical bug is an error which means that even though the computer is able to carry out its instructions, it doesn’t act as the programmer intended or the user expects.
Did you know?
In 1947 a real bug, a moth to be precise, was found in a computer, stopping it from working. Although the term ‘bug’ was already in use in programming, this is often thought to be the first time a real world bug was the cause of a computer not working!
A mistake in the code, or a bug, could be the difference between a program working properly, not working at all - or working but doing some pretty strange things!
Finding these mistakes and getting rid of them is really important and it is known as 'debugging'.
Let's imagine you have just created a new computer game in Kodu or Scratch. In the game, when you press the arrow keys your character moves around the screen trying to avoid an enemy - in this case an evil frog.
You want to show everybody your game because it looks great but you decide to check it works properly first.
When you run your program, the evil frog starts moving towards you but when you press the arrow keys, nothing happens. There must be a bug! The program works, but not properly.
So where do you start?
Do you look at all the programming for your game? Or do you look at the part of the program that isn't working properly?
Both options would work but a good debugging strategy would be to look for the part of the program that isn't working properly. Rather than going through every individual line of code searching for an error, computer programmers try to think logically about where the problem might have occurred. This is much more efficient.
You've programmed a toy car to drive around a map, but when it reaches a house the car continues to drive through it! Does your program have a syntax bug or a logical bug?
Your program has a logical bug. A logical bug means that the program runs but doesn't do what the programmer intended. Here, the car is not turning when it is meant to.
A syntax bug is an error in the code which usually stops a program from working. The car is moving, it’s just not going where it should... so the problem is probably not a syntax bug.