News for hackers. Stuff that matters.
Re:Productive Programming by vilya on Tuesday August 31, 2010 @06:13PM (#1651047)
I'm amazed and a bit saddened whenever I hear sentiments like this. These days more than ever.

The thing is, every single bit of code you write depends on something else that you DIDN'T write. Everything. No exceptions.

And the thing about stuff you didn't write is that you end up making assumptions about it - often without even realising. Sometimes the assumption is simply that the code works. Sometimes, some of your assumptions will be wrong.

Debuggers help you check your assumptions. They're a very useful tool for this - more so than many people who say "I prefer printf" realise.

I apologise if you're not in that category, but did you know that many debuggers have a way to print a message (without stopping) whenever the program hits a certain line? Just like printf, only it doesn't have to be built in to your code ahead of time.

There are times when a debugger isn't the right tool for the job, but it's always better to make an informed choice.

Re:Productive Programming by anamax on Tuesday August 31, 2010 @11:32PM (#1651731)
> I'm amazed and a bit saddened whenever I hear sentiments like this. These days more than ever.

The big advantage of printf as a debug technique is that it doesn't work unless you understand what you're doing. If you plan to debug using printf, you must write code that is easy to understand.

Great debuggers let you try to fix code that you don't understand. Worse yet, they make it reasonable to write such code.

Since the person reading the code is never as smart as the person who wrote the code, encouraging simple to understand code is extremely important.

Re:Productive Programming by Roboprog on Wednesday September 01, 2010 @01:00AM (#1651895)
A big disadvantage of printf() in C (or similar non-memory-managed languages) is that printing values can disturb the "noise" on the stack that you were trying to find. That is, if you had a bug due to an uninitialized local variable or buffer overrun, the printf() call could "stabilize" things and cause a bug to stop manifesting.

Better to use a debugger, if you can. Not that I haven't ever sprinkled print's into the middle of a big mass of somebody else's code or framework just to get the initial lay of the land :-)

Re:Productive Programming by vicaya on Wednesday September 01, 2010 @03:01AM (#1652099)
uh, the -g compiler flag to enable debugging will create much more disturbance than a few printf ever could, debuggers are mostly useless for tracking down race conditions as they change the timing completely. OTOH, I'd use a low overhead logging package instead of printfs.
Re:Productive Programming by Roboprog on Monday September 06, 2010 @12:22PM (#1666913)
Good one: yes, race conditions are not going to manifest while manually slowly stepping. You of course need to plan / read carefully for code which will be vulnerable to such.

Regarding "-g", at the job a few years ago where we were using C for some in-house jobs, we skipped using "-O" for the most part and simply deployed with "-g" left on. Better safe than sorry for single-use software.

Re:Productive Programming by vilya on Wednesday September 01, 2010 @07:33AM (#1652448)
I disagree with almost every sentence in your post, but there's one bit in particular I wanted to focus on:

> The big advantage of printf as a debug technique is that it doesn't work unless you understand what you're doing.

This isn't true, nor would it be an advantage if it were.

Any fool can stick a printf statement into their code, just like any fool can run it in a debugger. You might get lucky and find the problem, or you might not. Understanding what you're doing will help you out whichever technique you use. Better yet, it will help you decide which technique is more appropriate to the problem.

Re:Productive Programming by anamax on Wednesday September 01, 2010 @02:33PM (#1653718)
> Any fool can stick a printf statement into their code, just like any fool can run it in a debugger. You might get lucky and find the problem

That's like saying that you might get lucky if you make random changes to program code. Yes, it's true, but ....

> Understanding what you're doing will help you out whichever technique you use. Better yet, it will help you decide which technique is more appropriate to the problem.

Except that I was talking about understanding the program, not "what you're doing".

In my experience, debuggers let me find things, or, more often, think that I'm doing something useful, with less understanding than printf. YMMV.

Re:Productive Programming by berntb on Tuesday August 31, 2010 @07:45PM (#1651268)
>>Debuggers help you check your assumptions. They're a very useful tool for this - more so than many people who say "I prefer printf" realise.

A long time ago, I used debuggers and printf.

For years, I've instead added tests to checks my assumptions -- and I run the tests multiple times an hour.

(I still printf since it works well with the tests if I run a subset of them.)

(Edit: This was for when I write code, not working with others.)

Re:Productive Programming by vilya on Wednesday September 01, 2010 @07:16AM (#1652415)
If you're saying tests are an alternative to using a debugger, then I disagree.

Tests are very useful, but they fulfill a different need to debuggers. In fact the two work well together: stepping through a failing test case with a debugger can be a very effective way to find the root cause of a problem.