What Is Context Switching and How Does It Demotivate Programmers?

Many good articles and blog posts have been written on how to keep yourself motivated as a software developer. Simple Programmer alone has many.

Various effective techniques have been proposed by different authors, techniques ranging from turning coding practice into a game to reminding yourself that your work matters.

But just as some techniques and circumstances can motivate us, there are also things that we regularly do to demotivate ourselves. And unless we stop doing the things that demotivate us, the methods that are intended to motivate us will not be as effective.

Here’s the issue: While motivational actions need to be implemented consciously, the actions that actively kill motivation are often done without thinking.

One of the biggest (if not the biggest) activities that destroy motivation in the world of programming is multitasking, or rather one particular aspect of it: context switching.

What Is Context Switching?

Pure multitasking doesn’t exist. It is impossible to perform two mentally challenging tasks at the same time. Therefore, when we multitask, what we really do is constantly switch from one task to another. That’s what context switching is.

The term originates from computer science. However, it is just as applicable to mental tasks performed by humans. After all, the human mind is similar to a CPU in many ways.

Just as a CPU running multithreaded processes temporarily puts on hold an execution of a given thread while running another one, the human mind puts one task on hold in order to shift its focus to another one.

Why Is Context Switching So Bad?

As Scott Hanselman once said: “The optimal number of threads in any system is one thread.”

Hanselman is well-known in his leadership position at Microsoft, but he also writes code and blogs, teaches programming, and travels the world to give talks. As he does so many different things on a regular basis, none of which are easy, you would think that he multitasks a lot. Well, he doesn’t.

What he does instead is schedule his day to include periods of time when he can apply the maximum amount of focus to a single task. He also doesn’t do work-related stuff while he is with his family, and he strongly advocates saying no to tasks without feeling guilty about it when you feel overwhelmed.

The key reason context switching is bad is because it takes time and effort to get into focus. So every time we switch tasks, we lose energy that we wouldn’t have lost if we had just stayed on one task. And the fatigue that builds up from all of this energy loss is what heavily demotivates us, as well as potentially causing a mental burnout.

This is also the reason programmers often feel completely exhausted by the time they finish work even when they haven’t done anything hugely difficult that day. They blame insufficient sleep, an unhealthy diet, too much caffeine, or other factors. Any of those factors can contribute to fatigue, but constantly shifting focus contributes to it a lot.

Fatigue is not the only problem that arises from context switching. Productivity suffers, too.

To perform a mentally challenging task, such as programming at the optimal level, one needs to reach a state of flow, where complete focus is given to one activity. This is not easy. It takes the average person around 15 minutes of uninterrupted work to reach this state.

A person who is repeatedly distracted or constantly tries to perform several tasks at the same time is unlikely to reach this productive mental state even once during a typical workday. Therefore, they are quite likely to be outperformed by someone who has learned to master the art of focus.

Consumer Tech Doesn’t Make It Easy

Getting distracted by technology is one of the most common types of context switching. This includes habitually checking your phone, visiting social media, or reacting to any notifications from any of your devices.

What makes this habit widespread and difficult to shake is the fact that consumer technology has been deliberately engineered to be addictive. This was done primarily to keep users accessing the content so they would stumble upon as much commercial material as possible.

Features like infinite scroll and pending message animation were specially designed to keep the users engaged. Both of these are examples of strong dopamine triggers.

Dopamine is the main hormone of anticipation, and as such, it is capable of getting a strong hold on people. So once we’re distracted by social media, we have a terrible time switching back to a productive task.

Regardless of how fulfilling and interesting your work is, it still consists of activities that require you to make an effort to keep focused. And browsing social media requires no effort at all. So you experience fatigue once when you fall into the temptation to check your feed and experience additional fatigue later when you have to switch your focus from an easy and addictive task back to something that is difficult and unexciting in comparison.

So resist the temptation to check your phone while you’re engaged in a productive task: It’ll be easier than switching back to the productive task once you give into the temptation.

Finally, no matter how motivated and driven you felt before you started work, all those powerful dopamine triggers can make your work seem very dull. This is guaranteed to substantially reduce, or even completely kill, any motivation you had.

Counterproductive Productivity Hacks

There is another type of activity that looks extremely productive on the surface but is a real demotivator: performing activities that are productive but unrelated to your current task while waiting for external processes to complete.

For example, code often takes a while to compile, especially when a continuous integration system is used. So developers may use the time to read a programming blog, practice a new technology, or put some effort into fixing a bug in a different part of the system.

