The Theory of Debugging
Tags: java, developer, development, tutorial
Published: 2023-07-18 17:30:00

In the landscape of software development, bugs are an inevitable part of the journey, and debugging, albeit frustrating at times, is an integral part of the process. There's no escaping this truth, and the sooner we embrace it, the sooner we can master the art of debugging.

In the next few posts in this series, I will explain the little-known “theory” behind debugging. We all know the practice of debugging (to some degree) but there is also a theoretical underpinning that most of us never learned in University (I sure as hell didn’t). Understanding this theory will help you apply a more methodical approach to problem resolution and will improve your understanding of your code.

Before we proceed I’d like to mention that most of the content in this series is covered in my book [“Practical Debugging at Scale: Cloud Native Debugging in Kubernetes and Production” (Apress)](https://www.amazon.com/Practical-Debugging-Scale-Kubernetes-Production/dp/1484290410/). 

Also while we’re on the subject of books, my new book “[Java 8 to 21: Explore and work with the cutting-edge features of Java 21](https://www.amazon.com/Java-21-Explore-cutting-edge-features/dp/9355513925/)” (BPB) is the #1 new release in Java Programming on Amazon and available now.

{% embed https://www.youtube.com/watch?v=sJkPV7njpVY %}

## The Simplicity and Complexity of Bugs

Debugging is a labyrinthine journey, often reminiscent of Alice in Wonderland. It calls for acute observation, insatiable curiosity, calculated experimentation, and a sense of adventure. However, the general sentiment towards debugging is one of antagonism, largely because of the frustration it entails and the uncomfortable truths it uncovers. 

The reality is, most bugs are embarrassingly simple in retrospect. When we finally pinpoint the issue, the common response is a groan of disbelief — "How did I miss that?" While this reaction is natural, it breeds a sense of shame and inadequacy, often leading to impostor syndrome. Despite my 40 years of programming experience, I can confidently say that the bugs I encounter today are just as "stupid" as they were at the start. This constant humbling feeling, akin to a universal debugging facepalm, keeps me grounded.

The emotions experienced during debugging – surprise, frustration, and humility – serve as a reminder of our fallibility. It’s akin to a form of meditation, keeping egos in check. Perhaps some leaders could even benefit from debugging as a method of grounding, bringing them closer to the realities of their tasks and teams.

An important principle I have when debugging is to “start with stupid”. I look for the dumbest mistake I can think of and in a surprising number of cases, it’s indeed the bug. This isn’t a part of the theory

## Embracing the Debugging Methodology

The first step in tracking a bug is identifying the likely area in the code. This involves searching through documentation and conducting basic research. From there, we need to devise a strategy to tackle the bug. This step is often overlooked in our haste to find a solution, leading to unstructured and disorganized approaches. We need to formulate a plan, make assumptions, and then test these assumptions. 

Next, we should isolate the behavior causing the issue and aim to reproduce it consistently for testing. This could ideally be done in a local environment within the debugger. If we can’t consistently reproduce a bug, we won't be able to truly verify our fix, adding uncertainty to the process.

## Validation and Elimination

Following this, we must validate that the results of our tests and environment align with our initial assumptions. In the spirit of robustness, it’s advisable to have two forms of verification as one could potentially be flawed. I wrote about the importance of double verification in [this post](https://debugagent.com/building-for-failure-best-practices-for-easy-production-debugging).

Once we've made these verifications, we proceed to the elimination stage, taking inspiration from Arthur Conan Doyle's famous quote:

> "Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth."

In other words, we need to "Sherlock Holmes" our problem and rule out possibilities until we're left with the most plausible explanation.

With a deeper understanding of the bug, we can move on to resolving the issue. The resolution process should include filing the issue, creating a failing test case, verifying the proposed fix resolves the test case, and committing both the bug and fix.

## Reading the Docs: A Misconception

It's often said, “5 hours of debugging can save you 5 minutes of reading the docs.” However, this saying is misleading. Reading the documentation is not the answer, especially considering the sheer volume of documents associated with APIs, platforms, systems, and more. Documentation is never read in five minutes and rarely memorized to a level that will solve a bug. In all the decades I’ve been a developer I solved bugs by searching through the docs, but never by reading the docs in advance.

The key here is to know what, where, and when to search for the problem. Search engines and platforms like Stack Overflow have revolutionized debugging, enabling us to input error messages directly and find potential solutions. This method is not foolproof, but it's a good starting point. 

## The Importance of a Game Plan

Having a game plan saves us from being swallowed by the abyss of trial-and-error debugging. Many years ago, I lost two workdays due to a misplaced 'greater-than' character because I didn't use a methodical approach. 

Before diving into a debugging process, it's crucial to answer questions like:

* Can the user reproduce this?
    
* Can I reproduce this on my machine?
    
* Does the issue happen consistently?
    
* Is the issue a regression?
    

The answers to these questions will shape your game plan and the course of your debugging process. In the end, patience and strategy can save precious time and prevent unnecessary frustration.

In our next installment, we'll explore gameplans for debugging issues that can't be reproduced. Stay tuned and embrace the debugging adventure!

## Final Word

Debugging, despite being seen as frustrating, is an essential part of software development, offering moments of learning and personal growth. A methodical approach to debugging involves identifying the area of code responsible for the bug, formulating a strategic game plan, isolating and reproducing the bug for testing, and finally resolving the issue. A common misconception is that reading the documentation can save hours of debugging; however, it's more about knowing where and what to search for. Patience and a clear strategy can prevent unnecessary time waste and make the debugging process more efficient.

In the next installment, we will delve deeper into the game plan for debugging, particularly focusing on issues that are hard to reproduce. We will further explore strategies and tools that can assist in efficiently tackling such elusive bugs.