9 ms·
Bah, sprinkling printf statements is a quality debugging technique. Sometimes everything really is a nail.
by lacker 7y ago
Bah, sprinkling printf statements is a quality debugging technique. Sometimes everything really is a nail.
- chabad360 7y agoYou ever seen those bugs, where adding a printf makes the bug disappear?
- bryanlarsen 7y agoHave you ever seen those bugs where attaching a debugger made the bug disappear?
- Enginerrrd 7y agoHeh. Sounds like race conditions. That or your code is possessed, which I've had happen too. My personal favorite is programs that seem non-deterministic. Different errors/results for different executions.
- est31 7y agoIt seems to me that there are two schools of thought: one school which uses debuggers, the other school which uses printf statements and equivalents. Neither schools are absolutely false.
- irrational 7y agoA third school that does both, based on the circumstances.
- kevvok 7y agoExactly! I’ve dealt with multiple real-time embedded platforms that have a log output channel that is waaay more convenient to use via printfs than hooking up a whole debug apparatus.
- ergothus 7y agoI was once told a quote (original unknown, so any butchering is mine) something " when I was a young programmer, I relied on print statements to debug. When I learned more, I used a debugger, and when I learned even more, I used print statements" I always took that to mean dogma was no substitute for results, so I use whichever works better for the problem at hand. (Or at least try to - often this means trying to notice when one way isn't working well and being willing to switch, but that means I was doing the worse choice for a while)
- RoboTeddy 7y agoAgreed — if you're strategic about it, you cut in half the space in which the bug could be hiding each time you add a printf and re-execute.
- matheusmoreira 7y agoWell gdb is pretty hard to use. I learned how to step through programs recently but I'm still not confident using it. The fact that printing variables is still the quickest and easiest way to gain insight into a running program's state speaks volumes. To use gdb, I have to recompile all code with -g, run gdb, set up breakpoints and variable printing, run the program in gdb and then slowly step through it. Alternatively, I can just write this somewhere: fprintf(stderr, "i = %d\n", i);
- astura 7y agoThere's times I use the debugger and times I just print to the console. It really, really depends on the situation. Not to mention setting the logging level to DEBUG is basically the same as just a bunch of printfs and it's many times the best way to debug issues in production. To say printf is not a "quality debugging technique" is just false.
- sam0x17 7y agoThough I've had the super rare occasion where the bug actually IS the code behind the print function, usually in experimental PL/OS stuff. There's a sort of turtles all the way down feeling the first time it happens.
- ThrustVectoring 7y agoGotten bit by that before in Ruby, which attempted to pretty print objects by recursively enumerating their properties and printing them. One mutually recursive data structure later...