News for hackers. Stuff that matters.
Re:Icecream: Never use print() to debug again in Python by maccard on Tuesday March 30, 2021 @05:39AM (#26632697)
> It's quick way to narrow down the "area of search" before bringing in the big guns.

What are the big guns? with a debugger, I can stick a breakpoint and look at the entire state of everything. Given we're talking about Python, in pycharm [0] you can even execute your print statements in the debugger if you so wish. If you get the location wrong, or want to see what's going on elsewhere you can just continue execution and use another breakpoint.

This is even more important if you have a long compile/deploy cycle (I work in games, and rebuilding and deploying to a console can be a >10 minute iteration time)

[0] https://www.jetbrains.com/help/pycharm/part-1-debugging-pyth...

Re:Icecream: Never use print() to debug again in Python by ZaoLahma on Tuesday March 30, 2021 @07:17AM (#26633252)
Sometimes sticking the debugger into the wheel makes stuff come flying over the handle bars in spectacular ways that have nothing to do with what you wish to observe. You might not even know which wheel to jam the debugger stick into, if the behaviour is complex.

In these cases prints work well as a less intrusive way to get a rough idea of what is going on.

Re:Icecream: Never use print() to debug again in Python by maccard on Tuesday March 30, 2021 @08:04AM (#26633610)
I don't understand your wheel analogy, sorry.

> You might not even know which wheel to jam the debugger stick into, if the behaviour is complex.

If you don't know where to put a breakpoint, how do you know where to put a print statement?

Re:Icecream: Never use print() to debug again in Python by Macha on Tuesday March 30, 2021 @08:19AM (#26633733)
Imagine putting breakpoints in multiple tight loops in the stage of narrowing the search space. Imagine how many times you need to click next. A conditional breakpoint will only help if you know the condition you're looking for, but there's stage before that of "Well, what looks strange during execution".

Also for multithreaded code, stopping one thread dead for long enough for a human to investigate it can inadvertently resolve all sorts of race conditions.

Re:Icecream: Never use print() to debug again in Python by sebastialonso on Tuesday March 30, 2021 @08:50AM (#26633961)
What I imagine Macha is arguing for is that the cost of using print is extremely small, smaller at least than breakpoints.

No one is saying breakpoints are useless, sometimes printing is 'cheaper' in time and effort in order to locate the region code of code in which using breakpoints is cheaper.

Re:Icecream: Never use print() to debug again in Python by Macha on Tuesday March 30, 2021 @09:44AM (#26634532)
Yes, print() and breakpoints are different tools with different uses and there's cases where one is superior to the other. This is why some tools now offer logpoints, which are basically print() inserted via a breakpoint UI rather than in your code where you can forget to remove them
Re:Icecream: Never use print() to debug again in Python by kyawzazaw on Tuesday March 30, 2021 @11:50AM (#26636065)
Oh please tell me about those tools.
Re:Icecream: Never use print() to debug again in Python by maccard on Tuesday March 30, 2021 @12:39PM (#26636845)
Re:Icecream: Never use print() to debug again in Python by Macha on Tuesday March 30, 2021 @11:54AM (#26636139)
VS Code and Firefox Developer Tools are the two I'm aware of with actual support. Also some tools you can adhoc it as a conditional breakpoint by basically putting "print(whatever); return false" as the condition
Re:Icecream: Never use print() to debug again in Python by maccard on Tuesday March 30, 2021 @12:48PM (#26636987)
> the cost of using print is extremely small, smaller at least than breakpoints.

I don't think it is, at all. The cost of using print is re-running your applciation with a code change, whereas the cost of a breakpoint is re-running your application with a breakpoint. Clicking in a gutter in an editor, pressing a keyboard shortcut, or typing "b <line number>" into your debugger is no more time or effort than adding a print statement, and re-running your program.

Re:Icecream: Never use print() to debug again in Python by maccard on Tuesday March 30, 2021 @12:45PM (#26636940)
> Imagine putting breakpoints in multiple tight loops in the stage of narrowing the search space.

If you have enough loops to make breakpoints impossible to use, you've likely got enough log output that you're not going to be able to parse. You're almost certainly going to look for other ways of narrowing the search space.

> stopping one thread dead for long enough for a human to investigate it can inadvertently resolve all sorts of race conditions.

Stopping one thread for long enough to do console IO has the same effect. Especially if you're using python, you'll need a lock to synchronise the print statement across your threads!

Re:Icecream: Never use print() to debug again in Python by Fethbita on Tuesday March 30, 2021 @04:33PM (#26640068)
Today I was trying to solve the exact scenario in the second example. A multi threaded program had a race condition that would sometimes occur. printing numbers helped a great deal. Might also be that I'm not that proficient with my debugger even though I use that more than anything.
Re:Icecream: Never use print() to debug again in Python by Franciscouzo on Tuesday March 30, 2021 @02:11PM (#26638139)
> Imagine how many times you need to click next

Once or twice? Any sane debugger has a way to disable the breakpoint.

Re:Icecream: Never use print() to debug again in Python by Macha on Wednesday March 31, 2021 @07:14AM (#26645801)
Then you overshoot the iteration that has a problem
Re:Icecream: Never use print() to debug again in Python by kyawzazaw on Tuesday March 30, 2021 @11:50AM (#26636057)
As someone who help teaches intro level CS class, debuggers can be too OP.
Re:Icecream: Never use print() to debug again in Python by klyrs on Tuesday March 30, 2021 @12:47PM (#26636977)
> in pycharm [0] you can even execute your print statements in the debugger if you so wish

In my experience, debuggers are really good to expose hidden control flow. But usually, I know the flow, and using a debugger for human-in-the-middle print statements is just going to slow me down. Worse, those print statements are ephemeral, so I'm disinclined to write a nice formatter.

Print debugging leverages the language -- want to print in a certain condition? Easy. Have a recursive structure, an array, or a graph that you need to investigate? A naked print sucks, a custom formatter is great. Need to check some preconditions/postconditions? Do that in code. Don't try to check that stuff by hand in the debugger.

Speaking personally... the only thing I like about icecream is that ic(foo) both prints and returns foo, because you can inject it into code basically for free. But I already have a solution to that:

  def ic(x): print(x); return x