loading...

10 steps for debugging your code effectively

BY Shyam Pindoria

{App Developer}

10 December 2018

Reading Time: 5 minutes

How many times have you felt hopeless, looking for a needle in a haystack and it turns out to be right in front of your eyes?

For us, programmers, this happens almost every time we try to debug a problem in our code, after spending hours trying to look for a fix, flipping through documentation, staring at your code hoping that it will confess and it turns out that you had to use “<” instead of “>”.

To help you speed up your debugging process, I have gathered some tips that will help you find these pesky bugs and get rid of them as soon as possible.

Print to the console

Write out meaningful print statements that log the value of a doubtful variable that you want to keep track of. You can only fix a problem faster if you know where the problem lies. Writing print statements will not cost you any time and will instead save you a lot. They also help you find out if a block of code is being executed or not. A simple print “I’m here” will let you know the flow of your code and point out mistakes in your “if” conditions or for-loops.

Read and understand the error message

This is probably the first thing you’d want to do when your program crashes. It is pretty easy to ignore it and act like Sherlock Holmes thinking that you know where the problem is but the truth is that the error message knows more. The console will spit out a stack trace for where the problem could potentially be hiding. Most of the time the first couple of lines of the error message should give you a hint as to what’s causing the trouble. At the very least, it will point out some line numbers where the program stopped, which is a good place to start your hunt.

Google the error message

There is a high chance that you will figure out where the bug is just by reading the error message as they are fairly descriptive. But depending on the type of problem, the error message might not always be accurate or it could be pointing to something that you might not be understanding. In this case, your best bet is to search the description of the error message on Google and you might see a few stackoverflow.com or official documentation results. On this entire planet, there is a high chance that someone has already faced a similar error, asked about it on Q&A sites like Stack Overflow and got an answer. If you are lucky enough, then this might also answer your question. It depends on how specific or generic your error is. The first thing is to read the question and make sure that it is similar to yours. Most important of all make sure it’s the same language, it’s good to include your language in the Google search phrase. If you found the answer that you were looking for, be sure to read a few comments on it or check out other answers and see if it worked for others, then try them out yourself.

Use the debugger

They included this handy tool for a reason and trust me, it does what its name suggests. Most IDEs come equipped with one and it will be helpful if you know how to use it. At the very basic level, it allows your code to run line by line letting you know what’s going on, most programmers completely ignore this tool. Knowing what the problem is, is a different topic, but if you don’t know where the problem is you’ll end up spending hours trying to figure that out. If print statements and error messages don’t give you a clue to where the problem is, 99% of the time the debugger will tell you exactly where it is. It’s very useful for keeping track of all the variables and pointing out that notorious variable that wasn’t supposed to contain “null”. Some IDEs even have the ability to trigger conditional breakpoints which is useful if you want to pause code execution when a certain condition is met and from there on you can look at the contents of all variables.

Create test cases

Only do this if you know what you are doing. Creating test cases is a great way to ensure that your code ends up doing what it was supposed to do. Think of it like writing code that will test some other code that you are working on, ensuring its output matches what’s expected from it.

Check and run your code frequently

Don’t wait to write 100 lines of code and start sweating when you hit the run button as it never usually goes as expected. Be sure to run your code at frequent intervals so that when a problem appears, you know from which point the issue occurred. This will save you quite some time in finding where the issue is.

Go out for a walk

There is no science behind this one. Just take a break away from the keyboard and refresh your mind before hopping back into it. Don’t be afraid that you might lose track of where you left, instead, it might even help you find a different approach. Try thinking about how you would come up with a fix while walking down the garden or while having a shower, your mind tends to work best at those times. You’ll be surprised at how helpful this might be.

Ask for help

Think about the guy who posted that question on Stack Overflow which helped you fix a bug sometime before. What made him ask that question and he might also be lucky that he had his question answered. Again, there is a huge community out there that is waiting for you to ask a question and help you out. So when nothing seems to work, simply post your question and hopefully someone will answer it. This might save you hours or even days. But keep in mind, there are some guidelines in asking an excellent question.

  • Be clear and concise on what you are trying to achieve. Don’t include extra details that are not related to the code as they might confuse others.
  • Copy – paste the code that you think is causing the error. This will let others know what and how you were trying to do something.
  • Include the full error message – even if you didn’t understand it, someone else might.
  • Explain things that you have already tried out and give reasons as to why they didn’t work out for you. This will prevent people from providing answers that you have already tried out.
  • If applicable, show some steps on how to reproduce the problem. This could even lead to a conclusion that it is an issue on the language developer’s end and they might come up with a fix for it.
  • Don’t forget to mention what language you are facing the issue in. Some websites allow you to include tags for your question that help others to easily find it, be sure to utilize them.

Analyze and leave comments

After fixing the issue, go through the steps that you took and keep them somewhere in your mind. If there is no space there, leave a comment on your code, explaining exactly what you did so if the future you faces the same problem, you can always go back and see what you did in the past.

Celebrate!

Don’t celebrate too early though, some fixes lead to even bigger problems. However, after you’ve solved it all and left some comments, give yourself a nice little pat on the back for tackling and solving the issue. You deserve all the credit for saving the day on this one.

Happy coding (or rather debugging)!

Like what you see?
Subscribe now to receive regular updates

MORE ARTICLES LIKE THIS