How to code when you’re tired
In the past, I was experiencing some sleep disorders that affected my focus during waking hours. However, since I had to wait a few months before getting the treatment, I thought it would make sense to adjust my workflow temporarily.
It should be obvious that you want to fix the underlying problem, but if you don’t have a choice, I propose a few things you can try (at least temporarily).
I’m not making assumptions as to what your underlying issue is, so research that separately.
The first things that suffer from being tired are short-term memory and the willpower to ignore distractions. It is also easier to experience anxiety.
Cut feedback loops as short as possible. If there is some action that you need to wait for, see if there is a way to speed it up, e.g., by setting a temporary keyboard shortcut.
Do not multitask—close apps that are not necessary at the moment and full-screen your editor.
Turn off all non-essential notifications from your devices.
Split tasks into smaller parts. Name them and write them down. Prepare your environment for a particular task (e.g., rearrange windows or set up temporary keyboard shortcuts). Then execute the parts serially. This way, it is easy to return to the task if you take a break or get distracted.
Take breaks regularly and ask what you are doing. Make breaks more frequently than when you would without being tired. E.g., work for 25 minutes and take a 3-minute break. During the break, ask: what was I doing for the last 25 minutes? Why was I doing it? It’s too easy to get carried away by yak-shaving when you can’t remember what’s the end goal.
I also suggest experimenting with a slight variation: if you hear a timer beep, but you feel “in the zone,” skip the break, but don’t do this more than once in a row.
Write. Things. Down. Have a notepad or a tablet with a stylus. I use an iPad with Apple Pencil and Concepts app with its endless canvas feature. Draw diagrams. Make something you can look at if you get lost.
Write more code comments than you usually would. Unlike the usual comments, they are only meant as immediate, temporary help in remembering what’s left to be done in a particular code component. You can delete or edit them later if you believe that the code is expressive enough.
Plan at least 2 (sub-) tasks ahead. If I only plan 1 task, get it done and take a break, I tend to feel anxious because it’s unclear what comes next. So I define one extra step ahead so that I always have clarity about what to do next.
There are also a few somewhat weirder things that work for me, but I’m not sure if they would work for everybody:
I have syntax highlighting disabled in my editor. The color soup of a regular IDE is too distracting to me. It takes time getting used to, though.
Learn to type fast so that you don’t have to hold things in your head for a long time. It’s somewhat controversial, and a lot of people feel that learning to type fast is not worth the effort, but I do believe that being a competent typist helps save mental energy on what really matters.
Work in a well-lit room. However, some prefer the opposite.
I prefer silence or gray noise to music when I’m tired; I’m fine listening to music and code when I’m not tired, though.
For some reason, sometimes, I find it easier to work on smaller screens. I guess the brain has less to process when the amount of information on the screen is limited.
Most of these tips are applicable even when you are not tired since the short-term memory and willpower needed to ignore distractions are always a limited resource.