But, as before, the programmer is context switching even if there is currently nothing they can do related to their main task. This is especially true in situations where a build wouldn’t take more than a half-hour to complete, which would mean they would have to switch back to the original task before completing any meaningful amount of work on the other task.

Once the code compilation is complete, the programmer who initiated the process will need to switch back to that part of application. So the programmer is unlikely to have been able to fully immerse themselves in the other task before having to switch back to the previous one.

The drawbacks of this are obvious. If the other task involved reading technical articles or documentation, it’s likely that not much of the information would have been retained. If it was related to a different part of the system, chances are it would have been left incomplete, making it harder to pick up later.

The potential for these issues to occur gets larger every time you switch tasks because every activity that forces you to change your focus decreases your mental energy.

Being mentally exhausted is an unpleasant feeling, and if you experience it too often, you will eventually start to associate it with being a programmer, which will demotivate you from doing your job. So, however strong your enthusiasm for programming was to start with, there may be nothing left of it after a few years if you try to overoptimize your job.

So, while utilizing idle time this way may seem efficient, it often isn’t. Not only are you unlikely to complete all the tasks in the best possible way, but you may also kill your motivation altogether.

How to Stop Context Switching

So what do we do with our downtime? When you come to a point in your work where you have to wait before continuing with your current task, you can still remain in the state of flow.

You can dedicate the waiting time to planning the next steps in your current task. Planning is something programmers don’t tend to do enough anyway due to a perceived lack of time, but proper planning makes software so much better than just building on top of a prototype.

By the time you can continue work on your main task, you will have a clear idea of what to do next. And the switch between different types of activities will be gradual and flow more naturally.

Another productive thing you can do without breaking your state of flow is to do some learning that is directly related to parts of the system that you were working on.

For example, you may have some asynchronous code to work on. If you have only a vague understanding of how it works, this would be a good time to update your knowledge. Same applies for any other technologies or programming language features you were using.

Discipline Your Mind

Even though not getting distracted while you are surrounded by technology is hard, there are a number of reliable ways to prevent it.

A well-researched, long-term solution is mindfulness, which is both the ability to be fully mentally present in the moment and a set of exercises that helps to achieve this state. In the short term, mindfulness may not yield any benefits at all and may even seem tedious. However, if practiced regularly, it will eventually discipline your mind and will help you to reject any distracting or otherwise unhelpful thoughts.

Although mindfulness is often associated with meditation, these two things are not necessarily the same. Any activity where you place yourself firmly in the moment is mindfulness, and for people who don’t like to sit in one spot for 20 minutes while paying attention to their breathing, it may include running, swimming, or playing a musical instrument. Meditation just happens to be the purest and the most researched way of practicing mindfulness.

Once your mind is disciplined enough, you will be able to easily enter the state of flow and perform deep work, where you can keep your concentration up for hours on end.

In the short term, the Pomodoro technique is something that can be utilized effectively. The method consists of purely performing work-related tasks for a set period of time and then having a short break before starting the next round.

It is much easier not to get distracted when you know that you only need to concentrate for a relatively short period of time and will be able to do something fun afterward.

The actual durations vary depending on whom you ask. Some say 20-minute work intervals with 10-minute breaks work the best, while others would tell you 50-minute intervals with 15-minute breaks are more optimal for them.

I personally find one-hour work intervals with 15-minute breaks work best for me. It’s a good idea to find out what works for you through trial and error.

One thing to note about the Pomodoro technique is that although the idea is to not get distracted during set time intervals, it may still happen now and again. To make the technique effective, you can record how many times you got distracted during each round.

What gets measured gets improved, so you may find that over time the numbers will go down to zero and stay there. This is one of the things Scott Hanselman suggested, and it’s worked for me.

Get Context Switching Out of Your Life

We talked about how context switching is bad for productivity, energy levels, and motivation while you are at work and what to do to minimize it. The good news is that the same principles can also be applied to other areas of your life.

Context switching drains your energy regardless of the context it’s applied under. Your life satisfaction will go up as the amount of context switching in your life goes down.

The first step is to minimize the number of interruptions during your natural flow of activities. After that, you can get into the habit of making a gradual transition between different activities rather than mindlessly following to-do lists of unrelated but important tasks.

We programmers are good at building software. But how about building a fulfilling life for yourself? Perhaps engineering your work life in a way that all activities flow naturally is a good place to start.