Coding in Flow logoGet my free React Best Practices mini course
← Blog

How to Stay Laser-Focused While Coding

Oct 25, 2022 by Florian

Featured image

This post will give you actionable tips and techniques to improve your focus as a programmer. You will be able to get more done and feel happier afterwards.

Table of Contents


No matter if you're just starting out learning to code, or already work a job as a programmer, being able to focus for extended periods of time is an important skill to get anything done. But this can be quite difficult. Not only do we live in an environment full of distractions like notification alerts and time-wasting websites, but coding itself sometimes just feels tedious and boring. This makes it even harder to stay concentrated.

First of all, there is no reason to feel guilty about this. Programming is a difficult task and our brains' natural tendency is to avoid discomfort and get away from hard work. Also, you just can't expect yourself to do intense mental work for 8 or more hours a day. Like one of my favorite thinkers, Naval Ravikant, said in one of his tweets:

Naval on Twitter: Forty hour workweeks are a relic of the Industrial Age. Knowledge workers function like athletes - train and sprint, then rest and reassess.

I'm happy when I get a good 4 hours of productive mind work in per day, like coding or writing a blog post. The rest of the day, I usually spend on tasks that require less thinking, like going through emails or simple maintenance work.

If you're just starting out learning programming, a good way to increase your motivation and ability to focus is to work on a project that you really care about. I talk more about this and other benefits of building your own projects in my How to Learn Programming Fast blog post.

But even if you are past this stage and have to work on something that's not always new and exciting, there are many things you can do to improve your ability to concentrate and your mental wellbeing. We will go through the ones that helped me the most in this blog post.

Use Timeboxing

I'm most easily distracted when I don't set clear boundaries between work and break times. As someone who works from home, I'm solely responsible for structuring my day. You might think that working whenever you want, without anyone supervising you, is the ultimate freedom. But it can easily lead to procrastination and a mingling of work and leisure time. This is not only unproductive but also bad for mental health because we never feel like we got enough done, even after working all day.

Focus requires structure. When you don't clearly separate work time from breaks it's very easy to get distracted.

If you work from home and allocate your own work time, I recommend that you set up your own schedule. This schedule will dictate when you work and when you take breaks. You can use the free Google Calendar for this. I even schedule when I go to bed to avoid postponing my sleep longer and longer every day.

This is how my schedule looks at the moment. I keep it simple and separate my day into big blocks of time:

My daily schedule in Google Calendar

I recommend working in 90-minute blocks with 20-minute breaks between them. This works really well with our natural rhythms and the breaks are long enough to feel significant.

Within the work blocks, all distractions like social media and YouTube videos are strictly forbidden. In the breaks, you can do whatever you want, including browsing the web. However, within the work blocks, you are still allowed to take breaks, but those must be boring breaks. I explain what I mean by that in the next section.

If you work on-site in a company, you might not be allowed to take 20-minute breaks whenever you want. If this is the case, you can also work in shorter bouts with smaller breaks in-between, and wait with your leisure activities until after work. You can also use a countdown timer instead of the calendar approach if you don't want fixed time blocks. 50-minute work bouts with 5-10 minute breaks work well for many. This is enough time to get something done without exhausting yourself.

Even if you work at a company with fixed times, I still recommend that you set up your own schedule for the weekends and the time after work. It's a good way to stay disciplined in your spare time.

Planning your work and break times in advance also helps tremendously with procrastination. It gives you a clear starting signal to begin working and you know exactly how long you have to keep going. If you're frustrated and want to get away from a task, it's easier to push through when you know that you only have 30 minutes left until your next break. The time blocks act as intermediate goals you can work towards, making it easier to stay motivated than focusing on one big end goal (like finishing a complex new feature). And at the end of the day, you have a good sense of how much you have actually done.

If you have trouble staying focused and motivated, try scheduling timeboxes into your calendar. It helps with procrastination by giving you a clear starting signal. And it's easier to keep going with a task when you know that you only have 20 minutes left until your next break.

When using the calendar approach, try to stick to your schedule as good as you can. Things will get in the way from time to time, like appointments. When this happens, just resume your usual schedule for the rest of that day as soon as you can.

Take Regular Boring Breaks

No matter if you use timeboxing or not, regular breaks are important to stay focused and solve problems for extended periods of time. Not only do breaks avoid exhaustion, but stepping away from a problem every once in a while is important in order to solve it. That's because it's actually your subconscious mind, and not your active thinking, that solves problems that require new and creative solutions. But for this process to be activated, you need to give your brain a break.

Most of your breaks should be short and boring breaks. A "boring break" means that you do activities that don't entertain you, distract you, or suck you in. Stand up from your desk, walk around the house, drink some water, look out of the window and/or do some pushups. Do this for 3-5 minutes and ignore the urge to check your phone or browse the internet. These short breaks are very refreshing and can make you come up with novel solutions once you get back to work. And standing up from your desk regularly is good for your health because it gets the blood flowing and prevents eye strain.

