Always be Debugging

Back when I was a Junior Programmer at my first programming gig I used to have conversations like this with my mentor.

Me: See, I enter the value here and then I get back the correct result. So, it works.

Mentor: Did you run it through in the debugger and see what the code was doing?

Me: I don’t need to, it works.

Mentor: It might be working just by luck, run it through and check.

I’d do as he said and run it through and around 50% of the time I’d spot something that could have gone wrong (we were writing C++, there was an awful lot that could go wrong).

So, gradually, I got into the habit of always stepping through any new code I had written in the debugger.

Now I’m more experienced I don’t do that so often (I tend not to make those silly mistakes so much – plus we have unit tests for picking up on said silly mistakes, oh yeah, and I no longer code in C++).

But one habit that has stayed with me is to always run my application in debug mode in Eclipse (I develop web apps in Java now).

That way if I do see something dodgy I can set a breakpoint, refresh the browser and immediately find out what’s going on.

The option to run my application in non-debug mode may as well not exist in Eclipse for me – I think I’ve only ever clicked that button by accident.

However, when I am asked by another team mate to help them out with a coding issue at some point we might have a conversation like this.

Me: Stick a breakpoint on that line there.

Team mate: I’m not running in debug mode, I’ll need to restart.

Me: Hmm, you should always run in debug mode, it’s great for things like this.

Team mate: Debug mode is slower.

Me: Restarting every time you have a problem isn’t exactly fast either.

I try not to go off on one at this point and lecture on the benefits of running in debug mode but I do think that stepping through code and seeing what’s going on can help can make people better programmers.

It’s not just the scenario described above.

If you’re running your application and see something not quite right it’s very easy to add a breakpoint and find out what’s going on.

If you’re not running in debug mode the temptation is to think “I’ll take a look at that later” and then forget all about it.

If you don’t already do this then you might want to try it for a week or two.

My betting is that you won’t switch back.

Leave a Reply

Your email address will not be published. Required fields are marked *