In the zone
Now, here's a thing I've noticed of late. When I'm programming I sometimes get to a point I refer to as "living there". This is when all the bits and pieces which make up the program are sitting in my head, and I can think about the various components and how they interact without having to consciously remember where they all are. It often takes me a while before I'm "living there", and also a little while to get out again (and I'm not particularly good company when I'm there either). All well and good.
But recently I've noticed that if I move towards the door of "there", and then come back, I can often improve on my work. Normally speaking I solve a problem, code it up, and move on to the next problem. However, I've noticed that once I have found a workable solution it often pays me to walk away from the problem for a little while (say a teabreak's worth) and then go back. The act of re-clarifying the idea often leads me to an even better one. This is particularly valid if I'm fixing a bug. Rather than apply a fix I now tend to go off and spend 15 minutes doing something else. Then I'll think about the fix again, and often find a couple of ways in which it could be improved.
So, out of all this a bit of advice. Don't be afraid to walk away from a problem for a little while, even if things are going well. Of course, if things are going badly you really should walk away for a while, otherwise you'll go mad.