
Ask HN: Do you struggle to sleep after coding all day? - davidshariff
Seems whenever I&#x27;ve had a hard day fixing a bug or doing some difficult coding, I struggle to sleep at night thinking about it.<p>Anybody else suffer from the same? How are you switching off before you go to sleep?
======
jeffmould
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).

------
abfan1127
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.

~~~
WalterSear
Thinking about what I want to do the next day is a terrible idea for me.

~~~
nstart
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.

------
egeozcan
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/](https://justgetflux.com/)

~~~
vezzy-fnord
I recommend Redshift instead, which is a free software alternative:
[https://github.com/jonls/redshift](https://github.com/jonls/redshift)

------
thebigspacefuck
Anybody have those code dreams?

Those are worse than no sleep. You sleep all night feeling like you're
struggling with this mammoth problem or even worse having a breakthrough in
the problem, then wake up feeling tired and can't remember whether your
revelations were real or figments of your imagination.

Anyway, I used to have those a lot in school, when I was working towards more
solid deadlines and literally fell asleep coding. They've happened once or
twice since I've been out and working and I've learned that it's better to put
the problem down and walk away than to spend my free time or sleep time trying
to solve it. That's only going to lead to an unproductive day the next day,
when I actually have my tools in front of me.

Occasionally, I'll be watching TV after work and the solution just pops into
my head. A relaxed mind is a creative mind. Even then I won't pursue it, just
make a note to investigate tomorrow.

------
mrpoptart
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.

~~~
stephengillie
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.

~~~
wging
I have found that f.lux is not enough. Better than nothing, but not nearly as
good as avoiding screens.

------
rumblerob
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](http://www.aasmnet.org/jcsm/ViewAbstract.aspx?pid=29198))

~~~
maccard
All of the above + regular exercise.

~~~
saiya-jin
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

------
jMyles
This may seem crazy, but I have really found that the language makes a
difference.

Specifically, at least for me, any language that uses character-delimited
control structures (ie, curly brackets to begin and end a function as with
javascript or PHP) is far more haunting and sleep-stealing.

I have far less of a problem with python or ruby.

I don't know if it's some kind of spacial comfort issue or what, but I've had
many nights where I lay away seeing curly brackets on the back of my eyelids,
and it's awful.

Other things that have helped me:

* f.lux, as someone mentioned - this has more helped with eye strength, but sleep too

* martial arts (high impact exercise is a great way to change your mindset and get into a new one)

* sex, as someone else broke the ice on (sometimes at the end of a long day of coding I've got a lot of sexual energy)

* acro yoga (nice if you're pair programming because the poses are for two people)

* going for a drive (getting some earth under you is nice)

* taking some time to quietly look at the stars

------
humbertomn
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.

------
tptacek
Yes. I have a hard-stop on coding at 9PM. Of traceable causes for insomnia, #1
is "somehow managed to disable F.lux" and #2 is "for some reason was still
coding at 10PM".

------
VLM
Is anyone else going to admit sex in public?

Err ... to proactively eliminate parsing errors:

Is anyone other than me going to suggest sex as a strategy on this public
internet forum?

Unless your workplace is much more interesting than mine it is a change of
pace from day to day work, I guess it counts as light exercise, its fun and
stress reducing unless you're doin it wrong.

------
lsiunsuex
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.

------
zwieback
Definitely need some time between coding and sleeping, an hour or two minimum
for me.

When my mind jumps around I try to do focus on a single boring task, like
trying to enumerate all 50 states or think of a fruit and vegetable for every
letter of the alphabet.

~~~
justinph
I have this problem, too.

The "Alphabet Game" is something my wife taught me and it works pretty well.
The trick is to pick something that is the right level of difficulty: not too
hard and not too easy.

------
mrdrozdov
There's no silver bullet for a last minute, "Damn, this whole sleep thing
isn't working", so plan ahead. Your body has an internal clock that reacts
incredibly strongly to food and exercise. It's quite simple really. You don't
need a lot of exercise, just some basic movement throughout the day like
walking or pushups/squats if you're feeling ambitious. Make that happen before
dinner. And make sure that you don't eat too late, certainly avoid late
snacking if you can. Plus, hydrate a lot. Drink water every hour or couple
hours if you can. This should all be really easy, and the benefits compound
over time. Other tips also work like avoiding caffeine/alcohol, but what I
mentioned should be a general baseline. Habits that you should always do,
especially because some of the other tips are hard to perform consistently.

------
nstart
Having practiced 20 minutes of meditation per day since the age of 16 (I
started to learn how to lower heart rate and calm myself since I was heavily
into sports), I thankfully have no issues going to sleep. That said, things I
learnt along the way.

1) your bed is for sleeping and sleeping only. Not all of us are lucky enough
to have a separate room for work and sleeping but at the very least, don't
even think of taking work to bed. Be it laptop, or phone, your bed is sacred.
It is for relaxation only. Which is generally why using a phone before
sleeping isn't a great idea. Our phones and our activity on it have become
part of our daily routines. The whole idea of going to bed is to separate from
the rest of the day.

2) exercise. Your body can only go so far. Exercising well ensures that come
night, your mind will be telling you to just go the f __* to sleep.

3) time block yourself. Do not operate with flexible time slots. Set a
routine, and set the times. I've found that 1 hour focus time (turn off the
internet if you have to) will see yourself getting more done than if you were
to kind of free flow for 3 hours just because you have 3 hours available to
yourself. Our brains our crap at sticking to something unless given very
specific parameters to operate within, so set those boundaries. Ideally, your
time slot should end 30 minutes before bed latest. It doesn't matter if you
haven't finished the problem at that time, write a comment which has the set
of steps you want to try out, and switch off. No buts. This practice will not
only help your brain get trained to switch off, it'll also train yourself to
focus better (I guarantee the latter)

