TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: How do you debug code?

10 pointsby questionrover 8 years ago
For a given algorithm, I find myself placing debug logs all over to print values of variables.<p>I even find this quicker than using breakpoints.

5 comments

maramonoover 8 years ago
It depends on the difficulty of the bug(s) that need to be localized and fixed.<p>I use a wide range of techniques from simple printf&#x27;s all the way to a full-blown methodology that has not failed, especially when dealing with coupled bugs or those caused by concurrency issues, hardware and such.<p>I wrote about my methodology here: <a href="http:&#x2F;&#x2F;ortask.com&#x2F;wp-content&#x2F;uploads&#x2F;fault_localization_improved_testing-Non_ACM.pdf" rel="nofollow">http:&#x2F;&#x2F;ortask.com&#x2F;wp-content&#x2F;uploads&#x2F;fault_localization_impr...</a>
de4sherover 8 years ago
Though I&#x27;m a big fan of debugging, and one of my specialties is debugging <i>everything</i>, I&#x27;d also like to point out that writing unit tests is also a very good way to &quot;debug&quot;.<p>2 good things come out of unit tests:<p>1. You&#x27;ll decrease the chances of having to debug that code again 2. You&#x27;ll learn how to write code that needs less debugging.<p>Now to adress your question directly, I have developed a few techniques of debugging, which come in quite useful.<p>1. Going directly to the problem. Example 1: you&#x27;re debugging some database problems -&gt; put a breakpoint in the library that&#x27;s interfacing with the database. Example 2: Some object has some unexpected property -&gt; put a breakpoint at the point when that property is set. Example 3: you&#x27;re receiving weird http requests -&gt; put a breakpoint as close as you can to the code giving you the request.<p>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).<p>3. Write code that minimizes state change. I know I&#x27;m being preachy again, but the worst bugs I encountered all revolved around objects being changed by code in extremely unexpected places. Don&#x27;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)
_RPMover 8 years ago
I usually will look at the input, and then look at my output. If it&#x27;s not what I expect, I look at the code. Then I&#x27;ll discover something usually. It&#x27;s hard to articulate how I do it. I couldn&#x27;t pass one of those hacker rank style challenges for 2 or 3 companies this year out of the box, but I think I can debug code pretty well.
malux85over 8 years ago
For &#x27;shallow&#x27; bugs, printing &#x2F; logging - solves problem 90% of time.<p>Deeper bugs use the IDE, breakpoints and debugger, goto definition and call stack examination solve the other 10%<p>Some tips for production: Put logging calls in at critical points -- e.g. initialisation of interfaces log input and output shapes (I work in mostly in vectors), DEBUG level logging, which is more verbose, can log the shape of incoming vectors to help with sanitation efforts.
veddoxover 8 years ago
Yes, debug logs are my usual m.o. too. In general I follow the divide-and-conquer approach for localizing the source of a bug - it works well for &gt; 95% of bugs.<p>If you&#x27;re using an interpreted language, a REPL is brilliant for debugging individual functions (I use this mostly with Lisp, but also to some extent with Python).<p>Disclaimer: I&#x27;m not a professional dev...