So, you’ve created a brilliant application that will make you rich, famous, and maybe even happy. You’ve learned to program in all the necessary languages and spent months writing lines of code. From time to time, error messages appear, but you aren’t too worried about them.
It’s finally time to present your project to the world. Hundreds of people, including future investors, have gathered to hear what you have to say. But when you start your program, disaster strikes—it crashes!
The crowd is getting impatient, and the investors are dismayed. Every time you try to make your program work, more bugs appear. You frantically type instructions to resolve them, and you wake up with a start! Phew, it was all just a bad dream!
However, this incredibly stressful event can also happen in real life. All programmers, even the best in the world, encounter bugs during their careers. Bugs can make their programs malfunction or even stop working altogether.
Bugs are an integral part of programming, just like making mistakes is part of being human. And in fact, it’s a good thing that bugs exist.
Wait… bugs are giving me nightmares, and you’re telling me that’s a good thing?
It might be surprising, but the reason is very simple. Bugs help you discover the subtleties of a language and learn more about algorithmics while sharpening your problem-solving skills. They are an invitation (albeit an unwanted one) to enter into the complexities of the language.
Just one bug is enough to make a whole program crash. However, not all bugs are the same. They can be divided into three main categories.
Bug Category 1: Syntax Error
Just like the human language, each programming language has its own grammar. This grammar can incorporate symbols such as colons :
, or case sensitivity when using certain mechanisms (functions, classes, etc.).
If this grammar is incorrect, it creates a syntax error bug. When this type of error is present, your program won’t run. Here’s an example:
def sayHello(numberOfTimes, name)
for i in range(0, numberOfTimes)
print("Hello {}".format(name))
sayHello(5, "Bob")
The above code describes a function that displays the message “Hello Bob” several times. However, it contains some syntax errors. Can you spot them? 👀
One is that Python grammar requires the symbol :
to follow certain types of instructions—in this case, creating a function and using a for
loop—so it can tell where the instruction ends.
Python grammar also requires correct indentation—this is a common cause of errors, as it’s easy to forget the number of spaces you’ve used.
That one was easy! To correct it, add :
at the end of the first two lines, and indent the line that contains the for
loop. The program will now work as it should. 😄
Bug Category 2: Runtime Error
Imagine you’ve followed the grammar of your programming language perfectly, but a bug occurs when you run the program. This is a runtime error.
This type only becomes apparent after the program has launched. For example:
print("It’s the " + int("10") + " th time I’ve said ‘Hello’")
There are no grammar errors in the code, but when the program is launched, one appears by surprise! 💣
Why would that be? 🧐
Because concatenation using the +
symbol only works when you have a string on both sides. In this case, there’s a string on one side, It’s the
, and a number on the other, "10"
. So to fix it, you must remove the integer constructor int()
in front of the variable "10"
. This constructor casts a variable to transform it into an integer. Problem solved!
Bug Category 3: Logic Error
Programming is about writing algorithms. Algorithms are a way of dividing a problem into more easily achievable steps. For example, you create an algorithm when you give a friend directions to the park.
Logic errors sometimes occur when using logical operators and comparison operators (i.e., the instructions and
, or
, less than ( <
), greater than or equal to ( >=
), etc.).
Imagine you want to write a function sayGoodAfternoon()
using a while
loop, like this:
def sayGoodAfternoon(numberOfTimes, name):
i = 0
while i < numberOfTimes:
print("Good afternoon {}".format(name))
i = i - 1
Where’s the logic error? 🤔
It’s in the comparison i < numberOfTimes
. The problem is that i
will always be lower than numberOfTimes
, because, with each iteration of the loop, i
takes a new value of i - 1
. This means that, with the first iteration, i
will have the value of 0, and then -1, -2, -3, etc.
In other words, it creates an infinite loop. To resolve this bug, you must replace the symbol -
with +
so that the condition i < numberOfTimes
becomes false after the fifth iteration, and the loop stops.
Over to You!
Now you’ve seen these examples, practice by trying to spot the types of mistakes in the code below. It’s a function that displays the message “Good morning Bob” if the user types “1,” or “Good afternoon Bob” if the user types “2.”
sayGoodMorningOrGoodAfternoon(5, "Bob")
def sayGoodMorningOrGoodAfternoon(numberOfTimes, name):
choice = input("Type 1 to say good morning and 2 to say good afternoon")
if choice === "1":
i = 0
while i > numberOfTimes:
print(f"Good morning {name}")
i = i + 1
else if choice == str(2):
for i in range(0, numberOfTimes):
print(f"Good afternoon {name}")
else:
print('I didn't understand your choice')
Once you’ve identified the mistakes, try to make it work by removing the syntax, runtime, and logic errors. You can then compare your code with the correction in the GitHub repository.
Let’s Recap!
Bugs are errors that interrupt a program’s normal functioning or stop it from working completely.
Bugs are inevitable and can even be useful, as they help improve your understanding of programming languages and algorithmic skills.
There are three main types of bugs: syntax errors, runtime errors, and logical errors.
You’ve just learned about the three types of bugs and why there’s no need to be scared of them. In the next chapter, we’ll talk about a bug’s worst enemy—debugging! When you’re ready, join me in the next chapter!