Save the entertaining activities for larger breaks that you do less often, or for the time after work.

Regular breaks are important while coding because you give your subconscious mind the opportunity to come up with novel solutions. But most of your breaks should be short and not include any entertainment or distractions. Stand up, walk around, drink some water, do pushups, etc.

If you have trouble getting yourself to take a break, setting a countdown timer every 30-45 minutes can help. Of course, a break should not interrupt an important train of thought. But if you haven't made progress on a problem for several minutes, a break will be more useful than trying to force it.

Avoid Task Switching

Whenever you switch from one task to an unrelated one, you lose some focus and efficiency. Your brain has to get used to the new context and this takes some time. You should, therefore, not switch tasks too frequently and you should avoid multitasking.

Single-tasking also increases your chance of getting into a Flow state (also called "The Zone"). It's that feeling of being completely immersed in an activity, where time just flies by and you get a lot done without feeling stressed. Flow is the holy grail of focus, but it only happens when you stay with a task for a while and build up momentum.

Of course, you should be pragmatic about task switching. If another task suddenly becomes more important than your current one, just switch. I don't think the negative effects are as devastating as some sources make it seem.

Fix Your Sleep and Routine

Your sleep and what you do at different times of the day has a huge impact on your wellbeing, motivation, and ability to focus. If you don't get these fundamentals right, no technique in the world will help you fix your lack of focus. What's worse, bad daily habits can increase feelings of depression and anxiety.

Go to bed at roughly the same time every day. The habit makes it easier to fall asleep, easier to wake up, and it gives your day structure. Avoid staying awake until after midnight, as this doesn't work well with our natural rhythm. Plan enough sleep time so that you wake up before the alarm bell rings. For most people that's between 7-9 hours.

Right after waking up, do a basic morning routine that primes you for the day. Drink a big glass of water to make up for the dehydration that naturally occurs at night. This will quickly get rid of grogginess and make you feel refreshed in the morning. Make your bed, brush your teeth, go to the toilet, and get ready for the day. Fight the urge to check social media because giving in to distractions so early in the day will set a negative tone for the rest of the day, and it wastes precious brain cycles.

You should also get a few minutes of sunlight exposure as early in the day as possible because it primes your hormones for heightened alertness, focus, and mood. It also adjusts your inner clock so it's easier to fall asleep at night. Ideally, you do a short walk, but you can even just look out of the window and towards the sun (not directly in!) for a few minutes.

Once you're in bed, reading a book is a good way to fall asleep quickly. Screens tend to make us more alert, so avoid them. Also, install a blue light blocker like Flux on your computer to naturally get tired in the evening.

Do Your Most Difficult Task in the Morning

If you get enough sleep and have a good daily routine, your motivation and ability to focus will naturally be at their highest in the morning. This is the time when it's easiest to tackle difficult tasks that require intense mental effort. If you think that you perform best at night, chances are your routine is not good and you've just gotten used to it.

Do your most difficult or most important task in the morning. Your dopamine levels and alertness are at their highest soon after waking up. The later in the day it is, the harder it becomes to stay focused and motivated.

Move your tasks that require less focus and less motivation to later in the day. Especially directly after lunch, I like to do my easiest work because that's the time when it's hard for me to get motivated and focused again. Even if you don't eat lunch you probably have a slump in the afternoon, because of how our circadian rhythm works.

Eat Well and Exercise Regularly

A good diet and regular exercise are important for health and mental well-being. If you eat mostly unhealthy foods or don't exercise, you will not be able to work at your full capacity. Your energy levels will dip, you will lack dopamine and feel-good hormones that keep you motivated, and you will accumulate distracting health problems.

A healthy diet can look boring if you're used to consuming fast food every day. But once you begin eating more healthily, your brain and taste buds readjust and you learn to enjoy simple foods again. And your mind and body will function much better.

There is a ton to say about the topic of nutrition. Here is a summary of the most important points:

Regular physical activity is not only important for physical health and looks, but also for mental health and your ability to concentrate. Exercising releases hormones that make you feel and concentrate better, it improves your sleep, and it prevents distracting physical ailments. Keeping your muscles strong also protects your brain from cognitive decline as you age.

If you want to learn more about exercising and getting in shape as a programmer, I covered everything you need to know in this blog post.

If you do a weightlifting routine (and you should), I recommend that you do it later in the day. As I mentioned earlier, our minds are naturally at their sharpest in the morning and you don't want to waste that time with strenuous physical activities. I also find it hard to get back in the groove and concentrate after doing weightlifting or other intense exercise. A short run in the morning is okay though, and it also gives you that valuable sunlight exposure in the morning.

Use Caffeine Strategically

As many of us have experienced, caffeine can provide a nice boost in motivation, mood, and concentration. However, these effects quickly wear off when you consume it every day. How many people do you know that "need their morning coffee to even function properly"? At this point, it's not about reaping the benefits of caffeine anymore, it's merely satisfying an addiction.

