Sometimes the best way to solve a programming problem is to get away from the computer.
See, I spent several hours yesterday struggling with a problem that just didn’t want to be solved, and by the end of the day I was pretty frustrated. I was trying to add pagination to FeedDemon’s newspapers, which should have been simple, but it turned out to be trickier than I anticipated (it’s a long story).
Finally I gave up, and decided to go work out to get my head away from the computer. On the way to the gym the solution to the problem suddenly hit me – and I wasn’t aware I was even thinking about it. I sat in my car in the parking lot for a few minutes, then turned around and drove home. When I sat down at my computer, the code flowed like it was just waiting to be typed.
I know several other developers who have had similar experiences. We’ll struggle for hours on something, only to find that the solution comes to us when we’re doing something else (like sleeping).