Ask HN: Do you struggle to sleep after coding all day?

53 points by davidshariff ↗ HN
Seems whenever I've had a hard day fixing a bug or doing some difficult coding, I struggle to sleep at night thinking about it.

Anybody else suffer from the same? How are you switching off before you go to sleep?

76 comments

[ 2.9 ms ] story [ 152 ms ] thread
Sounds normal - your mind is still working on the problem.

I've been taking a melatonin supplement lately - it's helped. Cut back on your coffee intake if you drink coffee and try to work out for 10-20 minutes, even if it's just going for a walk or a light bike ride.

I have a somewhat reverse problem. Some days I struggle to through the day coding, even on routine or easier coding tasks. When this happens find myself daydreaming or finding other things to do that are not work related. The real problem comes though at about 10-11pm and I get a huge burst of energy and focus to start working. I am left with a choice to stay up later and get work done, or go to bed and wake up earlier to catch up on work I missed. If I do decide to go to bed I will lay there for an hour or so just thinking about work and what I need to get done.

With that said, last year I was talking to my doctor about it at my annual physical. He suggested I exercise more often. So for about the last 6-7 months I have made a conscious effort that whenever I feel like I am starting to struggle I will go out for at least a 30 minute walk. I usually end up taking about 4-5 a day. There is definitely a direct correlation between the amount I walk and my productivity. And I am starting to sleep a whole lot better at night. Another thing that has helped me is I stopped drinking soda and now only drink water (or coffee).

Doctor says I don't get enough exercise.
Whenever I get the urge to exercise, I just lay down for a little bit until it goes away.
Yes! Not just coding, but I have been struggling to turn my mind off from work to sleep. I think about the new feature I'm coding, about new ideas on how to reach out to more users, etc... I really felt it started to become a serious problem in the last months, so I then cut my evening coffee (only have it in the morning now) and started to swim/gym around 6pm.. But the main thing is not to use your laptop as the last thing you do before sleeping... Even if you're working late, get rid of it and do something else for 40 minutes before trying to sleep.
Read something non-coding-related, on paper, before you go to bed, typically for 1 - 1.5 hours. Almost always works.
Exercise, limiting blue light (really any light as darkness increases melatonin production) and mindfulness meditation are all excellent tools.
I have this problem as well and can agree with other commenters that physical exercise seems to help. In addition to not getting enough sleep, I often dream about programming and my mind is reeling all night trying to solve some issue.
Yes, I have that same issue. Generally I avoid stimulants before bedtime and sometimes will read the news or some interesting papers before bed. I've also use meditation to quiet all the random thoughts in my head to make sleep easier.
When i can't sleep I usually watch or read something in bed on my tablet until i can't keep my eyes open anymore
Every time when I am going through this, I will choose something to do something which needs no thinking. For example, watch some comics. This will redirect my attention to something has nothing to do with the hard thing.

Apparently currently I am doing this.

Sleeping takes practice like any other skill. If your bedtime is erratic, if your sleep pattern is erratic, if your environment is erratic, your brain isn't sure when it's time to go to bed.

Pick a time for a week and go to bed then regardless of anything else. Try to limit light exposure for the hour before sleeping (no phones in bed) and see if that helps. The more consistent you can be, the more consistent your sleep will be.

I hate the "no phones in bed" rule, and literally everything I do is PC-based or phone-based, so I stare at one screen or another right until I sleep. The f.lux and Twilight (Android) apps have been very helpful at making me feel more naturally tired when I should be.

Additionally, the Sleep as Android app makes for a great way to track and log sleeping patterns, and has a nice adaptive alarm.

I have found that f.lux is not enough. Better than nothing, but not nearly as good as avoiding screens.
That's interesting because I tend to use devices in bed too but find my sleep time varies. I'll give the f.lux and Twilight apps a go!
"I hate the "no phones in bed" rule, and literally everything I do is PC-based or phone-based, so I stare at one screen or another right until I sleep."

If you have trouble sleeping then it is a very good rule.

I often struggled to sleep, I would sometimes lie in bed from 2AM to 6AM, only to fall asleep after sunrise, get a couple hours and then limp into work around 10.