If you want to keep the stimulating effects of caffeine, use it not more often than 1-2 days per week. If you've been consuming it daily for a while, first quit completely for 2-3 weeks to reset the tolerance you've built up. Quitting caffeine is not as hard as some people think, I did it before and I experienced zero withdrawal effects. When you then drink your coffee or tea only occasionally, you will be surprised by how intense its effects will feel again. I drink my Yerba mate only on Sundays, and my mood and focus are always through the roof on these days.

Tip: Use caffeine not more often than 1-2 days a week. This way you don't build up a tolerance and the positive effects on mood and focus stay very intense. If you need caffeine every day, you're merely satisfying an addiction.

Improve Your Work Environment

To improve focus, it's also important that you set up your work environment correctly. And there are even some little tricks that you probably didn't know yet.

We want to avoid distractions, so mute your phone and put it away. If you use your smartphone's Do Not Disturb mode, you can silence it but allow certain numbers (like your mom) to still ring. This way you don't miss important calls.

Your desk should be tidy because a messy desk is distracting.

If you have trouble staying disciplined to not open social media and other distracting websites, you can install a website blocker. Many of them allow you to whitelist certain sub-pages so that you can still, for example, watch certain programming tutorial channels but block the rest.

Fix your screen position: Did you know that our focus and alertness are enhanced when we work with our screens at or above eye level? Here is a timestamp from the great Andrew Huberman podcast explaining this biologically hardwired phenomenon. Don't work with your laptop on your lap if you want to be fully alert and focused. Instead, put it on a stand to raise it to eye level (or use an external monitor).

A standing desk is another great way to increase alertness and focus because standing up gets the blood flowing. It's also healthier than sitting all day. It even cured my mouse shoulder, because my arm has a more natural position when I'm standing, and I can let it hang down from time to time, which is relieving. I recommend that you get an adjustable standing desk where you can alternate between sitting and standing position.

Install a video speed controller extension: Most programming tutorials can be watched at an increased playback speed, especially when you already know some of the content. This can improve your efficiency, but also your concentration because you get bored less easily. Although YouTube has an option to increase the playback speed built into its video player, an extension like this one I am using has additional benefits:

  • You can control the playback speed of any online video, including for players that don't have a speed setting built in. It even works on video ads.
  • You can increment the video playback speed in smaller steps (like 0.1x) and beyond the 2x limit of YouTube's player.
  • You can set hotkeys that are much quicker to use than clicking through the player settings.

The video speed controller extension on a YouTube video set to 2.3x playback speedThe Video Speed Controller extension gives you much more fine control over video players' speed

What Music to Listen To

If you work in a noisy environment, like an open office or an apartment with neighbors, putting something on your ears can improve your ability to concentrate drastically. Many programmers like to listen to music when they work because it drowns out the background noise and boosts motivation.

However, I find that music is always a little bit distracting and I don't like to use it unless I'm doing a very simple task. What I prefer instead these days is Brown Noise. You might be familiar with "white noise", which is that static sound you can hear on old televisions when they don't have a channel selected. Brown Noise sounds similar, but it's softer to the ears, almost like a big waterfall. It drowns out background noises very effectively, but disappears from consciousness after a while, leaving you with a feeling of silence.

I recommend that you listen to music only during your breaks. But if you must listen to music while working, use something without lyrics or distracting changes in loudness/speed. Some good genres are smooth Jazz, lofi hip hop, and video game soundtracks. Keep the volume low so it doesn't become a distraction, and use noise-canceling headphones if you can still hear the environment.


When you practice mindfulness meditation, you try to single-pointedly focus your attention on your breath for a certain period of time. When you get distracted, and you notice it, you bring your focus back to the breath. It's quite easy to see how this can also improve your focus in daily life.

I recommend that everyone does a daily meditation practice of at least 10 minutes. It has benefits for mental health that go far beyond just the ability to concentrate better. It trains your brain to get less carried away by thoughts and worries, making you more able to enjoy every moment.

Here are basic meditation instructions that you can start doing today. If you wanna go down the rabbit hole further and learn what real Buddhist meditation is about, I highly recommend reading the book The Mind Illuminated. It's a very detailed meditation manual written by a former brain scientist turned meditation teacher.


This article gave you a ton of actionable advice to improve your focus and mental health as a programmer and knowledge worker. Those are not just some random tricks that might or might not work. The techniques and tips explained here help you fix the core problems of your lack of focus: Bad structure, bad routines, and bad personal habits. Follow all the strategies I outlined above and your ability to concentrate for longer periods of time every day will improve. You will also feel happier.

Happy coding!

Tip: I send regular high-quality web development content to my free email newsletter. It's a great way to improve your skills and stay ahead of the curve.

Subscribe Now

Check out these other posts: