If you’ve been churning away hours of code only to realize that a few pesky bugs have snuck their way into the software you’re writing, don’t worry. We spoke with a couple of programming experts to help us create a guide that offers advice that any programmer can use to quell those bugs in any programming language.
First…What’s the Root of the Issue?
Before rolling up your sleeves, take a step back and think about what might be at the root of the issue. “After waiting a moment, check any relevant logs which may hold clues to what the problem is. Depending on what’s found there may be a simple solution. If not, it’s time to debug,” says Tim Henrich, founder of Turret.IO.
Most of the time, bugs (or errors) usually fall under one of three major categories:
Logic errors are the trickiest because, quite simply, there is a problem with what your program is trying to do even though it is syntactically correct. “These [logic bugs] are hard to find because they usually aren’t errors you can look into from the compiler or interpreter,” says Jameson Quave, president of JQ Software and author of Developing iOS 8 Apps in Swift.
Logic Errors: Set Breakpoints and Inspect Values
“Whether you’re using a debugger, like GDB from the command-line, or using an IDE, like IntelliJ, setting breakpoints and inspecting the values of any stateful variables is your first step,” Quave says.
He says it’s extremely helpful to familiarize yourself with your debugging tool and then see how other values change as a result. Jorge Colon, senior software architect and owner of 2U Media, points out that any popular IDE for each language has an interactive debugger. “For instance, if you store the value 9.99 as a price, and then upon inspection, you see that its value is–instead–9, you might conclude that you accidentally stored the value in an integer variable at some point along the way.”
Always write a failing test for broken code for future reference.
Use an automated test to help you easily figure out when the problem was resolved. “So, in the future, you can prevent regressions by running the test again after any changes to the code,” he says.
Runtime Errors: Dig into the Error Code
Getting to the solution for runtime errors is a little more straightforward. “You can usually get enough info on the problem by simply reading the error message, or look into the error code,” Quave says.
Even Quave, a veteran programmer, also suggests going the good ol’ fashion Google route. “Searching for an error message is a pretty quick way to either locate others who have faced the same issue, or documentation describing when and where that message would be displayed,” Quave says.
Stackoverflow is a popular favorite for solutions as well. For more program-specific questions, check out smaller communities, like this Swift forum. Here’s one on Salesforce coding as well.
For Exception Runtime Errors, Consider it a Tap on the Shoulder from a Developer
“Exceptions are raised intentionally by platform developers, usually in anticipation of their APIs being used incorrectly in one way or another,” he says. Platform developers often anticipate common issues and incorporate exceptions in order to help you redirect you to the right path and change what you’re doing.
As a simple example, Quave says, “if I wrote a game engine that I know couldn’t handle JPG files as efficiently as PNG files, I might raise an exception when encountering JPGs, in an attempt to persuade developers to convert to PNG.”
For Compile-Time Errors, Modern Tools Will Tell You the Problem
Compiler errors are similar to that of exceptions runtime errors, but they tend to be a little more abstract. “Learning how to address compiler errors is all about knowing the language well, and identifying what particular line is causing problems,” Quave says.
Whether it’s a syntax error or you accidentally declared multiple objects in the same name, fortunately, most modern tools will tell you exactly what line of code is the problem during the compile phase.
These are just a few of the basic guiding principles that are crucial in solving some of the most common bug errors. So, the next time your hours of hard work come to a grinding halt, we hope these tips can help save you some valuable time.