Code Debugging Techniques – A Practical Approach

0
1080

When it comes to development, learning how to debug an application is a key element that separates the great developers from average programmers. In this guide I’m going to walk through popular code debugging techniques. And then discuss how you can use them in your applications.

Why is Debugging Important?

So why exactly is it important to learn code debugging techniques? There are a number of reasons, however one of the main ones is that debugging teaches you how to work through problems in a structured manner.

code debugging techniques

You can look at debugging as a way of becoming the code version of Sherlock Holmes. Debugging forces you to:

  • Analyze a situation.
  • Learn everything you can about the data and app behavior.
  • Implement fixes based on the information that you learn.

Modern Debugging

Years ago debugging code projects was a manual process. Typically you’d be forced to simply print out data to the console and then comb through the terminal to see the output.

However, with the growth of modern programming has come a nice selection of code debugging techniques that you can take advantage of.

Code Debugging Techniques

There are a number of ways that you can debug an application. We’re going to walk through tools that I use for Ruby programming projects. However the principles that I’m going to discuss can be applied to every modern language. The only difference will be the debugging library names.

Pry Debugger

code debugging techniques

First and foremost is Pry. The Pry Debugger gem is my favorite debugging tool. Once you install this gem, you can access the binding.pry feature. Simply add binding.pry into a line of your source code. Then, whenever it executes the line, Pry will open up and inspect the program while pausing the script. This makes it easy to find issues quickly.

Pry also comes with others features, including syntax highlighting, code indentation, and more. If you look over a senior Ruby developer’s shoulder, you’re most likely to see them using Pry on a regular basis.

Better Errors

code debugging techniques

Next on the list is the Better Errors gem. While the Ruby on Rails framework has an error page in place by default, that page is limited in what it has to offer. If you add the Better Errors gem you’ll be able to debug your program each time you encounter an error.

Better Errors inspects all of the source code for all stack frames and displays the full stack trace of the error. It also makes both local and instance variable data available for inspection, as well as a live REPL on each of the stack frames. A REPL means that you can dynamically interact with the code and test it out. All of these tools make it easy to find the errors in your code. And more importantly, how to fix them.

Byebug

code debugging techniques

Last on the list of Gems that I use is the Byebug gem. Byebug is full of features that will help you debug your code. Most notably it includes stepping, which allows you to run a program one line at a time. The breaking feature allows you to pause and examine a current line of code or feature. While the tracking option keeps track of different variables and lines. It also has REPL functionality much like Better Errors.

IDE

code debugging techniques

Last on this list of code debugging techniques is the development environment itself. Depending on what IDE you’re using, it may also ship with built in debugging tools. I’ll typically use Sublime Text, which doesn’t come with debugging functionality. However if you use RubyMine you will have a full suite of ways to debug an application. This may be a good option if you came from using IDEs such as Eclipse.

Summary

I hope that this has been a helpful guide on code debugging techniques. If you’re wanting to take your development skills to the next level, I highly recommend you practicing with these tools. After you’re comfortable with using them in sample applications you’ll be able to start implementing them in your real world projects.

Show Notes

LEAVE A REPLY

Please enter your comment!
Please enter your name here