4) if you find yourself mulling over what to do tomorrow, follow what others
have said. Write your stuff down, and turn over. No googling. Nothing. Got
something to google? Write that down too. Your brain will eventually recognise
that it doesn't need to take on the overhead of tracking tasks. Or be like me.
Forget about what you have to do and wait for someone to remind you :D :D (the
latter is NOT for real. Don't try that at home or anywhere else :D )

~~~
bdcravens
> your bed is for sleeping and sleeping only

Point taken, but can we expand that list by one? ;-)

~~~
nstart
<cough> I really should have thought that line out. :D

------
burritofanatic
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.

~~~
sergiosgc
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.

------
colept
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.

------
yaronn01
For me the main thing is not how to sleep after a day of coding but how to
stop the day of coding from becoming a night of coding. It's really tempting
to slide into the night when I'm in my zone.

Otherwise f.lux is a must though it does not work on chromebook or ipad.
Nevertheless when I'm really bothered with coding the only thing that will
help is melatonin. I take it a few hours before going to bed and it ensures I
can sleep reasonably but it makes me too tired to be social late nights and
early mornings. The uvex glasses did not have a huge effect on me.

------
makmanalp
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!

------
emorning2
I've had this problem. Had trouble falling asleep, and when I did sleep I
would dream about coding. I don't have this problem anymore. I credit two
things with eliminating the problem... 1) Exercise. Preferably in the morning.
I do 20 minutes of weight training in the morning, 4 days a week. I follow the
StrongLifts 5x5 method. I walk two miles at lunch. 2) Valerian root. I just
take it every day 30 minutes before going to sleep. When my head hits the
pillow I'm asleep in 60 seconds and I sleep like a rock.

------
elevensies
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](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.

------
alistairSH
Are you working, or staring at a bright screen, right up to bedtime? That
seems to make sleep harder for me.

A few things I've found that help me: \- Exercise. I work out 8-12 hours/week.
Being physically tired makes sleep easier. \- Eat dinner >2 hours before bed
and avoid late snacks. \- No alcohol (I do have an occasional drink, but it
always impacts my sleep) \- No TV in bedroom. I either read for 30-60 minutes
or talk to my wife until we doze off.

~~~
sarwechshar
Same here, unless I'm extremely tired then sleep comes minutes after staring
at a bright screen.

When do you exercise though? I find exercise before bed makes it easier for me
to sleep but others I've spoken to seem to get too pumped up!

~~~
alistairSH
Usually right after work, so 5:30-7:30. I eat right after that. And head up to
bed around 9:45, read for an hour, and the alarm is set for 6am.

------
segmondy
Use a Kanban board, offload every single todo from your brain unto that board.
When you are done with your active tasks you can sleep in peace. If you still
find difficult, maybe you shouldn't go to bed directly after coding. Start
earlier and do other relaxing activity prior to going to sleep. The usually
blah blah blah, exercise, shower, stay away from electronics, don't drink
alcohol or coffee, etc

------
spike021
I actually find that using my phone while in bed increases my tiredness. I'm
not sure if it's related to when I was really young and I would read books
before sleeping or not, but it's interesting since a majority of people say
the opposite effect is more common.

Maybe try reading before sleeping- or something that can take your mind off
the day's activities (that's probably what winds up happening with me).

------
velavar
Nowadays I've taken to creating a to-do list before setting out to code and I
always make sure to add a couple of tasks that are somewhat simple and I'm
sure I can usually complete before bedtime. That way, even when I haven't run
through the entire list, I feel like I've gotten something done and the day
has not been completely useless. It usually takes some of the edge off any
worries I have.

~~~
sarwechshar
This is a great suggestion and hits the point about "small wins". What do you
mean however when you say simple?

------
comrh
Exercise, limiting blue light (really any light as darkness increases
melatonin production) and mindfulness meditation are all excellent tools.

------
sjones204g
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.

------
sjbase
I'd recommend experimenting with some different meditation techniques. If you
practice meditation, you'll sometimes find yourself actively try to NOT fall
asleep!

Look up the "body scan" method (it involves resting your attention on &
relaxing each individual group of muscles in your body). Puts me to sleep
without fail.

------
mzjs
Read something non-coding-related, on paper, before you go to bed, typically
for 1 - 1.5 hours. Almost always works.

