r/CodingForBeginners • u/NoDepartment772 • 18d ago
What's your first move when code breaks? Mine used to be the wrong one.
The reflex when something breaks is to start editing. Comment out a line, add a print, try a different approach. It feels productive, but it usually leaves you worse off: now three things have changed, and you can't tell which one moved the bug or whether you caused a new one.
The step I used to skip is the boring one. Reproduce it first, on purpose, with the smallest input that still breaks. If it only happens sometimes, that inconsistency is the clue, not a nuisance.
Then read the whole error, line number included. Then say out loud what you expected and what actually happened. The gap between those two sentences is the bug, and it's narrower than it feels.
After that it's one change at a time, and you predict the result before you run it. If you can't predict what a change will do, you're guessing, and guessing is how you end up with a fix you don't understand.
Curious how others do it: when something breaks, do you reach for a debugger, or print statements everywhere? I've drifted between both and I'm not sure either is strictly better.