Definition
The iterative process of identifying, isolating, and fixing defects (bugs) in a program’s logic or state.
Why It Matters
Debugging is the most frequent activity in software development. Mastering its systematic application reduces the “unknown unknowns” in code, transforming programming from a series of lucky guesses into a rigorous engineering discipline.
Core Concepts
# Assertion (Declarative Debugging)
def calculate_area(width, height):
assert width > 0 and height > 0, "Dimensions must be positive"
return width * height
# Logging (Declarative Debugging)
import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug("Starting calculation")
- Declarative vs. Imperative Debugging:
- Declarative (Logging/Assertions): Leaving “logic tripwires” in the code that trigger automatically.
- Imperative (Print/Debugger): Manually stepping through the code to watch it execute.
- The Debugger (Step Commands):
- Go: Runs until the next breakpoint or the end.
- Step In: Moves into the next function call.
- Step Over: Executes the next line without entering sub-functions.
- Step Out: Finishes the current function and returns to the caller.
- Quit: Terminates the program immediately.
- Breakpoints: A line of code where the debugger is instructed to pause execution.