The term "bug" in computing has an interesting origin. In 1947, Grace Hopper and her team at Harvard were working on the Mark II computer when they encountered a malfunction. Upon investigation, they discovered a moth trapped in a relay, causing the issue. This incident is often cited as the origin of the term "bug" in computer science, although the term had been used in engineering contexts before this event.
A bug is a flaw or error in a computer program that causes it to produce an incorrect or unexpected result, or to behave in unintended ways. More specifically, a bug occurs when there is a discrepancy between:
- The expected behaviour: How the program is intended or specified to function under certain conditions.
- The actual behaviour: How the program actually performs when those conditions are met.
This discrepancy can arise in various ways, such as:
- Incorrect output or calculations
- Unexpected program termination
- Unresponsive or frozen applications
- Security vulnerabilities
- Performance issues
🔑 Debugging is the process of identifying, analysing, and resolving these discrepancies to align the actual behaviour of the program with its expected behaviour.
Effective debugging involves several key steps:
- Establish or clarify the expected behaviour of your program. For example, if you call a function with a certain set of inputs, we should expect to get back a particular return value.
- Call the function/program you want to debug.
- Identify the difference between the expected behaviour and the actual behaviour (what happens when you actually run the code).
- Use various strategies (see below) to find out why the bug is occurring.
- Fix the bug and verify that the program now works as expected.
In Python, you can print or log variables to check if they're being updated with the correct values during program execution. The print()
function is a simple and effective way to log variables to the console, giving you a better idea of what's happening inside your program.
Stepping through code, also known as "playing computer," is a technique where you step through the execution of a program line by line to check the values in memory as the program runs. You can do this manually using a piece of paper or use a tool like Python Tutor for visualisation.
Static analysis tools are powerful allies in the battle against bugs. These tools analyse your code without executing it, helping you identify potential issues early in the development process. Pylint and Flake8 are popular static analysis tools for Python that can check your code for errors and ensure it adheres to style guidelines.
This technique involves explaining your code, line by line, to an inanimate object (traditionally a rubber duck). By verbalising your code, you often spot the issue yourself. This method can be surprisingly effective in helping you understand your own code better and identify logical errors.
We recommend you tackle these problems in the following order. The problems will generally increase in difficulty so it's important you're comfortable with the earlier problems before you start tackling the later problems.
As a group, you should check you understand the task by going through some problems together and asking questions. We recommend you debug the following problems together:
square
increment
capitalise
percentage-change
pence-to-pounds
12-hour-clock
convert-temperature
times-table
rotate-angle
right-angled-triangle
fizz-buzz
sum-digits
count-char
In this section, you can develop your skills further by using the VSCode debugger to step through the execution of the code in the problems below. Check out the documentation to familiarise yourself with this tool and debug the code.
multiple-of-five
find-century
is-valid-triangle
text-in-div
is-palindrome
find-closing-parenthesis