Resolving a bug always follows the same steps. In this chapter, we’re going to detail these one by one.
1. Identifying the problem
Generated messages in running code can contain important information about the location of the problem. Since error handling in .NET is based on exceptions, it's important to supply the necessary analytical information after the event. For example, if a received parameter has an unsupported value in an ArgumentException, the value and parameter name must be provided.
2. Reproduce the problem in a controlled environment
3. Debug the code
Now you need to go into the code and execute it step-by-step to understand what is not going as planned. The idea is to form a theory which you can then validate. Once you identify the data set that allows the bug to be reproduced, you can launch an investigation.
It’s important to understand that it would be possible to stuff the code full of messages indicating what point you’re at, the values of the parameters passed to the methods, and the status of the data structures. But this is unrealistic for several reasons:
It would affect the application's performance too greatly. This is why certain trace functions are only run in debug mode.
How do you determine the quantity and quality of the traces required?
It’s hard to know in advance what you should investigate. If you trace everything, it would generate so much noise that it would be impossible to find the necessary relevant information.
This is why we need a debugger. The idea is to gradually reduce the search area within the code to get to the faulty instructions and data structures.
In the following sections of the course, you’ll learn how to navigate through the code execution and see the data structure status.
4. Fixing the cause
The fourth step is to find a solution to the bug! To do this, you’ll need to:
Modify the instructions,
or modify the data structures.
5. Test and validate the correction
You must always test and verify that the correction solves the problem, and confirming that it doesn’t cause issues elsewhere (another added value of unit tests!).
6. And start again if the symptoms have not gone away
And if that’s not enough… well, you start all over again from the beginning.
That was the only theory part of the course! 😉
Now it’s time to get your hands dirty. We're going to solve a few bugs together throughout this course. Let’s get going!