The other day I was working on a code project and ran into a frustrating bug. I looked at it for at least an hour and got nowhere with it. I used breakpoints, debugging code, and scanned though the listing again and again. No joy. I had to leave for a meeting and so put it aside for several hours. When I got home that night I decided to take another look and within five minutes I found the problem. It was in a section of code that somehow I had neglected to look it. I was too close to the problem earlier. This happens a lot.
Somehow I think that after a break we return and our brain has done some processing we are not aware of. Or perhaps after a break we use a different part of our brain to get back into the problem and that results in a new view of the code. I’m not at all sure other than something changes when you put code aside and come back later.
I see a different sort of view change working with others. Very often a student will call me over to help them with a problem. As they are explaining the problem and what the code is doing they will often suddenly blurt out “never mind. I see it now.” Something about explaining the issue to someone else changes how we look at the problem. Sometimes that is enough to help us see a solution that was eluding us before. I should say that I saw this frequently working as and with professional developers over the years. It is not unique to beginners by any means. It’s probably common in all problem solving situations.
Clearly though sometimes we need to take a mental break from one problem so that we can find a different approach or a second set of eyes. Or a third set.
The question for me becomes how to teach students to find different views into their work. Pairs programming is one possibility especially as it forces communication. Requesting that students ask peers to review code maybe. Or even asking students to describe in regular works what is going on. Or maybe just showing it by example. Something to ponder this weekend I guess.