Debugging Skill
Objectives
Provide systematic methodology for debugging code:
- •Identify bugs efficiently
- •Find root causes
- •Fix without side effects
- •Prevent regression
Debugging Strategies
1. Binary Search Debugging
When bug location is unknown:
- •Identify range where bug exists
- •Test midpoint
- •Narrow range based on result
- •Repeat until isolated
2. Trace-Based Debugging
For flow-related bugs:
- •Add logging at key points
- •Trace execution path
- •Compare expected vs actual flow
- •Identify divergence point
3. State Inspection
For data-related bugs:
- •Capture state at key points
- •Compare with expected state
- •Identify corruption point
- •Trace data source
4. Regression Testing
When something "used to work":
- •Identify when it broke (git bisect)
- •Compare working vs broken code
- •Isolate the change
- •Understand why change broke it
Bug Categories
Logic Errors
- •Off-by-one errors
- •Incorrect conditionals
- •Wrong operator precedence
- •Missing edge cases
Data Errors
- •Null/undefined references
- •Type mismatches
- •Invalid state transitions
- •Race conditions
Integration Errors
- •API contract violations
- •Protocol mismatches
- •Version incompatibilities
- •Configuration issues
Performance Bugs
- •Memory leaks
- •Inefficient algorithms
- •Blocking operations
- •Resource exhaustion
Debugging Tools Usage
Logging
python
# Strategic logging
logger.debug(f"Function entry: {func.__name__}, args={args}")
logger.debug(f"State: {vars(self)}")
logger.debug(f"Exit with result: {result}")
Assertions
python
# Catch invalid states early assert data is not None, "Data should not be None at this point" assert len(items) > 0, "Items list should not be empty"
Breakpoints
- •Set at suspected locations
- •Conditional breakpoints for specific cases
- •Watch expressions for key variables
Common Patterns
The Scientific Method
- •Observe: Gather data about the bug
- •Hypothesize: Form theory about cause
- •Predict: What would prove/disprove hypothesis
- •Test: Execute debugging action
- •Analyze: Evaluate results
- •Iterate: Refine hypothesis
Rubber Duck Debugging
- •Explain the code line by line
- •Describe expected behavior
- •Describe actual behavior
- •Identify discrepancy
Output Specification
Debugging reports should include:
- •Clear problem statement
- •Steps taken to investigate
- •Findings and evidence
- •Root cause identification
- •Proposed fix
- •Verification approach