Andrew Stopford's Weblog

poobah

News

Articles

Family

Old Blogs

How much time did you spend in the debugger today?

Here is a question, how much time did you spend in the debugger today?

Lets say for a 10 hour project you spend on average 1 minute of every hour in the debugger. Of course this is a guess, if your writing code from scatch then you won't spend as often in the debugger as you aready have a mental note of where you need to set watches and breakpoints. Come back to that code in 6 months or debug someone elses code and you will be spending a lot longer trying to set breakpoints, stepping over/through etc. Even with user requirements helping point you in the right direction it will take time to figure out whats going on (and with out user requirements it's even more puzzle solving time).

Those that unit test there code, agile or otherwise will likely be saying I have spent less than a minute to no time at all in the debugger today. They will have had the inital time hump of learning what the code is doing, but it's likely a good set of user requirements and concrete set of tests allowed them to zero in on an area of functionality a lot faster than a trip with the debugger would. Further more, with the tests telling the developer what the code is supposed to do in accordance with the user requirements of the system, a developer can change\refactor the code with out fear of introducing new bugs or breaking the code completly (and changing the code in a tool like VS with td.net is zero friction, refactor, right click on the test method, run, green move to the next change, red go fix and repeat - before you commit). Refactoring with out effective regression is like swimming with sharks with your arm hanging off).

But I get ahead of my self, 10 minutes in the debugger out of your 10 hour project. Lets say that for every hour writing code you spend that 10 minutes writing tests instead. You can still find and eliminate bugs as you go, but you're also spending the time making sure that the tests for your code are automated and repeatable. Lets now say that 6 months on someone else has 1 hour of refactoring to make on the project. According to our average thats 1 minute in the debugger, but given that this person has never seen the code before it will likely be longer. However with our automated regression suite, it's likely going to take only a few seconds to run the tests and see if anything is broken, on average the developer is saving time. If the developer adds bugs through regression failures or spends time figuring out the code then this time scale goes up. 

Comments

FransBouma said:

tests in code aren't the documentation how the code works internally. Some people seem to think that, but they just test the outer interface's behavior.

(here, interface is the broader definition of the word).

Furthermore, the debugger isn't a code reviewing toolkit. It's also not equivalent with debugging. A debugger is a tool used DURING debugging, but you can also debug without a debugger, you're then still debugging. Unittests don't make debugging time smaller, they just test the code written. I.o.w.: they are measures to see if what's written is correct, but if that's not the case (test fails) they can't tell you WHERE the cause of that is and WHY, they just tell you that something is wrong.

Solving that is a puzzle. Most of the time, you don't need a debugger at all, just common sense.

So, what you tried to tell with the post, I have no idea... perhaps you wanted to illustrate that writing tests is making your code more robust. I can agree with that, but that has nothing to do with debugging, using a debugger or understanding how code works. In libraries with hundreds of classes and a lot of code, you can stare at your unittests all day, but you still won't understand how it works INTERNALLY.

That's something that shouldn't be overlooked, i.e.: documenting how stuff works internally is still required, no matter how much unittests you write.

# June 12, 2007 3:55 AM

andrewstopford said:

I agree that debugging is not a code reviewing tool, your preaching to the wrong guy Frans. I also agree that finding a bug will require you to debug, BUT to do that you can zero in, quickly on what you need to debug down to a small unit of code that with out tests would be a much wider\time costly exercise.

Your point about understanding how something works internally is an interesting one. Tests as executable units in accordance to your user requirements/user stories tell you exactly what is going on. Again you can zero in on an area that you wish to work on with out fear of whats going to happen on wider scale and in a lot, lot, less degree of time it would take than a wild walk with the breakpoint.

# June 12, 2007 4:34 AM

Kevin Hazzard said:

For many developers, the current infatuation with unit testing has taken a bad turn, in my opinion. A colleague of mine recently attended a week long seminar taught by a somewhat famous coding guru. I won’t say who the instructor was for fear of embarrassing him in a few years when he understands the folly of his youth. My colleague came back to work bragging that “So-and-so hasn’t used a debugger in 3 years because his unit testing discipline is so strong. I’m never going to use a debugger again.” Wow! I didn't have the heart to tell my co-worker that the lecturer had been teaching full time over the past 3 years which explained why he could make such an outrageous statement.

That same co-worker came to me a week later with a seemingly difficult problem he had been trying to solve for hours. He was out of options or so he thought. I listened to his problem and said, “Let’s run your code in the debugger to see what it’s doing.” He stood idly by while I fired up the debugger. Within one minute, I had found the root cause of the problem. The problem was very simple and the debugger exposed it brilliantly. On exception, some of the code my friend was depending on was throwing a new exception without attaching the old exception as the InnerException. The debugger let me inspect what was happening and to SEE the real root cause.

In this case, even the finest unit tests in the world wouldn’t have uncovered the real problem. My friend’s tests uncovered THAT there was a problem but they could never have found the root cause given that the code was hiding it. My advice is to use debuggers for what they are good for and to use unit tests for what they are good for. Furthermore, while unit tests are good for setting expectations about what code should and shouldn’t do, they depend heavily on the developer’s understanding of the boundary conditions. When working with complex business logic, the perimeter of understanding is often limited by many factors which are beyond the developer’s control. It’s very good to unit test prolifically. It’s better to use it with an arsenal of tools including debugging, profiling, functional automation, object mocking, etc.

What’s baffling about this current generation of developers is that they claim to embrace the open source movement. I know I do because I truly love using the debugger to step through the source code for Spring.NET, NHibernate, log4net, NAnt and so many other brilliant bodies of work. Stepping through a factory pattern implementation in Spring.NET, for example, is often like drinking from a fire hydrant of knowledge. And once in a while I’ll stumble on something in the code that I know could be done with much more efficiency. So, I’ll pop out of the debugger and pop into my profiler to confirm my suspicions. Then I’ll extend a class or two and pop back in to EACH AND EVERY ONE of my tools make sure everything is just as I expect it to be. Voila! The code runs 8 times faster under heavy load and I have much more confidence because I've actually SEEN IT working inside and out.

What I don’t understand, to be honest, is that if developers love free source code so much, why are so many of them afraid to step through it as it’s running? If developers are not going to use the debugger as part of their learning ritual (or at a minimum, for faster triage), why do they need the source code at all?

# June 16, 2007 10:48 AM

at what time did you come today said:

Pingback from  at what time did you come today

# June 4, 2008 10:27 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)