Debugging While Reading Code
Debugging While Reading Code
Reading code doesn’t have to be a passive activity. Sometimes, the best way to understand code is to watch it run.
The “Active Reading” Method
When you’re confused by a function, you can use print statements or a debugger as “glasses” to see what’s actually happening inside.
Step-by-Step: The “Print Statement” Trail
This is the simplest way to see the code flow.
- Find the entry point of the function you’re curious about.
- Add a
console.log(orprint) at the very beginning. - Add more logs inside
ifstatements or loops. - Run the app and look at the output.
function processData(data) {
console.log("1. Input data:", data); // Is it what I expect?
if (data.isValid) {
console.log("2. Data is valid, continuing...");
// ... logic ...
} else {
console.log("2. Data is INVALID, stopping.");
return;
}
console.log("3. Final step reached.");
}
Step-by-Step: Using a Debugger (The Pro Way)
A debugger lets you pause time and look around.
- Set a breakpoint: Click the margin next to a line of code in your IDE. A red dot should appear.
- Run in Debug Mode: Instead of
npm start, use your IDE’s “Run and Debug” button. - Interact with the App: Trigger the code you’re watching (e.g., click a button).
- Watch the Magic: The code will pause at your red dot.
- Inspect Variables: Look at the “Variables” panel to see what everything equals right now.
- Step Through: Use the “Step Into” (go inside a function) or “Step Over” (go to the next line) buttons.
Practical Strategy: The “Why am I here?” Log
If you find yourself in a function but don’t know how you got there, check the Call Stack in your debugger. It’s a list of every function that was called to lead to the current one.
Call Stack:
- processOrder (line 50)
- handleCheckout (line 120)
- onButtonClick (line 15) <-- The origin
Mental Model: The X-Ray
If reading code is like looking at a building from the outside, using a debugger is like having X-ray vision. You can see the electricity flowing through the walls and the water moving through the pipes in real-time.
Common Mistake: “The Log Pollution”
Always remember to delete your console.log statements before you commit your code! You don’t want to clutter the terminal for your teammates.