News for hackers. Stuff that matters.
Re:Ask HN: How do you familiarize yourself with a new codebase? by collyw on Friday June 26, 2015 @10:07AM (#9784423)
I am surprised how few younger programmers use a debugger these days.
Re:Ask HN: How do you familiarize yourself with a new codebase? by neves on Friday June 26, 2015 @02:27PM (#9786727)
Debuggers and profilers are essential tools, but I was surprised when I read the book "Coders at Work: Reflections on the Craft of Programming" that a lot of the greatest programmers debugged just with a printf.
Re:Ask HN: How do you familiarize yourself with a new codebase? by archagon on Friday June 26, 2015 @07:06PM (#9788255)
Maybe this is just my lack of experience speaking, but I find debuggers incredibly difficult to use when dealing with interactive software (which is most software I work on). Hitting a breakpoint freezes my interaction unless I configure it to print something, and configuring it to break conditionally requires knowing some obscure incantations that I can never remember off the top of my head (and that in turn require their own debugging). So it boils down to: should I spend 5 minutes figuring out how to set this conditional, print-only breakpoint? Or should I just put in a printf? The solution, at least to me, is obvious.

(With that said, when I have a nasty bug that requires squishing around in the guts of my program, breakpoints are invaluable. But it's a pretty huge hammer that only comes out on occasion.)

EDIT: I wonder how useful an editor that allows inline debugging code would be? Instead of setting breakpoints in your IDE and then configuring them in an individual window, you'd enter a "debugging editor" mode that would allow you to add debugger-only code for things like conditional breakpoints and printf statements right alongside your normal code. The original source files would not be edited, but while in this mode, it would appear as if they were. (Maybe the debugging code would show up in red.) That way, you could easily access all your local state and implement complicated queries without ever leaving the context of your code. In other words, it would be just like printf debugging, but once you no longer need to debug you'd just collapse those statements out by leaving the debugging editor mode and your original code would be unchanged. Perhaps this debugging code would not compile along with the rest of the code but would instead use the debugging hooks that normal breakpoints use, unless the conditions are particularly complex or something. Just a thought.

Re:Ask HN: How do you familiarize yourself with a new codebase? by bostonvaulter2 on Friday June 26, 2015 @08:30PM (#9788523)
Not sure if you know this but if you're in javascript you can just drop a 'debugger;' statement anywhere in your code and it will function as a breakpoint.
Re:Ask HN: How do you familiarize yourself with a new codebase? by archagon on Friday June 26, 2015 @08:35PM (#9788545)
Ah yes, I should have mentioned that this is coming from a native application developer!
Re:Ask HN: How do you familiarize yourself with a new codebase? by neverminder on Friday June 26, 2015 @10:11AM (#9784454)
I'm not a younger programmer and I don't use a debugger. Tried it several times and found it counterproductive.
Re:Ask HN: How do you familiarize yourself with a new codebase? by candu on Friday June 26, 2015 @10:48AM (#9784741)
Like with most useful tools, you have to use it more than "several times" to gain fluency.
Re:Ask HN: How do you familiarize yourself with a new codebase? by webhat on Friday June 26, 2015 @04:45PM (#9787640)
Indeed, and learning to use a debugger will allow you to gain fluency in new languages and frameworks much faster that the simple, albeit powerful, printf.

I remember needing to learn Ruby and Rails on an existing closed codebase and having the debugger take me right to the core of Rails several times to understand why certain things were done in a certain way in the top level code. This allowed me to get acquainted with internals of Rails and how Ruby's inheritance system much faster as well as the existing codebase that was built on top.

Re:Ask HN: How do you familiarize yourself with a new codebase? by kelvin0 on Friday June 26, 2015 @10:49AM (#9784755)
How is it counterproductive exactly? The setup of the debugger itself? Or trying to figure out how it works?
Re:Ask HN: How do you familiarize yourself with a new codebase? by chimprich on Friday June 26, 2015 @11:43AM (#9785318)
It depends on what you're working on.

If you're working on, say, a huge Java codebase, then a debugger is practically essential because you've got a lot of code to navigate, and you probably want to see what flavour of objects are being passed around and how their state is being updated.

On the other hand, if you're working on, ooh, a Nodejs codebase, you're probably looking at less code, with a debugger that's much slower, and probably more functional code that is operating on data directly. Using a debugger in that case is often slower than just using print statements.

Re:Ask HN: How do you familiarize yourself with a new codebase? by monk_e_boy on Friday June 26, 2015 @01:31PM (#9786357)
I'd argue that print statements are a debugger, albeit a custom one off debugger. Sounds like the actual debugger needs work (... or from my experience it's me who doesn't understand the debugger correctly)
Re:Ask HN: How do you familiarize yourself with a new codebase? by Retra on Sunday June 28, 2015 @11:29AM (#9793848)
That argument is a bit of a stretch. Just because you are debugging something doesn't mean you're using a debugger...
Re:Ask HN: How do you familiarize yourself with a new codebase? by neverminder on Friday June 26, 2015 @11:36AM (#9785240)
It's a complicated explanation I suppose, but I would say that among other things, I've seen my colleagues abusing debugging and since debugger is a "single threaded" and sequential approach if you will, they were losing the big picture. I try to understand the code and keep it in my memory so that I could predict the behaviour without the debugger. On a better day this would be a better explanation.
Re:Ask HN: How do you familiarize yourself with a new codebase? by blakeyrat on Friday June 26, 2015 @02:51PM (#9786918)
I'm not sure how you "abuse" debugging-- can you elaborate on that?

The debugger runs in as many threads as the app itself. If you're debugging C#, and your program is in 37 threads, you can debug all of them simultaneously or debug one and ignore the others.

Understanding the code and keeping it in memory sounds like a great practice, but it also seems entirely orthogonal to using a debugger.

I have the exact opposite attitude: I live in the debugger. Most of my resistance to trying "new hot" languages is their universally terrible debuggers. (Usually they either don't exist at all, or only exist in CLI form.) IMO, if you don't have a working, stable, graphical debugger, your programming language has no business being 1.x.

Re:Ask HN: How do you familiarize yourself with a new codebase? by jeremiep on Friday June 26, 2015 @12:39PM (#9785890)
The debugger being sequential is a consequence of your code also being sequential at the CPU level.

Understanding the code is often a luxury we don't have, either because we didn't write it in the first place or because there are just too many moving parts each tracking their own state and interacting with everything under the sun.

This is especially true for long-running programs that do much more than just transform inputs into outputs. For example a video game would be complete hell to develop without a solid debugger. Even if you believe you understand the complete code and all its behavior the testers will always find a way to make the game crash after 2 hours of playing leaving the game's state completely broken and without a debugger you'll be scratching your head endlessly trying to replay how that happened.

In these cases the number of different states and behaviors you can get out of the system is incredibly high, easily a few orders of magnitude more than the human brain can handle.

Re:Ask HN: How do you familiarize yourself with a new codebase? by yellowapple on Friday June 26, 2015 @02:18PM (#9786666)
> or because there are just too many moving parts each tracking their own state and interacting with everything under the sun.

For me at least, that's typically my cue to start refactoring. If I can't model at least the general behavior of my program in my head, then it's probably needlessly complicated.

This isn't always possible, of course (such as in the case of long-running programs, as you mentioned; it's still possible in a lot of cases, though, and splitting off functionality into smaller, easier-to-digest pieces can make troubleshooting much easier), but it's been a useful mentality for me, and has significantly reduced the need for me to use some sort of debugger or even 'print' statements to make sense of code.

This doesn't address the original question of understanding new codebases, though.

Re:Ask HN: How do you familiarize yourself with a new codebase? by corysama on Friday June 26, 2015 @12:00PM (#9785510)
> I try to understand the code and keep it in my memory so that I could predict the behaviour without the debugger.

That's the theme I hear when prodding people who (sometimes loudly) say they don't use debuggers. They work on codebases that are either small, solo projects, or change very slowly. That way they can keep an accurate simulation of the entire program in their heads.

Meanwhile, I've always worked on codebases that were changing faster than I can keep up. The majority of my debugger time has been in code I've never seen before.

Re:Ask HN: How do you familiarize yourself with a new codebase? by neverminder on Friday June 26, 2015 @12:29PM (#9785778)
Not necessarily. For instance, my colleagues work with a fairly large codebase in java which runs on top of a framework written in scala. When they tried to hook up a debugger it drilled the whole stack top to the bottom, naturally and showed them whole lot of scala code. Problem is, these guys don't now scala, only java.
Re:Ask HN: How do you familiarize yourself with a new codebase? by collyw on Sunday June 28, 2015 @05:19PM (#9795145)
My debugger shows a full stacktrace too. I usually ignore the Django code that's its written on top of and use it in the sections I have written.

I don't find it a hindrance in any way seeing what part of the Django code called my code.

Re:Ask HN: How do you familiarize yourself with a new codebase? by gbersac on Friday June 26, 2015 @10:27AM (#9784567)
I just use printf and other stuff to dump critical variable. I also use unit test a lot.
Re:Ask HN: How do you familiarize yourself with a new codebase? by kelvin0 on Friday June 26, 2015 @10:51AM (#9784781)
Ah yes, the good ol' printf-debug-polluting-codebase method. What's fun after that, is when programmers all start competing to have their printf's be more visible in the sea of prints ... I find this method primitive at best. How do you manage it?
Re:Ask HN: How do you familiarize yourself with a new codebase? by Chris_Newton on Friday June 26, 2015 @12:46PM (#9785945)
I find this method primitive at best. How do you manage it?

Don’t be primitive about it. :-)

The serious answer is: use a good logging framework. In particular, I’d suggest something that can log at selective levels of information for different parts of your system.

If your project’s culture is to instrument your code like this routinely, it can be a very useful asset, and it’s no more disruptive in practice than say adding comments or writing tests. In each case, with experience you get better at judging where to focus your efforts and how much detail is worth including by default, and if that turns out not to be enough you can always add more while you’re working in that area.

With modern tools for recording and analysing logs, there is relatively little useful information that you can easily find using a debugger but can’t easily find from good log output. On the other hand, logging has some big advantages: you can capture how your system changes over time, you can record and review concurrent behaviour cleanly, you can capture information from different parts of distributed systems that might be written in different programming languages or running on different devices.

Re:Ask HN: How do you familiarize yourself with a new codebase? by weavie on Friday June 26, 2015 @11:12AM (#9785016)
You remove the printfs when you have fixed the problem.
Re:Ask HN: How do you familiarize yourself with a new codebase? by thaumasiotes on Friday June 26, 2015 @05:57PM (#9787997)
It's so tempting to do this. Don't. Problems may occur again in the same area. Leave your logging statements around; configure your logging setup to skip them when you don't want them.

You're saying to delete commented-out code once the new code works. Put your code in version control instead. Logging is to once-off printfs as version control is to commented-out code.

Re:Ask HN: How do you familiarize yourself with a new codebase? by weavie on Saturday June 27, 2015 @03:50PM (#9791328)
Not so sure. Once I get to the stage where I have to stick printfs in the problem tends to be so localized and specific that once fixed I will never need to look at that area of code in that way again.
Re:Ask HN: How do you familiarize yourself with a new codebase? by collyw on Thursday July 02, 2015 @07:12AM (#9818112)
I do this far more than I should. The code ends up a mess after enough debugging like that.
Re:Ask HN: How do you familiarize yourself with a new codebase? by ninkendo on Friday June 26, 2015 @01:03PM (#9786093)
In my case, aggressively reject any printf statements in code review. Get that shit out of my repo.
Re:Ask HN: How do you familiarize yourself with a new codebase? by lberger on Friday June 26, 2015 @02:37PM (#9786798)
Without trace statements, how do you diagnose issues that don't occur locally and only appear in the production environment.
Re:Ask HN: How do you familiarize yourself with a new codebase? by ninkendo on Sunday June 28, 2015 @01:39PM (#9794291)
Traces are the kind of thing that should be automated anyway... You shouldn't have to write explicit trace statements in your source code, instead your language or framework should have an automated way of inserting these statements at function boundaries. Or even better, attach a debugger and reproduce the issue yourself on the prod server.

Just plain "debug log statements" I have yet to hear a good use case for. Every time people have put them in, it's because some bug called for their inclusion, then the bug got fixed, then the statements got left around after the bug was fixed. Or the bug didn't get fixed and it's a matter of "why don't you fix the bug?"

There's rare cases where there's an ongoing issue with a known bug that you don't know the fix for yet, so you drop debug statements in production code hoping to catch it. But this is supposed to be a rare, rare case.

Re:Ask HN: How do you familiarize yourself with a new codebase? by robertfw on Friday June 26, 2015 @05:04PM (#9787743)
That would be the difference between printing and logging
Re:Ask HN: How do you familiarize yourself with a new codebase? by casta on Friday June 26, 2015 @11:14AM (#9785035)
I think he meant to add print statements locally. You remove them before committing the code.
Re:Ask HN: How do you familiarize yourself with a new codebase? by barrkel on Friday June 26, 2015 @01:51PM (#9786492)
printf = breakpoint expression evaluation (but without the flexibility)

You're using a primitive debugger, you just don't know it.

Re:Ask HN: How do you familiarize yourself with a new codebase? by noir_lord on Friday June 26, 2015 @02:54PM (#9786939)
It does have the advantage of both immediacy and continuation.

You can dump statements through a function and see it's progression when run without having to interact with it.

Throw in something like FirePHP (which allows dumping pretty much anything out as a viewable/collapsible trace, other languages have similiar) and the use case for a lot of using a full blown debugger is removed (it's still incredibly powerful when needed).

So I use both.

Re:Ask HN: How do you familiarize yourself with a new codebase? by girvo on Friday June 26, 2015 @07:18PM (#9788298)
Codebug + Xdebug, setting a breakpoint and getting a full REPL at that execution point is far more useful to me when I'm in PHP land :)
Re:Ask HN: How do you familiarize yourself with a new codebase? by jeremiep on Friday June 26, 2015 @12:45PM (#9785934)
I tend to do that mostly for concurrent programs, as I want to see what happens without blocking the entire system like a debugger would.

For medium/large programs I'll often prefer a debugger because I can easily and quickly try to diagnose the problem without having to wait for a new build to complete with the added traces.