One of the goals we have as programmers is to write code that does not have any bugs. However; since none of us are perfect, we are going to fail. Does this mean we should stop trying? Of course not.
So how do we go about trying to reach this impossible goal? The quickest and easiest way I can know of is to make your code as readable as possible, not just to yourself but more importantly to the person that is going to be coming back to fix the bugs.
So how do I make my code more readable? You must first establish what your coding standard is, and then you need to practice the art of refactoring. Combine this with smart testing and some forethought and you will be on your way to easier, more efficient code writing.