6
I was at a library coding workshop in Phoenix and a kid showed me a better way to debug.
I was stuck on a loop that wouldn't end for like 45 minutes. This 12-year-old next to me leaned over and said, 'Why don't you just print the counter each time?' I felt like an idiot. I was trying to trace it in my head instead of just making the program tell me what was happening. Now I use print statements for everything. What's your go-to trick when your code just won't work?
3 comments
Log in to join the discussion
Log In3 Comments
ryan6531mo ago
Eric_johnson I get what you're saying about breakpoints being like magic but print statements are just WAY more practical for how I code. Most of my projects aren't massive enterprise stuff where I need to walk through every single variable. They're small scripts or tools I'm hacking together. Setting up a breakpoint takes me way longer than just typing console.log('here') or print('value is ', x). Plus when you're working with API calls or database queries the breakpoints can actually time out while you're poking around. I'd rather see a messy stream of prints I can delete in two seconds than fight with a debugger that's pausing everything. Different tools for different jobs I guess.
4
eric_johnson2mo ago
Oh man, that's a classic (and honestly, print statements are still the best debugger). But careful with using them for everything though. For bigger projects, you can end up with a mess of prints you have to clean up. I usually lean on the actual debugger in my editor now. You can set a breakpoint and walk through the code line by line, seeing every variable. It feels like magic the first time you use it.
3
wyattbennett2mo ago
Ever try debugging async code with a debugger? I always hit weird timing issues where the breakpoints mess up the flow. Print statements feel like the only thing that actually shows what's happening in real time.
8