The Sleepy Developer: How Sleep Affects Your Code
Published: 2021-03-29 20:19:40

Consider the stereotype of the elite programmer. Maybe you see someone sitting in front of a bank of monitors, staring at full-screen terminals, in a dimly lit bedroom, at 3am, slamming down Mountain Dew from a 7-Eleven Big Gulp while pounding furiously at a keyboard.

What's reality? Is the caffeine-fueled, elite "hacker" mindset an efficient way to code? How does this person stack against the science of sleep? Let's talk about sleep.

## You Can Be Elite Without Burning the Midnight Oil

To operate at an elite level for our bodies, we need to understand the role of sleep in life. Sleep is something that we all engage in regularly, yet many of us learn very little about it and what it ***really*** does to the body and mind. We'll begin to understand sleep (and the cost of ignoring it) by looking at it through the lens of a totally different profession, the practice of medicine.

Would you like a drunk doctor to operate on you? Of course not! How about a doctor who is still awake after working for more than 24 hours straight? Maybe that doesn't seem so bad until you learn that the two hypothetical doctors could be functioning at the same mental alertness level.

Unfortunately, at least in many U.S. hospital systems, medical residence programs have new doctors working in some of these more extreme environments. These tired doctors make medical errors that can lead to the deaths of patients. This is likely a factor in why medical errors are the [third leading cause of death](https://hub.jhu.edu/2016/05/03/medical-errors-third-leading-cause-of-death/) in the U.S. (behind heart attacks and cancer).

Who can we thank for this terrible statistic? The healthcare system is full of problems, but the extreme working hours of new doctors stem from William Halsted, MD, from his time at Johns Hopkins Hospital. Halsted trained new surgical doctors and pushed them to their wakeful limits in pursuit of their craft. This practice spread in other programs until it became normal for residents (so named because they essentially **lived** at a hospital) to work excessive hours.

Halsted was able to foist this kind of behavior on new doctors since he seemed to require no sleep and was the model of a brilliant doctor doing amazing work. Sadly, history reveals that Halsted was ***secretly a cocaine addict*** *that was using his highs to stimulate himself to alertness.* Today, many doctors are pushed beyond reasonable limits, to the cost of public good, because of a drug addict who dramatically impacted the history of an entire profession.

Lack of sleep impacts the ability of doctors to do their jobs and can impact software developers from working at elite levels too, but our profession is not driven to sleeplessness by a cocaine addict from more than 100 years ago. It can be so hard to get a good night of sleep. Why? Let's look at elements that hinder many developers from some good shut-eye.

### Caffeine

The most commonly used stimulant in the world is no stranger to developers. The trouble with caffeine is that its effects last far longer than we might anticipate.

Caffeine works by interfering with adenosine receptors in the brain. Adenosine is a protein that accumulates in our brains while we're awake. When adenosine is able to do its job, the elevated levels late in the day cause you to get drowsy and want to sleep. Since caffeine blocks adenosine, the presence of the stimulant in your body can prevent easy sleep.

The bad news is that caffeine has a long half life of 3 to 7 hours for an average adult. In other words, a late afternoon cup of coffee at 3pm will still have half the caffeine left in you at around 8pm. That caffeine is going to keep you up.

Sadly (for *this* coffee lover, at least), the best option for getting better sleep if you're consuming too much caffeine is to cut back or cut off the later part of the day entirely.

### Light, Specifically the Blue Variety

We are biologically wired for increased wakefulness with light around. When an excess of light is present, the brain delays the release of melatonin which is a critical component in triggering the onset of sleep. The modern world is surrounded with artificial light at night that can hinder your ability to sleep.

It's not only light that can impact your sleep. **The color of the light matters!** Studies show that blue light has a stronger suppressing effect on melatonin release than other light colors. This was less of a problem when incandescent bulbs dominated, however, the rise of LED lights increases the amount of blue that we experience at night. LED "white" is really just equal parts of RGB (Red, Green, and **Blue**). If you are using a computer at night to code without some filtering on the blue spectrum, then nighttime light may be holding you back from better sleep.

### Too Much Work

In many of the health surveys that I sent to DC area developers, individuals reported they are stressed by work. A taxing work environment can be a negative feedback loop on your sleep health. Stress and mental anxiety about work can lead to sleeplessness. Fatigue from lack of sleep can make completing work tasks more difficult or time consuming. The cycle can perpetuate.

How do we get so much work?

 * Some work cultures incentivize behavior that keeps people working longer hours (for example, always available free meals).
 * Some workplaces have developers on a death-march pace to complete projects before certain deadlines.
 * Some of us work a "second day job" as maintainers of open source projects that place demands on our time.
 * Some developers are seeking jobs. They are either doing extra training to gain the skills to break into the industry or are doing work projects that come along with many interview processes.

### So What?

Does it matter if we're tired? Does it *really* have an impact on the quality of our work and code? In short, yes, it does. Missing sleep [impacts your ability to retain new memories](https://www.jstor.org/stable/1414040?seq=1#page_scan_tab_contents). A lack of sleep leads to cognitive impairment (like our medical mistakes example above or the [data surrounding the strong relationship between little sleep and car crashes](http://aaafoundation.org/acute-sleep-deprivation-risk-motor-vehicle-crash-involvement/)). Sleepy workers account for [huge amounts of global economic loss](https://www.rand.org/pubs/research_reports/RR1791.html). These are all indicators that people need good amounts of sleep to function optimally.

> If you want to code at your best, you need rest.

## Write Your Best Code

Now let's focus on some concrete things you can do to move yourself toward a better night sleep. You can produce your best code when rested.

### Decaffeinate Part of Your Day

Cutting back on caffeine in the second half of your day will combat the physiological effects that make it harder to sleep. What are the food and drink that are the biggest typical sources of caffeine?

 * Coffee, nectar of life 😄
 * Soda
 * Tea
 * Chocolate
 * Energy drinks

This post won't comment on the health aspects of the items above, but the common denominator of caffeine make these things to watch out for later in the day.

Maybe you use caffeine deliberately as a tool to stay awake. This could be a sign of a sleep deficit from not getting enough sleep and is probably not a great long term strategy. Latent caffeine left in your system can impair the quality of your sleep and lead to a cycle of fatigue while awake.

Note that we're not suggesting that you cut out all caffeine from your life. Rather, this section highlights a need to be conscientious about when and how much caffeine you consume.

### Leave the Blue Light Specials to Kmart

Rather than blame Thomas Edison or Nick Holonyak (the inventor of the LED) for sleep woes, think about what your bedroom is like and the possibility that sleep trouble might be your own doing.

 * Does your room have black out shades to keep external light sources out?
 * Is there a television (or screen of any kind) on shortly before bed?
 * What about the humble alarm clock? Is it back lit and casting light into your room?

Getting light out of the bedroom late at night will likely help improve your sleep. What can we do in the early evening to send better sleep stimulating signals to our brains? We noted earlier that blue light is a thorny color keeping people awake. If you're looking at your computer while it's pumping out blue light, you're delaying the signals telling you to get some sleep.

One way to suppress blue light is with blue light filtering software like [f.lux](https://justgetflux.com/). f.lux and tools like it will change the color temperature of your computer screen depending on the time of day. As night progresses, these tools turn down the blue light. Your screen will change to have an amber hue over everything. This amber tone increases as the night passes which hopefully helps remind you to get some sleep.

The less technical solution to f.lux is to get off of your computer or other blue light emitting devices at night. This can be hard, for sure, but if sleeplessness is kicking your butt, it's worth a try.

### Get Sleep Educated

This post really only scratches the surface of all the data out there about sleep, its benefits, and the ramifications of not getting enough of it. If you want a fantastic resource about sleep, I strongly recommend Matthew Walker's *Why We Sleep*.

![Why We Sleep by Matthew Walker, PhD](https://www.mattlayman.com/img/2019/why-we-sleep.jpg)


Dr. Walker, a professor of neuroscience and psychology at UC Berkeley, filled the pages of his book with interesting stories, clear explanations from the science of sleep, and a mountain of data pointing to much of the influential sleep research. A number of the sources in this post were cited and explored in the book in greater detail.

To give you an idea of what you'd learn, here's a peek into the content. *Why We Sleep* is broken down into four main parts:
 1. A section about sleep and what it is 2. A section answering "Why Should You Sleep?" 3. Another section that explores dreams and their function 4. A final section looking at sleep in the context of society

The book is very accessible to readers from all backgrounds. It is also available in audio format from services like Audible and is definitely worth your attention. (To be clear, this is in no way a paid endorsement, I just think that *Why We Sleep* is awesome!)

We've looked at sleep and showed how important it is by observing the cost on society when doctors don't get enough of it. Then we saw some of the leading ways that developers can get hung up and not get enough sleep. Hopefully, I've increased your awareness of sleep's effects on your body. With the tips provided, I think you can get more and better sleep. Get rested and write your best code!

*Note: This post first appeared on a website called Full Health Developer which I ran as a side project. It's good information (IMO) that I didn't want to lose when I shut down Full Health Developer.*