Even after so many years coding I usually fall into the same trap: the elation of finally finding the solution to hours of frustration drives me to keep on coding; without the reward of rest and reflection my foggy brain inevitably hits another problem, and I get frustrated and demotivated. It's only then that I finally take a break, and it doesn't feel nearly so good as it would've.
One day I'll learn from this.