------
michaelbuckbee
I read fiction, often something I've already read. This seems like enough to
move my mind into another groove.

------
dferlemann
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.

------
andor
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.

------
github-cat
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.

------
ChuckMcM
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.

------
niix
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.

------
jhwhite
For some reason thinking about what I've done helps calm me down at the end of
the day. Some of the best sleep was when I was learning javascript and laid
down while thinking about what I learned and how I wound up making it work.

------
Sindrome
I've never slept more than 3 nights in a row in the past 6 years as a
developer.

------
jquiksilver13
I have a huge problem with this. What I normally do is read a book or listen
to music before I'm trying to sleep because I find that watching tv or looking
at another screen only makes the problem worse.

------
iblaine
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.

------
zebra
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.

------
binarymax
Probably a silly question but just to rule it out - are you drinking coffee
(or anything caffeinated) throughout the day while you are coding?

------
sudhirj
I actually find that the only thing that reliably puts me to sleep when my
mind is buzzing is sex or the solo alternative.

------
ricklancee
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

------
kelukelugames
Doctor says I don't get enough exercise.

------
Jean-Philipe
I find reading a book (a physical one, not ebook) before going to bed is
calming me down and getting me to sleep better.

------
yamalight
I do 10 min exercise routine (mostly stretching and couple minutes of
meditation) before sleep - works pretty well.

------
wojo1206
Good topic, I like to listen to my favorite Folk accordion music from folks on
YouTube before going to sleep.

------
thatusertwo
I usually go for a walk after work, it helps get my mind out of the computer.

------
tshannon
exercise. It'll give you a clear mind for programming, and a good nights sleep
every night.

------
zeidrich
Stress hormones take time to leave your body, and they keep you awake. When
you are frustrated that you can't sleep, that creates stress, which makes it
harder to sleep. When you think about your stress of the day it creates stress
too.

One big thing is to keep away from caffeine. Caffeine works essentially by
slowing the rate that stress hormones dissipate.

You can't really do too much to speed up that process, so the best thing is to
stop building more stress. If you have difficulty sleeping, stop trying after
15 minutes and take a break. Do something not very exciting or engaging. Then
try again. Similarly do something that stops you thinking about your hard day,
for me that's really easy to shut off and takes practice, but for you just try
to find something that works, meditation, reading a book, writing, drawing,
walking. Just avoid things that are going to build adrenaline like strenuous
exercise or competitive video games.

The goal of the previous is to reduce the factors that keep you wakeful. The
other side of the coin is that you can increase the state that makes you seek
sleep.

Seek satisfactions. Take stock of your body, are you hungry? are you sore? are
you dehydrated? do you need to go to the bathroom? Correct these things.
They're easy, but you'll make yourself that much more comfortable, and at the
same time you'll lower stress.

Take stock of your mind. What is your mind craving? Do you want stimulation?
Do you want entertainment? Do you want to socialize? Do you want to relax?
When you can, do these things.

This is going to make you feel satisfied.

Sleep crudely works in that there are some hormones that keep you awake, and
some hormones that make you want to sleep. Wakeful factors are generally
stress-related hormones, which are one of the things that we have most control
over increasing, (it's easy to work ourselves up) but hard to decrease (they
have a half life in our bodies and can't really be forcibly purged). Then
there are restful factors that are generally satisfaction related, seratonin
which is crudely a 'things are good' kind of chemical turns into melatonin
which is crudely a sleeping factor. Others build up during the day.

Sleep happens when your pressure to sleep overcomes the stress that keeps you
awake. I think that keeping that in mind lets you stay healthy.

I think there's a few things we can do in a pinch to help directly too though,
but relying on these mean that there's something wrong that you're failing to
correct. For instance, a bit of alcohol can be a great way to break the cycle
of stress worrying about the day. This can allow your stress levels to drop
and even after it's out of your system, you're out of that cycle of worrying.
That might get you some better sleep and then you might have a less stressful
day the next day.

Similarly, you could take something like melatonin, but again that's
artificially taking the place of a feeling of satisfaction and accomplishment.
Again, if it's an occasional thing it might be helpful, but if it's covering
for the fact that you're always working overly long in an unsatisfying
environment and not getting time to yourself, it might be worth it to identify
it and work to correct that issue.

That said, everyone's different, and everyone's bodies and brains handle
things differently. For some people they might just not properly respond to a
normal amount of sleep pressure and have insomnia. For them taking supplements
might not indicate anything wrong with their environment, it might just be a
quirk of their own personal condition.

But I think we should always look at ourselves first. Not because I'm against
supplements, but because it's good to know if our bodies are telling us
something. I don't have trouble sleeping, if I go through the process of
trying to determine what I'm missing. Whether I'm craving some creative
stimulation, or some mindless entertainment or whatever. But generally when I
commit to bed I'm asleep within 15 minutes. However, sometimes I realize that
there are some things that I need to do to be satisfied that are going to keep
me up late. If I had a hard day, I know I'm going to need more time to recover
from it, and trying to force myself to go to bed early is just going to make
things worse.

