Debugging Techniques

What Is Debugging?

Debugging is the process of finding and fixing errors in a program.

It ensures that code runs as expected by identifying logical, syntax, or runtime errors.

Common Debugging Techniques

Technique Description Example Use
Trace Tables Manually tracking variable values at each step. Ensuring a loop updates values correctly.
Breakpoint Debugging Pausing execution at specific points to inspect program state. Stopping before a conditional statement to check logic.
Print Statements Printing variable values and flow information. Using print() in Python or System.out.println() in Java.
Step-by-Step Execution Executing code line by line to analyze behavior. Using an IDE debugger.

Example: Using Print to Debug a Simple Loop

Although the example is trivial, there may be times when variables don't always have the 'state' you expect. Sometimes, a simple print statement can help, by actually showing the state of a given variable. This gives you, the developer, a chance to cross-reference the actual behaviour with the intended behaviour.

# Debugging with print statements
numbers = [1, 2, 3, 4, 5]
total = 0

for num in numbers:
    # Track current value being added to 'total'
    print("Adding:", num)
    total += num

print("Final Total:", total)
public class DebugExample {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        int total = 0;

        for (int num : numbers) {
            System.out.println("Adding: " + num);
            total += num;
        }

        System.out.println("Final Total: " + total);
    }
}

Example: Using Breakpoints to Debug a Simple Loop

Even in a simple loop, the program’s internal state may not match your expectations. By setting a breakpoint on a critical line, such as the total += num statement, you pause execution just before it runs. This lets you inspect variables like total and num in your IDE’s debugger without cluttering your code with extra print calls. You can then step through each iteration, verify the actual state against the intended logic, and quickly identify where things go awry.

def sum_list(numbers):
  total = 0
  for num in numbers:
    total += num # desired breakpoint
  return total

print(sum_list([1,2,3,4,5]))
  1. Execution stops before the loop starts.
  2. Inspect numbers.
  3. Step over to see num = 1, total = 1.
  4. Repeat until you verify the final result.

Set-Up a Breakpoint in VS Code

  • Open the file you want to debug in the editor.
  • Click in the left gutter beside the line number to add a red breakpoint dot.
  • Hover over the red dot and right-click to configure conditions or hit counts if needed.
  • Start the debugger with F5 (or the green “Run ▶ Debug” button) to pause at your breakpoint.

Set-Up a Breakpoint in PyCharm / Eclipse

  • Navigate to the line where you want execution to stop.
  • Double-click the left margin (PyCharm) or left gutter (Eclipse) next to the line number to toggle a breakpoint.
  • Right-click the breakpoint icon to add conditions, log messages, or suspend policies.
  • Run your application in Debug mode (Shift+F9 in PyCharm, F11 in Eclipse) to hit the breakpoint.

Set-Up a Breakpoint in IntelliJ / NetBeans

  • Place the caret on the line you wish to break at.
  • Click the gutter (IntelliJ) or press Ctrl+F8 (NetBeans) to add a breakpoint marker.
  • Use the breakpoint’s context menu to enable/disable, add conditions, or set hit counts.
  • Launch the debugger (Shift+F9 in IntelliJ, Ctrl+F5 in NetBeans) and execution will pause at your configured point.

Example: Step-by-Step Execution

Step-by-step execution (sometimes called “single-stepping”) lets you walk through your code one line at a time in the debugger. At each line you can:

  • See exactly which branch you’re in (e.g. inside an if or else).

  • Watch variable values change as each statement runs.

  • Observe when and how often loops iterate.


Step-by-Step Execution in VS Code

  • Click the gutter to set a breakpoint on the desired line.
  • Start debugging with the “Run ▶ Debug” command or press F5.
  • Use F10 (Step Over) to execute the next line without entering functions.
  • Use F11 (Step Into) to dive into function calls, and Shift+F11 (Step Out) to exit.
  • Watch the Variables and Call Stack panels to observe state changes.

Step-by-Step Execution in PyCharm / Eclipse

  • Toggle a breakpoint by clicking the left margin next to a line number.
  • Click the Debug icon (🐞) or press Shift+F9 (PyCharm) / F11 (Eclipse).
  • Use F8 (Step Over) to move to the next line in the same method.
  • Use F7 (Step Into) to enter methods, and Shift+F8 (Step Out) to return.
  • Inspect variables in the Debug tool window to verify values at each step.

Step-by-Step Execution in IntelliJ / NetBeans

  • Click in the gutter to add or remove a breakpoint.
  • Launch the debugger via the Debug icon or press Shift+F9 (IntelliJ) / Ctrl+F5 (NetBeans).
  • Use F8 (Step Over) and F7 (Step Into) to navigate line by line.
  • Use Shift+F8 to Step Out of the current method.
  • Monitor the Variables (Watches) panel to see how each statement alters program state.

Challenges in Debugging

  • Identifying the Root Cause: Some errors are difficult to trace.
  • Complex Program Flow: Large programs require structured debugging strategies.
  • Intermittent Bugs: Some issues appear only under specific conditions.

 Key Takeaways

  • Debugging is essential for ensuring programs run correctly.
  • Common techniques include trace tables, breakpoints, print statements, and step-by-step execution.
  • IDE debuggers provide efficient ways to inspect variables and pause execution.