Hacker News new | ask | show | jobs
by pj 6151 days ago
I like to write my code in my head before I write it with code. Try to imagine all your code and then the code you are going to write and then run through it in your mind. Think of some special cases in your mind. Suppose you are writing a function that accepts a parameter. Okay, so go write it in your head and then imagine, what if someone passes in a string or an int? are these different cases? do I throw an exception, do I return a particular value? How will that affect the rest of the code? What if the code is converted to a library and used by someone else? Could a string be a valid parameter?

Then run through the code in your mind. Does it work? Pass in some variables.

Your mind can process information much faster than you can type it out. If you build the image of your code in your mind, you can map it out in a matter of minutes, but if you start coding without the image, you make more mistakes.

Some other commenters have said write it out or draw it out. This is a good idea if you can't keep it in your head. Do flow charts and state diagrams and things like that before you start coding.