When posed with a problem, particularly when it is complicated in nature, I try to take some time to think about the approach I am going to take to solve the problem. Despite this, what often happens is, as I am programming the solution, I start to think of details of the problem that I missed, and I adjust the code accordingly.
What results is a mess of code that needs to be refactored.
I want to "refactor as I go," but while it sounds easy enough to do, I have a really hard time doing it. When the detail that I missed is small, it is tempting to make a small update to my design, rather than erase what I've already written and write it the way it is supposed to be.
It sounds like a question with an obvious answer, but are there any techniques to use to better "refactor as you go"? I know that this is a good principle, but I fail with it time and time again.