It must have been late when I closed the laptop. The bug was still there, intact, no closer to a fix than it had been at the start of the evening. I'd been circling it for hours, changing one condition, then another, rerunning the tests, watching the logs scroll by without anything new clicking. At some point, I stopped. Not because I'd found something. Just because there was no point in keeping at it anymore.
I went to bed with the problem still open in my head, like a tab you forget to close. I kept turning it over under the covers, the same variables, the same path through the code, until sleep took over without my even noticing.
The next morning, I went back to the screen and typed the fix almost without thinking. It passed on the first try.
It wasn't the first time. For a while now, I've made a habit of debugging right before bed rather than right before moving on to something else. I leave the problem open, I go to bed, and in the morning I test whatever solution came to me, sometimes without even remembering having consciously thought about it. In the vast majority of cases, it works.
By then, I figured it wasn't really a coincidence anymore.
I'm not the only one this happens to, and someone has even measured it. A Harvard researcher, Deirdre Barrett, ran the experiment with her students: she asked them to focus on an unsolved problem, a homework assignment they couldn't finish for example, and to keep it in mind right before falling asleep, every night for a week. The result: in about two-thirds of cases, the students had a dream that touched on the problem. In a third of cases, the dream went as far as producing a solution judged to be valid, not just by the dreamer but by an outsider who evaluated the result without knowing where it came from. According to the Wikipedia article on dream incubation
What Barrett describes has a name: dream incubation. In practice, it means deliberately focusing on a specific problem before going to sleep, in the hope that a dream will seize on it and make something of it. If the problem lends itself to an image, you're supposed to hold that image in your mind until you fall asleep. For a coding problem, that might be the screen open on the function that won't behave.
It's not a new idea. In ancient Greece and Rome, people would go to sleep in places considered sacred, hoping for a healing or divinely inspired dream. Aristotle himself mentions a Sardinian tradition of sleeping beside the tombs of heroes. According to the Wikipedia article on ritual incubation
In an old Hacker News thread from 2017, someone wrote that he'd been doing this for thirty years. He would shut down his computer several hours before sleeping, go over printed code with a red marker right before bed, and sometimes wake up in the middle of the night to jot down an idea that had just surfaced. He called it a gift rather than a problem, and said he couldn't imagine accomplishing everything he did without it. The original thread
Not everyone experiences it as a gift, mind you. One developer described a night when a particularly stubborn bug followed him into his sleep, to the point where he could no longer tell dreaming from waking. He'd wake up convinced he'd found the solution, realize he was still dreaming, fall back asleep, start over. He tells the story here
I don't know exactly what happens during those hours. Whether my brain really keeps searching in the background while I sleep, or whether it's simply that in the morning, with a rested mind, I finally see what was there all along, the thing the previous night's fatigue kept me from seeing. I keep closing the laptop with the bug still open, and sleeping on it.



