Some say that a debugger is the mother of all evil. What do you think of this approach?
I have a friend at work, a colleague, who's completely against using a debugger whatsoever.
I asked him: So, you just write code without bugs? Is that it?
He answers: Of course not. Everyone makes mistakes, the difference is how you deal with them effectively, how you make sure not to make the same mistake again. When using a debugger, you may find your way to that bug, and you may fix it for the specific scenario you've witnessed, but
you're wasting your time because all that time put into debugging can never be reused. It's a one time hack in the sense that if you have another bug later, you'll probably need to start all over again, and
you've only solved this one bug, and while it might only occur for this specific scenario that you tested, you most likely did not solve a more general problem. That's because you're not thinking in generality, you're in a debugging mindset, not a general mindset.
Me: OK, fine, you don't use a debugger, you think it's a waste of time. What do you do when you find a bug then?
Him: When I find a bug here's what I do:
- Read my code. Understand it. Document it.
- If a class or a method or a function is not coherent refactor it until it is.
- Add asserts. Use preconditions, post-conditions etc. Asserts are very effective.
- Add logging. When the program runs it should tell its user what it's doing, like you're reading a book. Don't assume the user understands the code, don't assume you understand the code. Let the program tell you exactly what it's doing, you will not regret it.
- Unit-Testing. Except for the most trivial getters and setters, you need to test everything. Most bugs can be found while unit-testing, or while writing the tests.
- Code review. Have someone else look at your code. When he/she asks you questions you'll understand your code better. Many times I found bugs when trying to explain what my code is doing to a reviewer.
Me: OK, dude, that's a lot of things. Are you sure this is the best use of your time?
Him: True, if you have a single bug at 8 PM after a long day, and all you want to do is fix it and go home, you might get tempted to open a debugger and get rid of that thing already, right?
Me: yeah...
Him: Well, I think that this is when good developers show. A good developer needs to be self-disciplined and realize that: every minute you waste on a debugger is a wasted minute. You'll never get your time back. While if you invest your time smartly in documentation, refactoring, asserting, logging, unit-testing and code reviews you're investing in a brighter future. It might be that this evening you'll get back home late and that is indeed sad, but I also guarantee you that you are not going to regret this and in the next couple of days not only that your coworkers think highly of you, but also you'll have much more free time since at this evening you solved not only one bug, but also a design issue and five other bugs.
Me: OK, that's a bit extreme for me. I can see why you're saying that using a debugger is a very short-term investment and that professionals should make long term investments, that's cool. But, isn't it a bit too extreme? I mean is there any good time to use a debugger at all? What about, for example when you inherit the code and you don't even know how is it supposed to run?
Him: Dude, in my team I'd not want to have you. If you want to read new code, print it and take it somewhere quiet. A debugger is not a Kindle.
So, stackoverflowers, what do you think of this approach? Is a debugger the mother of all evil?