Debugging Tips for Software Developers

Bakare Eunice
5 min readMar 18, 2020

It’s normal for developers to encounter bugs during development, and it could be frustrating trying to find a fix. It just does not behave the way it’s supposed to. You can spend hours or even days trying to figure out what’s causing the bug. In the end, you find a hack (which is not a real fix but seems to fix the bug), or drop the project(which is not a good solution), or eventually figure out the cause of the bug and fix it (which is best for everyone, yeah!).

Debugging is an art, that involves more than just being able to write code. It means being able to find out the source of a problem, identifying its causes and solving the problem.

How then do you debug your code to find bugs and fix them easily and effectively?

  1. Always reproduce the bug
    Let’s say, for instance, the QA starts testing and there is a crash. The first thing to do is to reproduce that crash. Just follow the same pattern that caused the crash the first time to see if you can get the crash again. This will help you identify the block of code that is causing the crash. Same for any bug that comes up during development.
    Sometimes, what you think is causing the bug, may not be what is causing the bug. So, the first step is to replicate that bug before you start any fix.
  2. Find the specific block of code causing the bug
    Finding the block of code causing the bug will greatly help in finding the fix. Make sure to limit the issue to a specific section of your code as this will help cut down on the time you would have spent in trying to find out where the bug is and will help you to fix your bug effectively.
    Besides, it’s easier to make frequent changes to a single method or line of code.
  3. Check your Stack Trace
    Most times, there will be a stack trace for the bug you are trying to fix. Your job is to check out the stack trace, understand it and figure out the method or line of code that is causing the bug.

If the stack trace is accurate enough to point you to the exact line or block of code causing the bug, check out the first place in your code that the stack trace is pointing you to. This line or block of code will most likely be the cause of the bug or will lead to where the bug is coming from.

This stack trace points to AddEditTaskPresenter class, method createTask and line 142. It’s easy to find to know where to starting looking for the bug from this stack trace.

If there is no line in the stack trace pointing to your code, like this

then you’ll have to figure out the block of code causing the bug. One way to do this is to research your bug using the exception from the stack trace and this leads me to my next point.

4. Gather info on the bug from the internet
Gather as much information as you can on the bug. Copy the part of the stack trace that looks like the most probable cause of the bug and search for it on the Google or Bing or any other search engine.
Other developers would have encountered similar errors so you can learn from their experiences and work with their fixes.
A popular developers community to search for similar bugs is Stack Overflow. One way or another, you’ll find something that will help when you search for the cause of the bug.

5. Use Debugging tools
Debugging tools like Android Debugger for Android or Chrome developer tools for Javascript will help you track where the bug is coming from.
You can easily pause or stop the execution flow using the debugger, and get access to the data in the variables you used in your code.
With the information you get while using the debugger, you can see the step by step execution flow, how data is passed into each variable and where the bug is coming from.
Learning how to use the debugging tools well will help you find bugs faster and easier.

Just kidding, the debugger helps to find bugs and you can fix from there on

6. Writing tests to find your bug
Writing unit tests will also help you in finding bugs. When there are tests for each method in your code, you’ll be able to figure out where the bug is coming from, depending on which test fails. Also, this saves you time from running the code and debugging or clicking around before getting the crash.

7. Clear your head, and involve someone else
Doing this helps while you are debugging. Stop staring at the code trying to figure out when the bug is, stand up and walk around. Take a stroll. Listen to music. Watch a funny video. Get distracted. Change your line of thought. Come back to it later. This often helps because you come back refreshed and you’re more than ready to debug. After doing this, you find out that you debug better this way as you are more relaxed.
Involving someone helps a lot too. Ask someone else to look at it. A colleague, a senior dev, anyone who is into software development. Talk them through what you think the problem is and everything you’ve done to fix it, ask for what they think and listen to them. Sometimes, just talking about it to them can help you figure out how to fix it.
If you’re working alone, ask questions on developer communities, read up on their suggestions and try to understand it, implement these suggestions the right way and who knows, your bug may be fixed there and then.

Lastly, fix one bug at a time, do not try to jump processes while coding and make sure you understand clearly what you’re trying to do. I do hope that after you are done reading this, you can fix that annoying bug and make sure to celebrate it when you do. You deserve it.

--

--