DEV Community

Cover image for Stuck on a Bug? Your Brain Solves It Better on the Edge of Sleep
Spartan Rich
Spartan Rich

Posted on

Stuck on a Bug? Your Brain Solves It Better on the Edge of Sleep

Some bugs get fixed at the keyboard. A surprising number get fixed in the shower.

You know the pattern. You stare at a failing test for two hours. Nothing. You give up, go make dinner, and halfway through chopping an onion the answer drops into your head, fully formed: the cache key doesn't include the locale. Of course it doesn't.

It feels like luck. It's closer to a mechanism. Psychologists call it incubation, and over the last few years researchers have started pinning down when it works, when it doesn't, and one strange moment where it seems to work best of all.

Edison's falling spheres

There's an old story about Thomas Edison. When he was stuck on a problem, he would sit in a chair holding small metal spheres and let himself drift off. The moment he fell asleep, his hand would relax, the spheres would clatter to the floor, and he'd wake up and write down whatever was in his head.

For about a century, that was an anecdote. Then in 2021 a team at the Paris Brain Institute, led by Célia Lacaux and Delphine Oudiette, actually tested it.

They gave 103 people a number puzzle. Each trial was a string of eight digits, and you had to grind through them with two simple rules to reach a final answer. What nobody told them: there was a hidden shortcut. The second answer you computed was always the final answer. Spot that and every trial takes a few seconds instead of a dozen.

After some practice, people got a 20-minute break. They reclined with their eyes closed, holding a light plastic bottle in one hand, while EEG tracked their brain state. If the bottle dropped, they said out loud what they'd been thinking.

Then everyone went back to the puzzle.

The 15-second sweet spot

Here's what the team reported in Science Advances:

  • People who stayed fully awake during the break: 31% later found the shortcut.
  • People who slipped into N1, the light, drifting first stage of sleep, for at least one 30-second scoring window: 83% found it.
  • People who went deeper, into N2 sleep: 14%.

That's 2.7 times more insight from barely dozing. And the benefit vanished if they went too deep. The paper's title says it plainly: "Sleep onset is a creative sweet spot."

The EEG data backed this up. The best outcomes lined up with a middle level of alpha waves (relaxed, eyes-closed wakefulness) and low delta (the slow waves of deeper sleep). Not alert. Not asleep. Somewhere in between.

And Edison's bottle? It was a decent sleep-onset detector, and it seemed to help keep people in that twilight state instead of sliding into N2. The authors are careful here: they say a control without the bottle would be needed before calling the trick itself effective.

One more detail I love. The "eureka" didn't arrive the instant people woke up. On average it came about 94 puzzle trials later, roughly half an hour after the break. The nap didn't hand them the answer. It seems to have set something up that clicked once they were back at it.

A gradient from busy waking shapes through a glowing twilight zone to calm deep sleep, with a lightbulb spark in the middle

So is walking away a real strategy?

Mostly yes, with conditions that matter a lot for how developers take breaks.

In 2009, Ut Na Sio and Thomas Ormerod pooled 117 incubation studies (3,606 participants) in Psychological Bulletin. Setting a problem aside before trying again did help on average. The effect was modest (an effect size around 0.3), not magic. But two moderators stand out:

  1. Longer preparation gave a bigger incubation effect. You have to actually wrestle with the problem first. The break works on material you've loaded into your head, not on a ticket you skimmed.
  2. Filling the break with demanding work shrank the effect. Swapping one hard problem for another is not incubation. It's just switching.

A 2012 study from Jonathan Schooler's lab at UC Santa Barbara sharpened that second point. Benjamin Baird and colleagues had 145 people list unusual uses for everyday objects, then gave them a 12-minute break doing one of three things: a demanding memory task, an easy task that let the mind wander, or simply resting. A fourth group got no break.

When people returned to the same objects, the undemanding task group improved the most. It beat demanding work, and it even beat sitting quietly. Interestingly, those people didn't report thinking about the problem more on purpose. Their minds were just free to wander.

So the shower, the dishes, the walk to get coffee. Low-effort tasks that leave room for drifting thoughts look like close to the ideal break. Opening Slack and triaging five other tickets looks like close to the worst one.

A person walking away from a laptop holding a mug, a tangled thread from their head slowly straightening back toward the screen

What this means if you write code

Some honest caveats first. Lacaux's groups weren't randomly assigned: people fell asleep or didn't on their own, and the N1 group was 24 people. Number puzzles and alternate-uses lists aren't race conditions. These are interesting, well-run studies, not proof that a nap will fix your build.

Still, the pattern across them is consistent enough to try:

  1. Earn the break. Work the bug properly first. Write down what you know, what you've ruled out, and the exact symptom. That's the "preparation" the meta-analysis says pays off.
  2. Make the break boring. Walk, wash a mug, stretch, stare out a window. Skip the hard context switch and skip the feed.
  3. Try a "bottle nap" when it's really stuck. Sit back, close your eyes, hold something that will fall when you drift, and let it drop. Write down whatever was in your head. Then go back to the code and give it a real half hour, since the click may come later.
  4. Don't nap too long. In the study, going deeper erased the benefit. This is a doze, not a siesta.

The part nobody mentions

Incubation can only recombine what's already in your head. If the thing you needed (that blog post on cache keys, the talk on locale bugs) went in one ear and out the other, there's nothing for your drifting brain to connect.

That's one reason I built Recl. I save the talks and articles I learn from, it pulls out the key takeaways, and it brings them back later as quick recall questions on a spreading schedule. It's not a bug-fixer. It just helps more of what I read stay around long enough to show up in the shower.

Recl's Recall tab, showing saves scheduled to come back on Day 1, Day 3 and Day 7

Next time you're stuck, don't push for another hour. Load the problem in fully, then go do something dull. Your brain may not be done with it yet.


Asutosh writes about memory and how brains learn, and builds Recl, an app for remembering what you watch and read.

References

  • Lacaux, C., Andrillon, T., Bastoul, C., Idir, Y., Fonteix-Galet, A., Arnulf, I., & Oudiette, D. (2021). Sleep onset is a creative sweet spot. Science Advances, 7(50), eabj5866. https://www.science.org/doi/10.1126/sciadv.abj5866
  • Sio, U. N., & Ormerod, T. C. (2009). Does incubation enhance problem solving? A meta-analytic review. Psychological Bulletin, 135(1), 94–120. https://pubmed.ncbi.nlm.nih.gov/19210055/
  • Baird, B., Smallwood, J., Mrazek, M. D., Kam, J. W. Y., Franklin, M. S., & Schooler, J. W. (2012). Inspired by distraction: Mind wandering facilitates creative incubation. Psychological Science, 23(10), 1117–1122. https://pubmed.ncbi.nlm.nih.gov/22941876/

Top comments (0)