Now I just take Melatonin in the evening ( http://www.gwern.net/Melatonin ) and sleep somewhat normally.

EDIT: I also use the flux app, and I reduced my coffee consumption significantly, now down to zero, but melatonin had the quickest and most observable effect.

I had the same problem. Using f.lux[0] and writing down all the problems (programming related or not) that occupy my thoughts on a notepad which lives next to my bed, just before going to sleep totally eliminated the issue.

[0]: https://justgetflux.com/

Make sure that you install it on all your devices. I used to read late night on my iPad, and it helped me a lot.
I force myself to quit working at 12AM nightly. That'll give me an hour to watch TV before I hit the sack. It's worked well for me.
When I find my mind won't settle down, I'll do 1 of 2 things. If I'm concerned about a list of actions I need to do the next day, I'll write down the list on paper. This allows me to relax and know I won't forget anything. Otherwise, I'll write in my journal about what ever my thoughts are. This allows me to slow my brain down the my writing speed. It lets me process my thoughts. Its helped immensely.
As well as the list approach, actually say loud to yourself "I will solve thhe unfinished problems tomorrow, tonight I will relax". Give yourself permission to relax!
Thinking about what I want to do the next day is a terrible idea for me.
Writing it on paper helps though. Generally it's a bad idea to let tomorrow's actions cycle through your brain endlessly. Writing it on paper has this effect of transferring the mental responsibility to another medium. Instant relaxation. As always, your mileage may vary.
I've done similar things with Org-mode. I particularly like Org-mode because I can view all of the tasks I've written in my agenda and know I won't lose anything.
I read fiction, often something I've already read. This seems like enough to move my mind into another groove.
Last night for me was particularly tough. Coded essentially all day, struggled with a bug for hours, and was still on a caffeine high past midnight. The game plan for me should just be to wake up even earlier and wear myself out through more exercise.
Caffeine destroys my sleep. It didn't use to, but since I dialed down my caffeine intake, I'm more sensitive to it. If I forget and have a coffee after dinner, I'm done for and won't fall asleep before 2AM-3AM.

At least I know the pattern. A problem without solution is already solved: I just pick up a good book and wait it out, knowing I'll be sleepy the next day.

Happened to me last night

Worked 6am to 2pm at my day job. Came home, ate dinner (late lunch) took a 2 hour nap. Went to Starbucks and worked for 3 hours on my side business and had 2 grande coffees - got home around 9pm and couldn't fell asleep until midnight - one of those sleeps where your not really sure if you actually feel asleep.

Coffee, especially strong coffee, can destroy you late at night.

Probably a silly question but just to rule it out - are you drinking coffee (or anything caffeinated) throughout the day while you are coding?
1. Write down if there are more steps to do. 2. Watch a BBC documentary about the cosmos or something narrated by David Attenborough. Without advertisements.
I've tried melatonin, cutting out alcohol, f.lux, not looking at a screen for an hour before bed, but the one thing that has had the most influence on my sleep (for the better) is cutting out caffeine after 4pm. (relevant study: http://www.aasmnet.org/jcsm/ViewAbstract.aspx?pid=29198)
All of the above + regular exercise.
this. if you race your mind whole day in highest gear, tired body helps a lot to drag it into abyss of good old hard sleep. plus gazillion other positive effects, if all is done right
Works for me too, combined with a consistent bedtime.
melatonin supplements only go so far. f.lux is indeed awesome. 4pm seems a bit late for me but he has a good point. Also exercise helps a lot ! unfortunately, it also requires the most effort.
Cut out caffeine a minimum 6 hours before one sleeps. Fun factoid: caffeine's half life is around 6 hours, and by then one is pretty much over all of its effects.
I find physical exercise is just as important to mental exercise. Even if I am spent, mentally, there's still a craving to spend physical energy or else I'll be restless. So I run after work and have found it much easier to sleep.
I find that if I'm having trouble sleeping because my mind is jumping all over the place, intentionally taking time aside before sleeping to clear my head helps a lot. I just sit on the floor, examine my current thoughts, thing about them a little and write down important ones for a few minutes, then decide to stop. At this point I just practice clearing my mind for a few minutes. Thought popped back into your head? That's fine, realize it happened, and try again. Meditation, mindfulness, whatever you call it - works well for me!
A fellow coder friend of mine leaves smartphone and all electronic devices outside the bedroom door before sleep. Also, spend 1 hour before bed doing something like reading or chores like cutting up onions might help as well. Just imagine onions are more important than those bugs at that moment helps.
If I'm having sleep problems and feeling ambitious then I'll go to the gym. If I'm having sleep problems and I'm lazy then I'll take half an Advil PM.
Running or swimming for >30 minutes in the evening helps, if you can relax your mind doing so. For example, go running in a forest or a park, not in the streets where you need to watch traffic. Try to concentrate on your running or swimming technique.