You’ve spent ages writing a brilliant piece of code, are excited to start running it, press ‘start’ and… nooooo – your code does something completely different or maybe doesn’t even run at all! An important part of programming is testing your program and ‘debugging’. While to some the term debugging may mean removing spiders before you climb in the bath, in the world of coding, debugging is the process of spotting and eliminating potential and current faults in computer code that can cause the program to go astray.
What is a bug?
A computer bug is an error or flaw in a computer program that prevents it from working correctly. Bugs can arise from human mistakes and errors in a program’s code or design.
Bugs have a wide range of effects, with varying levels of inconvenience to the programmer. Some bugs have a subtle effect on the program, the code can still function and lie undetected for a long time. More serious bugs are a major interference and may cause the program to crash or freeze.
It has been said that while most software programs have bugs, well-written programs contain relatively few bugs, and the bugs they do contain typically do not interfere with the program’s performance.
Types of bugs:
There are more than 1 million species of real bugs; from flies to butterflies and beetles to bees. While there are different types of bugs in your garden, there are also different types of bugs in computing, for example:
A syntax bug is an error in the code which will stop the program from working. This error, caused by something the programmer has typed, is known as a syntax bug and can range from small spelling mistakes to a command that the computer doesn’t understand.
A logical bus is an error that doesn’t prevent the program from working. However, even though the computer is able to carry out instructions, it doesn’t act as the programmer intended/expected.
Do you know the difference?
You’ve programmed Marty to walk around an obstacle on a route. You hit ‘start’ on the program and Marty walks forward BUT, instead of avoiding the obstacle, Marty walks straight into it! Does your code have a syntax bug or a logical bug?
Marty is moving, just not going where they should! So, as the code is still running but not in the way intended, we can say that your code has a logical bug.
Process of debugging:
You’ve detected a bug but don’t know where to start? Should you look at the entire program or should you look at the section of the program that isn’t working properly?
For shorter programs, both options would work. However, for longer, more complicated programs a good debugging strategy would be to look for the part of the program that isn’t working properly, saving you countless hours going through every individual line of code! When searching for an error, you should try to think logically about where the problem might have occurred. This is much more efficient.
To debug a program, you must start with recognising there is a problem, then isolate the source of the problem, and then fix it. When the bug is removed, then the program is good to go!
Steps involved in debugging: