News for hackers. Stuff that matters.
Re:The unreasonable effectiveness of print debugging by CJefferson on Saturday April 24, 2021 @11:56AM (#26925861)
Personally, I think my biggest reason for using print debugging is.. it works.

In C++ I often find the debugger doesn't find symbols on projects built with configure/Make. If I have a Java Gradle project I have no idea how to get it into a debugger. Python debuggers always seem fragile. Rust requires I install and use a "rust-gdb" script -- except on my current machine that doesn't work and I don't know why.

I'm sure in each case I'm sure I could get a debugger working given enough time, but the only error I've ever had in print debugging is failing to flush output before a crash, and it's never been hard to search for "how to flush output" in whatever language I'm current using.

Re:The unreasonable effectiveness of print debugging by zamfi on Saturday April 24, 2021 @02:58PM (#26927401)
Print debugging always works, but also: it lets the programmer customize their view of the program’s (very, very large) hidden state in any way imaginable. Step debuggers are the “no-code” equivalent: extremely useful for the purposes for which they were designed—and often the better choice there—but inherently limited.

Geoff’s not wrong in invoking Bret Victor’s Learnable Programming argument that being able to track state over time is critical to debugging, and Geoff’s right that print debugging makes this easier than almost any existing step-debugger.

Bret’s deeper point, though, is that a major challenge in debugging is hidden state in general, and that variable state changing over time is just one example.

Not only is there a ton of hidden state in the execution of a program—the full execution trace PLUS state of every variable at each point in that trace—but there is also a ton of interpretation of that state that the programmer needs to do while debugging: “what does this sequence of events imply?” - “why is this pointer pointing here?” - etc.

Doing that interpretation is much easier when the programmer gets to selectively view that (again, HUGE amount of) hidden state. Print debugging gives the programmer complete control over what state is shown. No other debugger does that: they all show a ton of data and context (often useful!) and make certain operations easy (inspecting single variables! viewing call stack snapshot!), and these are often just the right things.

But sometimes they’re not. And often, when you start debugging, you don’t know if the fancy debuggers will be too much or not enough.

Print debugging gives you the power to write code to selectively view the (again, HUGE!) hidden state of your program, and this scales from the smallest code-tracing bug to the largest distributed systems.

Step debuggers, on the other hand, are essentially “no-code” debuggers — extremely useful for the purpose for which they are designed, still useful for adjacent purposes, and a great place to start if you know the tool well, but ultimately not as powerful if your needs exceed their capacities.

A good programmer will know how to use all these tools for what they’re best at!

Re:The unreasonable effectiveness of print debugging by vvanders on Saturday April 24, 2021 @03:02PM (#26927434)
If you've chased heap corruption printf doesn't really help you much but a data breakpoint is a godsend.

Same thing with watch windows, memory views and the like. There are classes of problems that do well with printf but calling them "no-code" is vastly underselling them.

Re:The unreasonable effectiveness of print debugging by zamfi on Saturday April 24, 2021 @03:22PM (#26927572)
I’m not sure when no-code became a pejorative, but that wasn’t my intent! Only that most of these tools, unlike print, are special-purpose, exceptional for that purpose, and often even useful in other circumstances.

A data breakpoint is a great example of something useful that print doesn’t do well.

Re:The unreasonable effectiveness of print debugging by vvanders on Saturday April 24, 2021 @03:29PM (#26927628)
You seemed to be drawing a parallel between no-code -> "not as powerful", in my experience they're different tools for different use cases.

I also don't think they're nearly as no-code as you call out. VS' watch window has very few limitations compared to printf back when I was working on win32 things.

Also important to consider iteration time. I once worked on a system where adding a printf was a 20 minute process due to the need to heavily optimize for a memory constrained platform(scripting fit in 400kb block with the asset bake step).

Re:The unreasonable effectiveness of print debugging by zamfi on Saturday April 24, 2021 @03:40PM (#26927704)
> in my experience they're different tools for different use cases

Exactly!

Debuggers are very useful tools, and typically not as general-purpose as print. I don’t view “not as powerful” as a meaningful distinction, because it requires that you ask “powerful at what?” ---

VS’ watch window is great but (I assume) doesn’t work across distributed systems, etc. — as a general technique, print is universal in the sense that there are very few problems that can’t be diagnosed by modifying your code and printing some (possibly a manipulation!) of the hidden state. This is going to be harder than using a special-purpose tool designed for exactly your problem.

In the same way, “no-code” tools are typically better and/or easier than writing code to solve the same problem, but special-purpose.

Re:The unreasonable effectiveness of print debugging by vvanders on Saturday April 24, 2021 @04:48PM (#26928166)
> typically not as general-purpose as print

In my domain which doesn't usually cover distributed systems printf can be worse because it introduces synchronization primitives that have caused race conditions to disappear(and that race condition causes second order heap corruption or the like). On one platform system memory was so small(8mb total) that each output to stdout went over the serial link slowing performance down to 1/20th of a realtime process under any real logging.

Like I said, different tools for different uses, and really depends on the context. If there was one size fits all then we'd just use that but the diversity of debugging tools I think shows that you need a variety of techniques to approach the problems we encounter.

Re:The unreasonable effectiveness of print debugging by zamfi on Saturday April 24, 2021 @05:07PM (#26928265)
> Like I said, different tools for different uses, and really depends on the context

We totally agree, and I'm not sure what we're arguing about--perhaps you can fill me in.

I'm arguing that print is almost always worse than any specialized tool. (After all, who would use a specialized tool worse than print?) There is not a one-size-fits-all tool, and print is not a one-size-fits-all tool.

Indeed almost every seasoned developer has a story about print failing. Whether it's the mysterious "Heisenbug" that disappears when you measure it (like the sync issues you mention) -- my personal story is when I was trying to debug a (class project) kernel scheduler. Printing to the console was so slow that by the time I'd printed anything, the scheduler had moved on to the next time slice!

It's worth nothing that "print debugging" is not literally just using the "print" function; it's a style of debugging that involves logging specific information using some logging function (usually, but not always, print) and then analyzing it after the fact (usually, but not always, by reading the printed output).

This strategy of "get data out, then analyze it" is the general form of print debugging, and in the small-memory case, or the sync Heisenbug case, this often means collecting data in the appropriate variables before outputting it to be visible. Isn't this still print debugging, even though it doesn't use a "print" function?

Re:The unreasonable effectiveness of print debugging by vvanders on Saturday April 24, 2021 @06:26PM (#26928720)
I think we're mostly arguing about how useful the various approaches are. At least for me print debugging is a measure of last resort unless I want to extract some historical data out and I know it won't influence the timing of the issue I'm trying to chase down.

With print debugging your inserting the whole build + deploy + repro setup loop into your debugging, if that's a long time(say 20 minutes in one job I had with production hardware) you're in for a world of pain. I find that just about any other tool usually is an order of magnitude more efficient.

Also even the "step debugger" tools do the same thing you'd do with a print. LLVM for instance uses the IR JIT API to generate watch/eval values: https://releases.llvm.org/9.0.0/docs/ORCv2.html#use-cases

IMO you should relentlessly optimize your iteration times, that's the inner loop of development speed and print debugging fares pretty poorly in that area for all the reasons above.

Re:The unreasonable effectiveness of print debugging by zamfi on Saturday April 24, 2021 @07:04PM (#26928961)
> I think we're mostly arguing about how useful the various approaches are.

Ah, that's fair.

> At least for me print debugging is a measure of last resort

Right, and I think this depends on the domain. For lots of mature environments, this makes sense -- there's been years for tooling to catch up to the kinds of bugs people run into, possibly corporate money being put into developing debugging tools, etc.

> IMO you should relentlessly optimize your iteration times, that's the inner loop of development speed and[...]

Agreed, though the effect on print debugging on iteration time is very environment-dependent.

> [...]print debugging fares pretty poorly in that area for all the reasons above

Adding console.log to a web app can be a trivial change (though of course reproducing app state is another issue) -- again very environment-dependent.

Re:The unreasonable effectiveness of print debugging by jayd16 on Saturday April 24, 2021 @05:19PM (#26928337)
This is a lot of words but I wonder if you've ever worked with a debugger with watchable variables or immediate mode code execution. I find it odd you say print debugging is more flexible.
Re:The unreasonable effectiveness of print debugging by zamfi on Saturday April 24, 2021 @07:09PM (#26928997)
I have. What information exactly can you get from a debugger with watchable variables and immediate mode code execution, that you can't get from print?

I'm not making the argument that people shouldn't use debuggers -- obviously if there's a good one that does what you need, it'd be silly not to use it. And good debuggers are great.

But what happens when you're working on a distributed system? Or multiple processes on a single system? Or...etc. etc. -- debuggers are almost always built to support a particular set of use cases for a particular set of domains. Step outside, and they're not usually as useful.

print always works. It's almost never the best. It's more flexible because it can almost always be used.

Re:The unreasonable effectiveness of print debugging by Too on Sunday April 25, 2021 @01:10AM (#26930464)
Silly example i made up on the spot, but not too far from real life. I'm iterating a list of 10000+ objects with about 20 or more nested properties. Some times one of them behave strange. This function runs several times per second.

Option1: Print all of them, requires rebuild, would log 200000 lines every second. Unless i wrap the print inside conditions, requiring yet another rebuild.

Option2: Conditional breakpoint: user[i].department.balance <= expected_value. Bang, i can now inspect both the complete nested user-object, and the previous/next item in the list, other local and global variables, the call stack of how it reached there, the state of all other threads in this moment, and so on. With the really good debuggers like .NET or JVM I'm even able to rewind the execution pointer to the start of the function or hot-swap the code as it's running.

In short, a debugger allows you to see all state of the program at once, and you can retroactivelly choose what is relevant. As opposed to printf where you must select upfront. Maybe even more importantly it's lacking stack traces, unless you add a log and request-id at every function-enter/exit.

There is also the case of third party code which you can't edit to add printf to. Many environments usually by default give you pretty good context of the symbol names or even complete source code which you can step through (except c++ land where you'd need a debug-build of the lib, which isn't impossible either).

Re:The unreasonable effectiveness of print debugging by Igelau on Saturday April 24, 2021 @09:49PM (#26929691)
> What information exactly can you get from a debugger with watchable variables and immediate mode code execution, that you can't get from print?

If you have some completely undocumented object oriented code with tons of relationships, peeling the state apart in a debugger is a good way to get an idea of what you even have that might be worth printing.

Re:The unreasonable effectiveness of print debugging by jayd16 on Sunday April 25, 2021 @03:13PM (#26935640)
>What information exactly can you get from a debugger with watchable variables and immediate mode code execution, that you can't get from print?

Lots of things but an obvious example is private member info.

Re:The unreasonable effectiveness of print debugging by gerdesj on Saturday April 24, 2021 @06:47PM (#26928876)
Using a debugger is largely passive - it shows you what is actually happening.

Debugging via print allows you to step outside and peer in ie it is active.

Print debugging can be prone to bugs within itself which may cause additional ignorance about the potential bugs being diagnosed. How meta can you get? 8) There's also the effect of the effort of actually looking - that may or may not have an effect.

Anyway, the discussion here is largely ignorant of language and function. At the moment I spend time fiddling up Python and OpenSCAD scripts if I dig out a programming language. For me, print is really handy. For a Linux low level latency sensitive driver in highly optimised ASM n C I suspect this matter is moot.

Re:The unreasonable effectiveness of print debugging by icedchai on Saturday April 24, 2021 @08:46PM (#26929444)
With interactive debuggers like python's pdb you can actively manipulate data, print it out, basically do whatever you need to do in a REPL. It is way more effective than adding print statements, rerunning your script, etc. That being said I typically use a combination of both: I use a print to get me to where I think the problem begins, then an interactive debugger (pdb.set_trace / breakpoint / etc) to drill down into the details.
Re:The unreasonable effectiveness of print debugging by Groxx on Saturday April 24, 2021 @07:01PM (#26928944)
(most) debuggers can print - I often use conditional breakpoints with the "condition" "print(thing)". It works great, doesn't require re-compiling, can be enabled/disabled with a single click, etc. It's handy when you want to see a lengthy sequence all at once.
Re:The unreasonable effectiveness of print debugging by crancher on Monday April 26, 2021 @12:33AM (#26939260)
Discovering this capability changed how I debug. Conditional breakpoints that log-only creating an always useful, easily enabled/disabled log of critical method results without littering the code itself with logging statements.
Re:The unreasonable effectiveness of print debugging by nyczomg on Saturday April 24, 2021 @09:32PM (#26929609)
"Print debugging always works..."

Nope, print debugging does not always work. Please stop saying that. Sure, it's often useful and often works. But go try to debug a race condition, or bug in some locking mechanism with print statements. I'll wait as you add a bunch of print statements, and then learn the harsh lesson that you are now debugging a completely different program with very different performance characteristics that now doesn't deadlock where it used to deadlock because you slowed down one of the threads massively as it spends time it used to not spend logging stuff.

"Step debuggers, on the other hand, are essentially “no-code” debuggers — extremely useful for the purpose for which they are designed, still useful for adjacent purposes, and a great place to start if you know the tool well, but ultimately not as powerful if your needs exceed their capacities."

I guess you'd think this if you thought print debugging "always works". But I promise you it is equally (if not easier) to exceed the capabilities of print statements in some scenarios.

Re:The unreasonable effectiveness of print debugging by ruslan on Saturday April 24, 2021 @10:22PM (#26929849)
You don't put printf() in critical sections, instead you collect some statistics. Once critical section is over you can freely print collected data for further analyzis. Or you can print it once a second. This simple technique works well for debugging racing conditions, performance issues (profiling), memory leaks, kernel drivers and bare-metal stuff where timing is a concern. So, yes "Print debugging always works...", but must be used wisely. :-)
Re:The unreasonable effectiveness of print debugging by Shikadi on Sunday April 25, 2021 @01:58AM (#26930605)
Collecting statistics can change race conditions too, it's just less likely
Re:The unreasonable effectiveness of print debugging by bluGill on Saturday April 24, 2021 @09:45PM (#26929666)
Debuggers don't work in the situation you described either.
Re:The unreasonable effectiveness of print debugging by Taniwha on Saturday April 24, 2021 @09:57PM (#26929728)
Yes, my reaction entirely, I do a lot of real-time work, an interactive debugger is quite useless when stopping everything is not an option
Re:The unreasonable effectiveness of print debugging by nyczomg on Saturday April 24, 2021 @10:04PM (#26929766)
Where did I say that they did work?
Re:The unreasonable effectiveness of print debugging by tomcam on Saturday April 24, 2021 @06:37PM (#26928800)
> it lets the programmer customize their view of the program’s (very, very large) hidden state in any way imaginable.

I’ve been trying to articulate this to myself for a long time. Thank you.

Re:The unreasonable effectiveness of print debugging by lelanthran on Sunday April 25, 2021 @05:44AM (#26931504)
> A good programmer will know how to use all these tools for what they’re best at!

Yeah, I run through valgrind; 9 times out of 10 I don't even get to the point of starting the debugger or recompiling with prints. The remaining 1 time where valgrind output is clean, I go with printing. About half the time the prints aren't enough and then I fire up the debugger.

Also, it depends. I'm working on a legacy C# project where the previous dev read somewhere that passwords shouldn't be stored in plain-text, so when changing the stored DB credentials you have to set a breakpoint where the hash is calculated, change the variable holding the cleartext password to the new password you want to use, step the line that calculates the new hash, copy the text out of the watch window and finally paste that text into the file holding the credentials.

I do not know how this helps. I also don't care enough to write a c/line utility that generates the hash in a base-64 string, becasue we've changed the DB credentials for the webapp only once since he left. We may have changed it more often if we had an easy way to do so :-/

Re:The unreasonable effectiveness of print debugging by greggman3 on Saturday April 24, 2021 @08:25PM (#26929346)
That suggests that debuggers should let you write visualizers and they should be so easy to write that you don't hesitate to write them.

That seems to be the idea behind Glamorous Toolkit https://gtoolkit.com/

I'd be curious what it would take to add any of those concepts to existing debuggers for other languages.

Re:The unreasonable effectiveness of print debugging by bakatubas on Saturday April 24, 2021 @12:19PM (#26926043)
As a counter-point, I think there’s an argument that folks don’t spend enough time in the debugger. But there’s a lot of value there and in fact one could use a debugger environment to unit test as even native debuggers have scripting environments.

Personally, I think folks should master the debugger _first_ and during all steps learning a programming language.

But similar to test-driven-development it’s a different way of thinking, and most books scarcely discuss the debuggers.

That being said, I do use print-debugging a lot too—in C++ a lot of functionality can be compiled-out, allowing one to, for instance, print hex dumps of serialized data going to the network.

On that note, there is a distinction between trace debugging that is part of the source code and general print statements that are hacked in and removed.

Re:The unreasonable effectiveness of print debugging by throwaway894345 on Saturday April 24, 2021 @12:36PM (#26926188)
I can believe there is value in learning a debugger, but debuggers could stand to improve significantly. Debugger UX is almost universally awful and per the parent it’s often difficult to get one up and running. Moreover, if you do your “print debugging” with log statements of the appropriate level, they can be useful in production which is perhaps the biggest value.
Re:The unreasonable effectiveness of print debugging by corty on Saturday April 24, 2021 @01:42PM (#26926770)
Also, in almost all languages debuggers are an afterthought. Take e.g. the situation with Golang, Haskell or Python. Either there is no useful debugger or there is one, but it came late and still cannot debug everything the language does.
Re:The unreasonable effectiveness of print debugging by hawkice on Saturday April 24, 2021 @01:54PM (#26926877)
Print debugging not (really) working is haskell is... Non-idea, and a bad pairing for bad real debugging. But test cases are usually easier to figure out. Presumably there's a balance discovered by people in big projects but it never seemed as good as normal approaches to me.
Re:The unreasonable effectiveness of print debugging by corty on Saturday April 24, 2021 @02:18PM (#26927098)
Haskell debugging by testing is great for small functions where you can use quickcheck. But larger tests for the more complicated stuff don't work in quickcheck and there isn't much else that one can easily do.
Re:The unreasonable effectiveness of print debugging by Quekid5 on Saturday April 24, 2021 @03:36PM (#26927672)
Not sure what you mean, there's e.g. Tasty for non-QC testing. It can do all sorts of variations of test, e.g. traditional unit tests, "golden" tests, etc.
Re:The unreasonable effectiveness of print debugging by slaymaker1907 on Saturday April 24, 2021 @03:34PM (#26927659)
I haven't actually used quickcheck in Haskell, but I've used it for very complicated tests in other languages including Racket, TypeScript, Rust, and Java. The nicest thing about quickcheck is that it lets you easily create test data without imposing too many constraints on it. Regular fuzzing or randomized testing is almost as good, but the narrowing done by quickcheck is sometimes nice for understanding test failures.
Re:The unreasonable effectiveness of print debugging by kaba0 on Saturday April 24, 2021 @06:15PM (#26928638)
On the other hand, it is reall great in case of the JVM
Re:The unreasonable effectiveness of print debugging by Too on Sunday April 25, 2021 @01:35AM (#26930546)
Not sure what situation you are talking about. Debugging Python is as easy as right-clicking a file in Pycharm and pressing debug. Why care if it was an afterthought when it for the past decade has worked perfectly.
Re:The unreasonable effectiveness of print debugging by corty on Sunday April 25, 2021 @08:23AM (#26932181)
I care. That it has worked for the last decade only means that Python was without a working debugger for 2/3 of its existence. 1/3 of which I had to suffer from it.

Also, pycharm isn't really what I would call a proper debugger yet, attaching remote running processes for example just doesn't work reliably yet and is very new anyways. Debugging embedded targets just doesn't work. Multithreading is iffy (but that's unfortunately normal in Python).

Re:The unreasonable effectiveness of print debugging by throwaway894345 on Sunday April 25, 2021 @11:37AM (#26933539)
Also needing to use a particular text editor to use a decent debugger is bananas.
Re:The unreasonable effectiveness of print debugging by foobarian on Saturday April 24, 2021 @02:25PM (#26927153)
The problem is that it's not just a matter of "learning the debugger for Java." In practice there are many different projects that configure debugging many different ways, and it doesn't matter that you know which keys to press in IntelliJ if it will take you an hour to figure out how to attach it to the project. This speaks to OP's point, where it's hard to use a real debugger to casually investigate random projects.

Having said that, it is absolutely a requirement when working on a project for any length of time (especially professionally) to set up and figure out a debugging environment, because it is significantly more productive than printing. But the startup cost is certainly there.

Re:The unreasonable effectiveness of print debugging by zmmmmm on Saturday April 24, 2021 @05:39PM (#26928422)
The java case is actually pretty universal ... you run the JVM with debugging enabled (fixed string of flags) and then tell your IDE to attach to the JVM on the port you gave it. You don't need compileable source, can be on a remote server, different OS etc - if you have just the source for the bit you want to debug you can set a breakpoint in it and it'll stop there.

Being able to debug third party code in remote / hostile environments (even when its mixed with proprietary vendor code) is one of the things I like about Java.

Re:The unreasonable effectiveness of print debugging by foobarian on Saturday April 24, 2021 @10:42PM (#26929941)
The Java case is arguably the least difficult out there thanks to reasons you outline. But still, the other day I had to debug a Gradle plugin written in Java. It's possible! But it took an hour or so of effort to figure out which options to use and which program to give them to.
Re:The unreasonable effectiveness of print debugging by eitland on Saturday April 24, 2021 @03:28PM (#26927610)
> The problem is that it's not just a matter of "learning the debugger for Java."

In the Java case, for stanalone projects (i.e. not something deployed on a server) an if it is your own project and you don't do anything unreasonable it is mostly just set a breakpoint and hit "run with debugging".

Probably the least painful debugging experience I know.

Doing it for Tomcat/Tomee was slightly more advanced IMO but still utterly trivial compared to wrangling css or js ;-)

There are reasons why we "old folks" like Java so much despite its verboseness.

Re:The unreasonable effectiveness of print debugging by klyrs on Saturday April 24, 2021 @01:20PM (#26926586)
In a world with perfect optimizing compilers that never introduce bugs, we should never "need" print debugging. But that's not where I live, so I'll keep using print debugging.

On the other hand... adding print statements can also invalidate certain optimizations (an excellent source of heisenbugs), so I'll never stop using debuggers either

Re:The unreasonable effectiveness of print debugging by toast0 on Saturday April 24, 2021 @02:35PM (#26927231)
Print debugging is essential in distributed systems. Sitting in the debugger waiting for human input often leads to timeouts and not covering the case you want. Of course, sometimes adding the prints, or even just collecting values to be printed later also changes execution flow, but like do the best you can.
Re:The unreasonable effectiveness of print debugging by bakatubas on Sunday April 25, 2021 @09:08AM (#26932404)
All the more reason for folks to spend more time using the debugger; for instance, break points are only one feature.

Setting up remote debugging I’ll agree is more difficult than a local application, but each remote machine can automatically run startup commands and not require user input; commands can be run at particular places too (to print output etc) with conditional trace points, all while not impacting the code itself.

Main point is that folks don’t spend enough time learning the debugger, as print statements are easier. But using the debugger is a better practice in my opinion in the case where print statements are added just for a quick test, then removed.

Re:The unreasonable effectiveness of print debugging by 1337shadow on Saturday April 24, 2021 @12:16PM (#26926023)
What Python debuggers you are talking about? have you tried the built-in CLI debugger? Just drop breakpoint() in your code and you're in. Have been using it daily for over a decade and really happy with it - it's actually one of my favorite features of the language, amongst the many super useful features that Python and its excellent stdlib have to offer.
Re:The unreasonable effectiveness of print debugging by CJefferson on Saturday April 24, 2021 @12:33PM (#26926165)
Honestly, I didn't know that and I'll try it. Last time I had to debug I remember adding "-m pdb" (as at the start of https://docs.python.org/3/library/pdb.html , first result in Google), but for some reason that immediately threw an error instead of starting the program, so I just chucked some prints in instead.
Re:The unreasonable effectiveness of print debugging by montecarl on Saturday April 24, 2021 @12:42PM (#26926230)
I have mapped a key to insert "import pdb;pdb.set_trace()" in my editor. Also use it daily, and not just for debugging. It is useful when working on a new project and you just want to interrogate some object you got back from a library to see what valid operations you can do with it. Or to double check some math operations.
Re:The unreasonable effectiveness of print debugging by 1337shadow on Saturday April 24, 2021 @12:55PM (#26926353)
I just tried python -m pdb and it works for me https://dpaste.org/9EQo#L26 but I really always use breakpoint(). You can even configure it to use other debuggers with an environment variable, ie.: PYTHONBREAKPOINT=ipdb.set_trace
Re:The unreasonable effectiveness of print debugging by CJefferson on Saturday April 24, 2021 @12:59PM (#26926393)
I just quickly went any checked the program I was trying to debug. I was running 'python3 package --arguments', where 'package' is the name of a directory which contains a package I was working on. 'python -m pdb package --arguments' just complains that 'package' is a directory.

Adding a 'breakpoint()' at the start of the program does get me into the debugger. I'll remember that for future (but, it's not easy to find by googling if you don't already know what you are looking for!)

Re:The unreasonable effectiveness of print debugging by fake-name on Saturday April 24, 2021 @10:10PM (#26929794)
For complex problems, `import pdb; pdb.set_trace()` instead of a print statement can be super handy. It basically launches the debugger from the context of wherever you stuck the line.

For large unwieldy data-structures, you can go ipython: `import IPython; IPython.embed()` launches the ipython REPL from the calling line's context.

I use the latter a fair bit when spelunking around in other people's code. `pdb.set_trace()` lets you continue execution more easily.

Re:The unreasonable effectiveness of print debugging by kbd on Saturday April 24, 2021 @01:15PM (#26926551)
Never felt that Python debugging was "fragile". BTW if you're not using pudb you're missing out.
Re:The unreasonable effectiveness of print debugging by zmmmmm on Saturday April 24, 2021 @05:41PM (#26928427)
Only thing I hate about this .... regular point of code review: remove the debugger breakpoint you left in your code!

We haven't had one hit production yet, but it came close. Print statement is a lot more harmless.

Re:The unreasonable effectiveness of print debugging by rufus_foreman on Saturday April 24, 2021 @01:48PM (#26926816)
>> If I have a Java Gradle project I have no idea how to get it into a debugger

You download Intellij IDEA, run it, choose File->Open and select the build.gradle file, right click the main class and there's a Debug option.

Re:The unreasonable effectiveness of print debugging by pugworthy on Saturday April 24, 2021 @12:07PM (#26925957)
I agree with this sentiment. Print debugging is essentially universal. The same goes for compiler directives or simple constant driven conditionals to toggle it (in a brute force way) on and off.
Re:The unreasonable effectiveness of print debugging by m463 on Sunday April 25, 2021 @12:02AM (#26930267)
> Personally, I think my biggest reason for using print debugging is.. it works.

I agree.

A similar statement is that using a debugger often does not work.

the sort of ratholes I've run into are: - debug build - proper symbols - interrupts and debugger - kernel and debugger - unfamiliarity with debugger - limitations of debugger

Without a proper debug build, you can't run the debugger effectively. You have to set up a whole debug environment.

Sometimes you need to do broad work to get proper symbols and stack trace information. By broad, I mean a debug build for everything.

I've also found many debug builds, apart from altering some behavior, they also turn on a lot of printfs.

If you're using interrupts/timers or debugging a kernel module, many times debuggers don't work or may alter, move or supress the problem.

and then there's the... I haven't used a debugger in 6 months, how do I do (very simple thing). And sometimes the debugger is just not the right tool or a tedious tool to use. "If I just load this one macro and somehow get the right address maybe I can decode this one kernel data structure..."

Personally, many times an ephereral printf isn't crude and meaningless, it's a precise scalpel getting to the core of the problem.

Re:The unreasonable effectiveness of print debugging by bc4m on Sunday April 25, 2021 @01:11AM (#26930473)
This is the only argument for print debugging I can understand. Given the debugger experience I've had at every job in the last 10 years (IDE-driven, set breakpoints in editor, inspect state visually) it's mind-boggling that anyone thinks print statements are superior, but getting that experience can be frustrating depending on your tools and environment.

FWIW my experience debugging Python in VS Code required no setup and I've encountered zero issues.

Re:The unreasonable effectiveness of print debugging by wmichelin on Saturday April 24, 2021 @01:37PM (#26926729)
Python easily has the best debugger I've ever seen in a language.

``` import pdb; pdb.set_trace() ```

that's literally all you have to do at any point in your code. Run your code in the foreground of a terminal, and boom you have a debugger exactly where you want it.

Re:The unreasonable effectiveness of print debugging by gmfawcett on Saturday April 24, 2021 @02:49PM (#26927340)
In recent Python, it's even easier -- just put `breakpoint()` in your code.
Re:The unreasonable effectiveness of print debugging by anarazel on Saturday April 24, 2021 @06:03PM (#26928543)
Fwiw, it's not too hard to approximate that in c/c++. Print out the pid (I often print out "gdb -p %d") and then sleep (and perhaps send SIGSTOP to other processes in more complicated scenarios).
Re:The unreasonable effectiveness of print debugging by twobitshifter on Saturday April 24, 2021 @10:55PM (#26930003)
Pry with Ruby allows you to drop in to a repl anywhere with binding.pry

You can edit the state and keep running after altering the program.

But usually I just end up printing a few things from the repl and figuring out what is fouled up.

Re:The unreasonable effectiveness of print debugging by fighterpilot on Saturday April 24, 2021 @01:52PM (#26926862)
Part of the reason it works is it forces you to reason about the code, otherwise you won't know where to put the print statements.
Re:The unreasonable effectiveness of print debugging by christophilus on Saturday April 24, 2021 @02:05PM (#26926965)
Agreed. Also certain classes of bugs (such as bugs in parallelism) are easier to debug via print than using a debugger.
Re:The unreasonable effectiveness of print debugging by tgsovlerkhgsel on Saturday April 24, 2021 @06:29PM (#26928741)
Especially when you need to run your code on a remote server as part of a bigger platform like some Cloud or Serverless system.

These systems likely already have a way to get logs, but good luck getting a debugger to work there.

Re:The unreasonable effectiveness of print debugging by le-mark on Saturday April 24, 2021 @12:04PM (#26925928)
I’m sorry but this is just ignorance, learning to use the debugger for the platform at hand is a basic skill every developer should master. So many times have seen developers use the debugger to troubleshoot and fixed issues and been perceived as a “ninja” (despise that term but that was the effect) because they knew how to use the debugger. I mean yeah keep printing lines, and keep being out performed by your debugger using peers. That’s the choice.

Yes I am dieing on this hill.

Re:The unreasonable effectiveness of print debugging by axaxs on Saturday April 24, 2021 @12:09PM (#26925970)
OK. And I've also cringed watching ninjas step through code slowly, reading everything, spending 20 mins catching something 2 print statements would have achieved.

Debuggers aren't bad. But neither is printing. Knowing when to reach for them is probably a bit more key.

Re:The unreasonable effectiveness of print debugging by dragonwriter on Saturday April 24, 2021 @01:12PM (#26926526)
> And I've also cringed watching ninjas step through code slowly, reading everything, spending 20 mins catching something 2 print statements would have achieved.

The problem is that the two print statements will only catch the bug if they are the right two, based on a correct hypothesis of what the bug is. Which, with a debugger, won’t require stepping, but setting two breakpoints, doing a run-to-breakpoint, and inspecting values.

Stepping is required when you are exploring behavior because you don’t have an easily testable hypothesis about the source of the bug.

Re:The unreasonable effectiveness of print debugging by cratermoon on Saturday April 24, 2021 @03:43PM (#26927727)
But with print statements if the first place I put them doesn't work, I can start doing bisects and quickly find the right place to print.

As you note, debugger breakpoints aren't magically better than print statements when I'm investigating a hypothesis – I'm no more likely to put them the right place than I would have put print statements.

And then there's a class of problems that neither debugger nor print statements will help: many years ago a very junior co-worker was wondering why his C code was giving the wrong answer for some math. It took me pointing out that one of the numeric types he was using in his code was different from the rest (I think it was #defined elsewhere, in some library, as an integer type). When the compiler did the math it had to do some type coercing.

Re:The unreasonable effectiveness of print debugging by dragonwriter on Saturday April 24, 2021 @03:52PM (#26927785)
> And then there's a class of problems that neither debugger nor print statements will help: many years ago a very junior co-worker was wondering why his C code was giving the wrong answer for some math. It took me pointing out that one of the numeric types he was using in his code was different from the rest

A debugger and watches on the values of concern absolutely will help with that (so will properly placed print statements), so its a really bad example. (Of course, strong typing helps even more with that particular case.)

Re:The unreasonable effectiveness of print debugging by cratermoon on Saturday April 24, 2021 @05:51PM (#26928481)
No, my co-worker was so junior he didn't understand why that was happening. It took me a moment to glance at the types in the source and point out the problem, no debugger needed.
Re:The unreasonable effectiveness of print debugging by jacques_chester on Saturday April 24, 2021 @05:31PM (#26928394)
> But with print statements if the first place I put them doesn't work, I can start doing bisects and quickly find the right place to print.

Can't I also bisect with breakpoints?

Re:The unreasonable effectiveness of print debugging by cratermoon on Saturday April 24, 2021 @06:00PM (#26928532)
You can, but the comment was talking specifically about stepping through the code line-by-line.
Re:The unreasonable effectiveness of print debugging by iudqnolq on Sunday April 25, 2021 @01:00AM (#26930438)
I can bisect faster with breakpoints. Plus with a time traveling debugger like rr the the time is further reduced.
Re:The unreasonable effectiveness of print debugging by mmazing on Saturday April 24, 2021 @12:37PM (#26926190)
Tools in a toolbox.

The worst developers I've ever known always have their "this is the best way to do everything" hill they die on.

Re:The unreasonable effectiveness of print debugging by pjmlp on Saturday April 24, 2021 @12:15PM (#26926017)
They weren't ninjas, otherwise they would have used breakpoint actions for doing those print statements without modifying the source code.
Re:The unreasonable effectiveness of print debugging by majjgepolja on Saturday April 24, 2021 @12:32PM (#26926160)
What's breakpoint action? Is it like inserting printf before breakpoint?
Re:The unreasonable effectiveness of print debugging by formerly_proven on Saturday April 24, 2021 @01:30PM (#26926657)
Breaking is just the default behavior when a breakpoint is hit, you can generally attach whatever behavior / conditions you want using the debugger's scripting language.
Re:The unreasonable effectiveness of print debugging by nomel on Sunday April 25, 2021 @12:06AM (#26930285)
Reading through the majority of this comment section, I get the impression that those who like print statements find value because they aren’t proficient with modern debuggers, rather than they find print statement valuable even though they’re proficient with debuggers.
Re:The unreasonable effectiveness of print debugging by pjmlp on Sunday April 25, 2021 @02:43AM (#26930799)
I once saw an interview with Visual Studio members that one reason why they started doing talks about how to use the debugger was the continuous set of requests for features that Visual Studio does almost since it exists.

Same applies to other debuggers.

It is not only the debuggers, but also OS and language runtime tracing facilities like DTrace, eBPF, ETW, JFR, ....

Many devs aren't 10x because of QI, rather because they learn and make use of the tools available to increase knowledge about the platform.

Re:The unreasonable effectiveness of print debugging by CJefferson on Sunday April 25, 2021 @08:19AM (#26932163)
I agree, but my feeling is, if one person is bad at using debuggers it is their fault. If (as it seems to me) most developers are bad at using debuggers, then it's probably to debugger's (and associated tooling's) fault.
Re:The unreasonable effectiveness of print debugging by pjmlp on Sunday April 25, 2021 @11:36AM (#26933527)
For me it is the teacher's fault, given that a large majority never teaches anything related to debuggers.

So we get generations that use vim and Emacs like notepad, create Makefiles by copy-paste and barely know gdb beyond set breakpoint, run, step and continue.

Using C as example, but feel free to extrapolate to another language.

And no I am not exaggerating, this was the kind of students I would get into my lab when I spent a year as TA in 1999/2000, and naturally would have to get them up to speed into good programming practices.

Re:The unreasonable effectiveness of print debugging by frou_dh on Sunday April 25, 2021 @03:25AM (#26930975)
Reminds me of this diagram http://i.imgur.com/ZOuf9hg.png (which I don't necessarily agree with)
Re:The unreasonable effectiveness of print debugging by pjmlp on Saturday April 24, 2021 @01:39PM (#26926741)
It maps breakpoints to debugger actions that are triggered instead of actually stopping execution, like formatted output of whatever variables are in scope.
Re:The unreasonable effectiveness of print debugging by edoceo on Saturday April 24, 2021 @01:07PM (#26926471)
maybe this is sometimes called assert()? or in some debuggers you set a watch on a var and the BP triggers only on the watch-condition so the BP don't trigger each loop, only when x=7
Re:The unreasonable effectiveness of print debugging by d0mine on Saturday April 24, 2021 @01:26PM (#26926632)
You are missing out on the important point: printing forces you to formulate a hypothesis: what you expect and to compare with what you actually get. Debugging encourages less modeling and more trying random stuff until something sticks.

It is an exaggeration. In practice, it is useful to apply both. Novices can get some insight using debugging, more experienced with code base people should exercise their understanding of the code and use well picked prints.

Re:The unreasonable effectiveness of print debugging by SAI_Peregrinus on Saturday April 24, 2021 @02:00PM (#26926927)
You can also do print debugging with a debugger. Just have a breakpoint that doesn't halt, but instead simply prints the values of interest to the debugger console. This is particularly nice for things like debugging interrupt handlers where the time taken to print output normally is too much to accept.
Re:The unreasonable effectiveness of print debugging by toxik on Saturday April 24, 2021 @03:26PM (#26927598)
Watched variables they were called once upon a time.

Also, I think what you suggest to do here is way harder to learn than printing.

Re:The unreasonable effectiveness of print debugging by saagarjha on Saturday April 24, 2021 @06:26PM (#26928719)
Perhaps, but that lends credence to the theory that people using print debugging may just not have learned how to effectively use a debugger yet.
Re:The unreasonable effectiveness of print debugging by CJefferson on Saturday April 24, 2021 @12:28PM (#26926119)
I do use debuggers when I have a hard core problem, I've found some horrible memory corruptions using RR and gdb for reverse debugging. However, sometimes someone throws a horrible gradle java project at you and asks for help, and figuring out how to debug is a pain.

I'd throw your comment back -- maybe all languages should be "debug first", make it as easy to get code into a debugger as it is to just build and run it.

Re:The unreasonable effectiveness of print debugging by xondono on Saturday April 24, 2021 @01:46PM (#26926801)
I’m also willing to bet you live way up the stack.

Try debugging something in the embedded world, and you’ll see why a lot of bare metal programmers use printfs. Turns out timing is critical most of the time, so using a debugger hides a LOT of bugs from your eyes.

Debuggers are very useful, but so are prints, there just different tools, they have different purposes.

Re:The unreasonable effectiveness of print debugging by zeta0134 on Saturday April 24, 2021 @12:13PM (#26926004)
I've used print debugging extensively doing embedded development, when I could reasonably hook up a serial port to capture the output, or put a crude console on a tiny screen. These systems can't always be debugged in the traditional sense, and if you're troubleshooting some bug that happens on hardware but not in your simulator, then you use the tools you have available.
Re:The unreasonable effectiveness of print debugging by mschuster91 on Saturday April 24, 2021 @04:44PM (#26928140)
Worst thing with Java is, especially when taking over a project or extending some open source thing: finding where that goddamn log4j config is. Is it in web-inf, in tomcat/glassfish config, somewhere entirely else (e.g. specified in the run config), or is it configured in one of the five wrapper layers.

And then you have to figure out the syntax... does it want package names, class names, or (hello Intellij plugins!) need a fucking # at the beginning to be recognized.

And then you have stuff like a "helpful" IDE that by default only shows WARN and above levels without telling you somewhere "there might be stuff you don't see" like Chrome does.

For actual debuggers, shit is worse, across the board. Running in Docker is always a recipe for issues, not to mention many applications actively messing around with stuff like ports.

A system.out.println always ends up somewhere sane.