7 ms·
The fact that you _know_ where or which variable it is, and know where to put the print in order to see the value indicates that the bug is either trivial, or t
by chii 6y ago
The fact that you _know_ where or which variable it is, and know where to put the print in order to see the value indicates that the bug is either trivial, or that you already have an idea of where the bug is!
I argue that printf debugging is more difficult if you dont know _what_ is incorrect, or if you dont know where a value is being changed. Stepping through your code is a great way to find out.
To me, actual stepped debugging is like having a print statement for every single line in the code base.
- falsaberN1 6y agoYou aren't wrong, but you do need a debugger to be available, and it's not the case with every language, unfortunately.
- stinos 6y agoI argue that printf debugging is more difficult if you dont know _what_ is incorrect Or even impossible? I use print debugging often, not even only in cases where there's no step debugging possible and/or I'm too lazy to fire up a config/software which allows debugging, but from time to time I encounter bugs which I really can't even begin to imagine how terribly awkward it would be without step debugging. For example: finding a bug in a mark&sweep garbage collector written in C which only occurred in certain LTO builds. To get a picture of the problem I stepped through assembly code while looking at certain memory locations, regsiters, the C stack, and some related variables. How would one tackle that with print debugging? Plus the print statements would affect the very thing being debugged? That should be fun. Not to mention the effort it took to even be able to reach the exact code location where all of this happened. Would almost require to randomly sprinkle print statements all over the place.
- misthop 6y agoIf you have _no idea_ where a bug could come from in your code You should spend the time understanding your codebase before you even begin debugging. At the very least you need to be able to reproduce your bug to reliably fix it. How can you have no idea where a bug is happening if you can trigger the error in a code path? EDIT: I don't want to go so far as to say that your example can never happen, but I can't believe there is any domain where it is the norm
- Jtsummers 6y agoEver inherited a 500k+ SLOC program with piss poor documentation that you can't rewrite (at least not within the needed timeframe)? Or worked in an office that, on some projects if not overall, experienced high turnover (due to promotions, internal movement, quitting, etc.)? It's very easy to end up with a project in a state where no one actually knows most of the code. A debugger shines here. In fact, the debugger can be a fantastic way to learn the code since the dynamic behavior of a system can often be hard to infer from its static representation (looking at you WPF and XAML, or any project where architecture astronauts went hog wild with certain design patterns).
- misthop 6y agoChanging code that you don't understand because you "found" the bug with a step through debugger is a recipe for disaster. In those situations (high turnover/fast moving tech debt accumulation) perhaps no one cares that the code base is becoming unmaintainable, but it isn't a good practice.
- Jtsummers 6y ago> Changing code that you don't understand because you "found" the bug with a step through debugger is a recipe for disaster. I don't get your point, because that's not what I suggested someone should do. I think (on rereading my original comment) my point was the exact opposite: Develop an understanding of the code through the effective use of a debugger: > In fact, the debugger can be a fantastic way to learn the code since the dynamic behavior of a system can often be hard to infer from its static representation (looking at you WPF and XAML, or any project where architecture astronauts went hog wild with certain design patterns). You seem to have inferred something from my comment that I did not say.
- misthop 6y agoIf I did I apologize. I agree that using a debugger can be a good way to understand what a codebase is doing. I think we are on a similar page. Sorry for the miscommunication
- gus_massa 6y agoJust add more print instructions. And if that doesn't work, add even more print instructions! Add some instructions to print "**1**", "**2**", "**3**", ... to see how far the program goes, which branches it takes, print a few variables that look interesting. If that fails print the uninteresting variables, remove "**1**" and add some "**2a**", "**2b**", ... in between 2 and 3 ... After a while, you discover the correct spot and the correct variable, and the error is trivial.
- rgoulter 6y agoI do this too; but it seems to me this is the case where using a debugging tool would probably save more time. Looking at the effort involved: here you have to spend the time to add a bunch of print statements throughout bits of code; then compile/run the program (repeating until you've found the right place). With a debugger: you'd have to take the time to set up the environment and learn how to use it (e.g. set breakpoints in parts where things would be interesting). But then the "look at interesting values" wherever isn't significant additional effort.
- scoutt 6y agoI am sorry, but this approach seems terrible. It seems the thing I do when I have to debug something I don't know, like PHP ("echo hello1", "echo hello2"). And that is just because my field is far from web development (I do embedded C), and I might be forced to do it like once every 5 years or so. I don't know if you program for a living, but I would be kind-of ashamed if I see a colleague debugging like this. It seems a desperate approach.
- danaris 6y agoThat's quite a judgement when you have no idea what kinds of tools the language the person you're responding to is using has. Frankly, even when I'm using a language that has a stellar GUI debugger, there are times when print debugging is vastly more effective. Why? Because I have a loop that iterates thousands of times, and crashes on some indeterminate but high iteration. A simple line-based breakpoint requires me to manually continue the code thousands of times, then take note of what state the variables were in the time just before it crashes. Even the ability to set a more complex variable-value-based breakpoint doesn't help that much when I don't know what the values are where it breaks. Printing out the loop variables every time lets me just check the last printout, and say, "Ah, it's when it hits i=37129", then follow that to the array value it corresponds to, with a substring in it that just happens to trip an edge case of one of the calculations. Or whatever.