Hacker News new | ask | show | jobs
by idlewords 6151 days ago
Try setting yourself a mental challenge of having a subroutine/code snippet work the first time you run it. Start very small. You'll be surprised at how hard this is, and trying this repeatedly can build good habits of reasoning about code.
2 comments

Strangely I had the experience (almost 46 years ago) of writing a program a week for a semester, without encountering either a bug or compilation error. It wasn't until my first assembly language program that I encountered one (a period in one statement where a comma should have been). They were to be sure, very simple programs (similar to the very easiest problems on Project Euler).
This is good advice, but the best way to do it is paradoxical. Before you even run the code, debug it in your head. Pretend you ran it, and it failed in whatever way that you'd expect it to, assuming it were going to fail. You will find that this exercise causes you to hold yourself accountable, in ways that the traditional edit-compile-test cycle does not. When you truly hold yourself accountable, the silly mistakes will happen less often.