Hacker News new | ask | show | jobs
by de4sher 3546 days ago
Though I'm a big fan of debugging, and one of my specialties is debugging everything, I'd also like to point out that writing unit tests is also a very good way to "debug".

2 good things come out of unit tests:

1. You'll decrease the chances of having to debug that code again 2. You'll learn how to write code that needs less debugging.

Now to adress your question directly, I have developed a few techniques of debugging, which come in quite useful.

1. Going directly to the problem. Example 1: you're debugging some database problems -> put a breakpoint in the library that's interfacing with the database. Example 2: Some object has some unexpected property -> put a breakpoint at the point when that property is set. Example 3: you're receiving weird http requests -> put a breakpoint as close as you can to the code giving you the request.

2. Conditional breakpoints. Some IDEs have this feature build in. If not, you can just write code around that. All you have to do is write a conditional statement with the condition you expect, and if it will evaluate to True, put a breakpoint inside this condition - there you have it, simple conditional breakpointing (helps with loops and other scenarios).

3. Write code that minimizes state change. I know I'm being preachy again, but the worst bugs I encountered all revolved around objects being changed by code in extremely unexpected places. Don't do that! Think by default never to modify objects once created, and only stray from this rule when the alternative becomes ridiculous (e.g. when you have to write a lot of really complicated code which nobody will understand)