25
Just realized a 5-year-old showed me the best way to debug code
My neighbor's kid was watching me stare at a broken loop for 20 minutes. He said 'why don't you just check what it's doing step by step?' So I added 3 print statements inside the loop and found the bug in 2 minutes. Has anyone else had to unlearn bad habits from starting with complex tools too soon?
3 comments
Log in to join the discussion
Log In3 Comments
ray13614d ago
Disagree with that approach for anything beyond basic scripts. Adding print statements works for small stuff but real debugging needs proper tools like breakpoints and watches. Print debugging teaches bad habits because you clutter the code and miss edge cases the print statements don't cover. A kid's advice works for a simple loop but fails on multithreaded code or production systems where you can't just add random prints.
10
olivia39814d ago
Respectfully, @ray136, I see this one differently. Print statements are a tool like any other, and calling them bad habits is selling them short. When you're neck deep in some gnarly multithreaded code, a breakpoint can crash the whole thing or make timing issues disappear. But a well placed print that logs to a file? That's gold because it shows you exactly what happened in the real run, not just when you paused it. I've fixed plenty of heisenbugs this way where the breakpoint itself changed the behavior. Sure, cluttering code with prints is dumb if you leave them in, but that's a cleanup problem, not a debugging problem. For production stuff especially, your logger is your best friend.
3
murray.drew14d ago
Wait, MULTITHREADED? You're debugging race conditions with PRINT statements? That sounds NUTS to me, honest. Print statements in async code can totally mess with timing and make the bug disappear or shift around which is the exact opposite of helpful. I've seen that happen on a project where a print statement fixed a deadlock because it slowed down the thread just enough. Sending logs to a file is different though, I can see that working for real. But the way you were talking about print statements themselves it sounded like you were still coding and printing to standard output. That's a whole different beast from logging, especially in production with a proper logger. I just can't wrap my head around using raw prints to track down a heisenbug in parallel code without the prints causing the exact problem you're trying to fix.
2