How To Use A Debugger
Imagine this: you add some code to a project, thinking that it all looks good. Then you run the program, and it blows up somewhere in the middle. Reading through the code, it’s not immediately understandable why the code you added doesn’t work–so what do you do? It’s time to debug! Debugging is the practice of finding and resolving errors in code (“bugs”), and knowing how to use a debugger is an important skill for every developer.
Debuggers are tools that allow developers to examine code execution as it happens. A debugger allows you to step through the code one instruction at a time, so that you can examine variables and track the program flow through each step. A debugger allows you to execute your code step-by-step. By making the execution of a program observable and providing ways to examine and change variable values during execution, debuggers help developers identify discrepancies between what they expect to happen when code is executed, and what is actually happening.
If you’re using an IDE, there is probably already a debugger built into the application. Most programming languages offer built-in debugging capabilities or support third-party debugger tools; you can find specific instructions for setting up a debugger on the official website for your language or IDE. The examples for this article are from Visual Studio 2022, running on MacOS 12.6.3. Once you have a debugger installed and ready to go, here’s how to use it effectively:
Step One: Set breakpoints
A “breakpoint” is a line of code where you want the debugger to pause execution. Since a debugger allows you to look at code step-by-step, executing one line at a time, you probably don’t want to start debugging at the very first line of code in your program–otherwise you’ll likely be clicking the “execute next line” button for a very, very long time before you actually get to the part of your code that’s buggy.
Instead, try to identify one or two points in your code where you suspect the bug might exist. Set breakpoints at those locations to pause execution, and you can examine all the variables in your program as they exist specifically at that line of code. You can always add more breakpoints if you need them later.
To set a breakpoint in Visual Studio, click once in the narrow gray-ish sidebar to the left of the line numbers in a code file. You’ll see a red dot appear in the sidebar, and the line of code will be highlighted. This signifies that a breakpoint has been added on that line.
Step Two: Run the debugger
Run the debugger, and let the program execute until it reaches the first breakpoint. In Visual Studio and most other IDEs, this is as easy as setting the run configuration to “debug” and clicking a button. Follow the instructions for your specific language and software setup.
Step Three: Examine the Application State
When the debugger pauses execution of the program, you can inspect the values of the variables the program is using at that moment in time. Most debuggers provide a dedicated window or console for variable inspection. In the example below, which is paused on a method defined in a class, you can see the properties of the current object (represented by ‘this’), and the ‘profitPercentage’ parameter passed into the method.
Step Four: Step Through the Code
Use the debugger’s step commands (usually ‘step into’, ‘step over’, and ‘step out’) to navigate through the code line-by-line, observing how variable values change and affect the program’s flow.
- Step Into: this tells the debugger to move to the next line of code and execute it. It will also enter any function or method called on that line–if the current line contains a method call, the debugger will hop ‘into’ the method and pause execution at the first line of the method. ‘Step into’ is useful when you want to go into the details of a method and track a variable through the method’s execution. This can help you understand how the method operates and allow you to identify any issues with its implementation.
- Step Over: this command tells the debugger to execute the current line of code without entering any functions or methods called on that line. If the current line contains a method call, the debugger will execute the entire function without pausing, and proceed to the next line in the current scope (usually, the next line of the code in the file you are already in). The ‘step over’ command is handy when you’re confident that the method called is working correctly and you don’t need to analyze it in detail, and only want to focus on the broader context of the code in question.
- Step Out: the “step out” command tells the debugger to continue execution with pausing until the current function or method completes and returns to where it was called. Once the current method is done executing, the debugger will pause at the line immediately following the method call. In other words, if you set a breakpoint in the middle of a method, ‘step out’ will finish running the method and return to the line of code directly after where the method was called.
By using these three debugger commands, you can navigate through your code and do a thorough analysis of how it’s working. It’s important to note that the availability and syntax of these commands may vary depending on the debugger you are using. Most popular debuggers provide dedicated buttons or keyboard shortcuts for these commands.
You will often have “stop” and “continue” commands available to you as well. Using the “continue” command will resume code execution without pausing until another breakpoint is reached; the “stop” command will stop code execution and end the debugging session.
Step Five: Fix the Code
When you identify a problem with your code, fix it! Many debuggers will allow you to modify variable values on the fly during debugging, which allows you to try out different scenarios and see the impact of your changes in ‘real time’. You can also stop the debugging session before making changes. Once you identify a bug, apply the necessary fixes to your code, and run the debugger again to step through the code and make sure that everything is working correctly.
If the bug persists, or you accidentally cause a new bug, repeat the steps above, adjusting breakpoints and examining the code until all the bugs are resolved. You can also use print statements and logging alongside the debugger to gain more insight into your program’s behavior.
Conclusion
Using a debugger is an essential skill for developers–by understanding the fundamental concepts and mastering the techniques of using a debugger, you can significantly reduce the amount of time you spend fixing bugs. By leveraging the power of debugging tools, developers can pinpoint the root causes of errors and bugs, test different scenarios, and make the necessary code fixes relatively quickly. With consistent practice, you’ll enhance your debugging skills in no time, be well on your way to becoming a master bug-fixer!