How often should I be running code and debugging it verses trying to spot errors before they occur?
So I have a chunk of code that does some stuff, I write it all out, how I think it's going to work, everything compiles, but I hit an error somewhere. I find myself just fixing the error and debugging again (rather quick cycle), but I'm just wondering if I should be spending more time looking at my code without debugging.
I know there has to be a balance somewhere, I want to have a pretty good idea my code is correct before I start debugging, but I feel that debugging helps me identify issues faster, but it feels lazy.
There isn't a solid answer, but I'm just looking for some guidance on "manual review" vs "run until it breaks" so I don't start using the debugger as a crutch. Any thoughts would be appreciated!
Thanks!
Posted Feb 4, 2016, 2:29 AM UTC
Are you testing it out incrementally as you run your code, even if it's just with print-debugging (print the current values of whatever variables are in play to the console)?
This helps you find errors faster.
Similarly, breaking out longer chunks of code into smaller methods lets you test each individual piece.
It doesn't really matter if it's lazy to use a debugger if that's what it takes to solve a problem. Ideally you're catching bugs as they happen and verifying the correctness of your program such that when an error occurs, you know that it's most likely contained in the last few lines you wrote. But this isn't always possible.
Similarly, it's not always the case that the error is easy to find via the debugger.
This may also be a good time to learn about unit-testing, if you're not using that already.
All of the solutions here, though, basically boil down to "Break your code out into smaller parts, then test each of those parts, and also make sure you're checking things as you go along rather than at the end."