Codementor Events

How to fix a bug?

Published Sep 30, 2018

I went through four years of engineering in computer science, I have done numerous technical courses online and I have read loads of blogs. All of these tend to teach me programming, algorithms, data structures or some other technical concept. One thing that I have never been taught is how to fix a bug. Apart from the course I did numerous internships and I am working as a member of technical staff at VMware, I have realised that most of the work in industry is actually fixing bugs. Not only in industry even if you are developing your own pet project you are bound to encounter bugs and you will need to fix them. I believe this is one of the most important aspect of programming and yet no one teaches us how to fix bugs.

Fixing bugs is not an easy task. It requires a lot of patience and time to find out the source of the error in thousands and thousands of lines of code. It is one of the major things that discourages newbies when they start programming. Addressing this problem I am going to talk about some tips and steps that can be followed in going about fixing bugs.

One of the important skill that we need to pick up when learning to code is to read and understand error messages. These are the first indications that something is wrong and error messages are usually very precise giving us the exact reason and the line number causing the errors. However I have seen people usually ignore these messages and not understanding what these messages are trying to convey to us. Googling the error message often gives us plausible causes that might have caused the error and helps us in better understanding the problem. Over a period of time we get used to these messages and we know exactly what is wrong when we see a particular kind of error. Just like an experienced mechanic who can tell the problem from the sound of your car engine.

8bT1Y.pngSimple error message

If the error is not simple the next step is to understand the code at the point of error. This requires the next essential skill set, that is, reading code. 99% of the time when working on large project we read code written by other people. It is an important skill to read code and understand exactly what it is trying to do. The understanding should be very precise and we should be able to trace every step that the code is performing. I suggest read the entire function that is causing the error and try to figure out every step and every operation it is performing. This will take up a lot of time. Again this is a skill that is picked up with practise and you become faster at it over the years.

As you read the code keeping track of all the variables usually helps in understanding. This is where we need a debugger of some kind. If not available we fall back to print messages in code (this is a very primitive way of doing it and is not advised). There are very powerful debuggers available for most of the languages used. These debuggers can help us in keeping track of the variables in every step of the program and understanding how they are changed and follow the control of execution. This in itself can become a full fledged course. In brief we use breakpoints to stop code where we want to and can observe the values of variables at runtime. We can also proceed step by step through the code observing the changes in variable values.

maxresdefault (1).jpgGDB – Debugger for C programs

Summarising the step to be followed are:

  1. Read and interpret the error message.
  2. Google the error for better understanding and possible causes.
  3. Understand the code around the error message.
  4. Try to create a pseudo code with a record of all the variables.
  5. Use a debugger to see how the variables are changing in runtime

The above steps are used to solve bugs that occur during development and you have a clear compile time or runtime errors. More complex bugs are those that occur after the software are released and are encountered by customers. These don’t have any clear error message and in some cases it is not even possible to recreate the bugs. These are the bugs that are solved by trained experts. A few common steps that can be followed are:

  1. Recreate the problem consistently.
  2. Check logs and error messages if any.
  3. In case some error message or warning gets logged it directs you to the part of code that might be causing the problem.
  4. Even if there are no warning or errors reading the log messages printed around the time of error usually gives us an idea what the system is running or trying to do.
  5. The next step involves finding and reading the code that is supposed to be doing the operation having bugs.
  6. Special attention need to paid to every condition and branches.
  7. Debugger and other similar tools helps in determining the path of execution being taken and the value the data structures are holding at and around the time of error.

It should be noted that fixing bug requires a lot of patience and clear thinking , computers are very dumb and they do exactly as they are instructed by us. It is a skill that is developed by practise and one becomes better at it over the years. The solution to these bugs are not very complex but to get to the root cause requires deep understanding of the system and the language being used.

This is just an introduction to solving bugs, next time I will pickup bugs of various difficulty levels and try to demonstrate how can they be solved. 

Discover and read more posts from Siddhant Gupta
get started
post commentsBe the first to share your opinion
Show more replies