Debugging is an essential skill for all developers by applying a methodology to find and eliminate bugs in a program. The key word is âmethodology.â Debugging isnât something you can make up as you go alongâyou need to be methodical about it and use your detective skills! đ€ This is essential when several different bugs are involved, as is usual when a program crashes. See below for an effective methodology you can use.
This is where you describe the âwhatâ and the âwhenâ of the problem. For example, imagine you have a problem with your car. đ To find out the âwhatâ and âwhen,â you need to ask:
Is the problem coming from the tires (puncture, underinflation, bent rims, etc.) or the engine (flat battery, trouble with the ignition)?
When does the problem occur, and when doesnât it?
Saying âit doesnât workâ isnât enough. Here are some helpful questions to think about:
What exactly is wrong?
Where could the source of the problem be located?
Under what conditions does the bug appear?
Your Python interpreter will also help you by sending an error message containing more detail (weâll return to this in the following chapters). Unfortunately, if your description of the problem isnât clear, your colleagues or Google wonât be able to help you.Â
Here are two examples of requests for help. Which one best describes the âwhatâ of the problem?
âMy program has crashed. Can someone help me? Hereâs the codeâŠâ
âIâm trying to retrieve information from the Twitter API via this address [URL] using the requests module version 2.25.1. Iâm getting the following error message after a few seconds, but I donât understand it. Can someone help me? Hereâs the codeâŠâ
Did you guess right? OK, that was easy, but accurately describing your problem will be a huge help.
One of my former managers used to say, âif itâs not tested, it doesnât work.â Itâs common sense! Think about itâif your program doesnât pass the tests to prove it is functional, how can you be sure it works at all?Â
In a test, you can simulate example scenarios to ensure the program responds to them correctly. Testing is crucial before using the program, as it allows you to identify and anticipate potential bugs.
Testing is also helpful once the program is in operation. Using tests, you can ensure your fixes have solved the problem and not created another one.Â
This is where you need to become a detective and form hypotheses on the source of the bug. This step aims to make the âwhatâ and âwhenâ from the first step clearer by answering âwhy.â This stage is essential to finding the root cause of the bug. You donât just want to deal with the symptomsâyou need to find out why the problem is occurring to stop it from returning. Finally, you must find the real cause to solve the problem once and for all.Â
Going back to the program having trouble retrieving data from the Twitter API, you could ask:
Is the URL correct?
Is the API still working?
Does the most recent update of the requests module mean that it has to be used differently?
Do you definitely have an access token?
These questions should help you find the real cause. You can then test it in the following step.Â
Youâve formulated all the hypotheses you can think of on the bugâs origin. Itâs time to resolve it by implementing a solution which will depend on the type of bug youâre dealing with (as you saw in the previous chapter). For example, you might need to:
Make changes to the behavior of the code;
Refactor the structure of the code;
Update modules or software.
However, just as there are different possible causes for a bug, several possible solutions may exist. Youâll need to implement one or more of these to resolve the bug.
Of course, thereâs a good chance youâll need to go through the four stages several timesâas many as needed for your program to work properly. As you gain experience, youâll get quicker and better at this.Â
You now have a scientific method for resolving bugs. Letâs make it even better by using two very effective problem-solving techniques that apply to ITâand life in general!
The 5 whys method is handy for getting to the root of a problem. As its name suggests, it involves asking âwhyâ five times to analyze the situation in greater depth.Â
For example, a developer might ask, âWhy am I not happy and productive?â. They may come up with the answer that they need to work on a beach rather than in an office. But a more detailed analysis of the situation will probably show that things arenât that simple. To discover the real cause of their unhappiness and unproductiveness, they must ask âwhyâ several times.Â
When you apply the 5 whys method, you get a cause and effect chain that can be linear, tree-shaped, or circular. Here are a few examples:

This is the classic configuration when using the 5 whys method.
Why am I late? Because I didnât get up in time.
Why? Because I went to bed late.
Why? Because I was watching Master Chef.

Each branch corresponds to a cause. The branches should be limited to the most relevant explanations to keep it clear.Â
Why has the car run out of gas?
Branch 1: Because neither my wife nor I filled it up. Why? Because we both thought the other was going to do it.Â
Branch 2: Because the gas station ran out of fuel. Why? Because the truck bringing it had an accident.Â
In this case, the cause and effect chain goes around in a circle. Therefore, to solve the problem, you must break the chain.Â
Why canât I sleep? Because Iâm stressed.
Why? Because I canât sleep.
Why? Because Iâm stressed.
Letâs go back to the Twitter API example and try to answer the 5 whys:
Why did the program crash? Because I donât have an access token.
Why? Because I didnât read the API documentation stating that itâs mandatory.
Why? Because I donât know the technical vocabulary necessary to search the documentation.
Why? Because I havenât taken the time to learn it.
Why? Because Iâm so busy at work that I havenât had a spare moment to do it yet!
What did you notice? You can see several underlying problems, including the ability to read the documentation. Throughout your career, youâll have to read a lot of documentation on using APIs, modules, frameworks, objects, etc., which will be full of technical vocabulary! đ
The developer needs to address all the different causes, especially the underlying problem. Otherwise, this sort of bug will repeat. Think back to the last time a program you were running crashed, and try to use the 5 whys to analyze the situation.Â
The 80-20 principle, or the Pareto principle, explains that 80% of results stem from 20% of causes. This principle is widely referred to because it applies to many different areas.
When applied to resolving bugs, the Pareto principle suggests that 20% of the causes are responsible for 80% of bugs. Therefore, itâs essential to list the potential causes and rank them by how often they appear.Â
You could survey your colleagues and ask, âWhich bugs have you encountered most often, and when?â

You can also look at the most common questions on forums such as Stack Overflow to find out where the most common 80% of bugs originate.Â
If you remember this and focus on the most common and plausible 20% of causes, youâll save time, money, and sanity!
Debugging is a methodology for finding and resolving bugs. Itâs composed of four steps:
Clearly describe the what and when of your bug.Â
Create a test to check whether the program (or part of the program) is working correctly.
Come up with hypotheses on the bugâs origin, remembering that there may be more than one cause.Â
Resolve the bug by focusing on the cause, not just its consequences.Â
The 5 whys method helps you get to the root of the problem by asking âwhyâ several times.
According to the Pareto principle, 80% of bugs result from around 20% of the programmerâs actions.Â
Now youâre equipped with all the key information, letâs get practical and install a debugging environment. Are you ready?Â