Follow this simple rule:
Write a comment for each line of code, making it a complete sentence to express a complete thought.
You should aim to write for two computers in parallel -- one is the CPU, and the other is between your ears.
Why does this technique work to make software more reliable?
Line-by-line comments serve as a kind of logical checksum for the code, such that if the code and its comment differ in function, then you know immediately that work needs to be done in that area. Often, a coding bug will be revealed as you are writing the comment that goes along with the code.
More broadly, line-by-line comments serve to hold the intellectual context and keep your mind connected to reality.