Ask HN: I lost my ability to focus for hours on coding. How to regain?

142 points by codesternews ↗ HN
I lost my ability to focus. I check social media and youtube frequently while doing work. If I do not do that I feel some void and feeling of losing out(I do not know how to express this).

How to regain my focus and avoid this trap?

126 comments

[ 3.6 ms ] story [ 185 ms ] thread
I had that issue two months ago. I digged into my calendar and found out that's when I stopped going to the swimming pool, doing enough physical exercise.

It's old knowledge but : "A Healthy mind needs a healthy body" stays true.

I tried exercise, and now that's all I want to do. Ugh...
I know that feeling; it's addictive, because it feels so good. Make sure to take rest days, otherwise you might injure yourself from not giving the muscles and tendons enough time to repair.
Social Media Trap -- I installed the Leechblock extension for Firefox which helped me a bit. Also going to special places like silent libraries is helpful for me to concentrate. As final escape, think about going offline. Documentation can be downloaded.
This may sound stupid or trite, but have you considered moving into a management position?

Social media and Youtube may be stealing your attention because you simply enjoy them more. If coding was more enjoyable, you'd be doing more coding at the expense of those distractions.

But maybe you just don't enjoy coding your current project. A lot of projects are not intellectually or morally stimulating, and it's hard to stay excited about them, particularly if your work culture is the least bit toxic.

Being a manager means dealing with people, and a lot of experienced coders actually get more out of that than out of JIRA-code-Git-repeat.

It might be surprising, but dealing with people still requires focus :) Also, instead of code, you write docs - so many docs. Which, again, needs focus.
I think the comment was more about the fact that social media is more enjoyable and therefore drawing OPs attention. If they moved into management, perhaps that would be more enjoyable and therefore remove the social media draw. They could have used any occupation not just management. Maybe OP just doesn't enjoy coding anymore.
Use tools such as SelfControl.app, ColdTurkey.app, and Freedom to block websites.

Some people will claim that is a crutch. Thats a great analogy because crutches help you get things done while you heal.

Focus is another one I really like on the Mac - it replaces websites with a different quotation each time:

https://heyfocus.com/

On Windows, I add websites to the hosts file as others have suggested: HN and Google News are the main ones, I gave up FB long ago.

Doing this still doesn't get you back on track coding / focusing, but it helps. The idea is to break your habit of visiting distracting sites - where these apps help - but also build a habit of work. The more you make it a habit, the easier it becomes over time.

  echo "127.0.0.1 facebook.com twitter.com ycombinator.com youtube.com" >> /etc/hosts
(comment deleted)
Windows Edition (admin command prompt):

echo 127.0.0.1 facebook.com twitter.com ycombinator.com youtube.com>>"%WINDIR%\System32\drivers\etc\hosts"

The real trap is feeling like you need to be focused eight hours a day when it's not strictly needed.

There's an occasional crunch where I have to heads down get something done, but in that case there is actual motivation to be heads down and get things done.

I find a due date way more motivating than a mere thought of "I should be working...".

Overall, if I'm not already focused in, it's because it's something not as important as other things in my life.

Regarding the "some void and feeling of losing out(I do not know how to express this)" - "The Willpower Instinct" is a book that covers that exact feelings and how to address it. Unfortunately I am too lazy to find the exact location in the book for you.
Watch more porn, or get laid more.

I was having focus issues, because coding is fascinating but not inherently sexy to me. After working on a problem for a while, the enjoyment of the problem would become overshadowed by my sex drive, and I would start to find anything else more interesting. Ended up being my body's way of reminding me that I should make some babies before I died, so I was subconsciously distracting myself away from anything fascinating that wasn't the opposite sex.

Realize that promiscuity is a disease vector.
The OP was crude and perhaps hinging on taboo, but there are well established schools of psychology on the topic, namely Freud's psychoanalytic theories come to mind.
Getting laid is crude now? Man, I thought it was 2019, not 1950. Excuse my crude language.
I'm actually really curious why you think I was being crude/taboo here. Maybe just a cultural difference? I spent most of my adult formative years in the military, and there's been some cultural friction ever since I ended my service. An explanation of your opinion would be quite welcome!
What? I was defending your comment, seeing the downvotes!

Crude, well... to be honest, because of the slang words, eg. 'getting laid' and your argument could have been framed better to be more intellectually stimulating. Taboo, well.. because I don't see anyone else talk about this, yet it's such an important topic of human psychology / sociology.

I understood that you were defending it! The downvotes were a bit confusing to me also, as I felt pretty well founded in my original comment. As I said, I'm more just interested in your reasoning that I was being crude and taboo, because I have a background that seems less common among HN posters. It would help me better understand the downvotes!

I definitely could've swapped out "getting laid" with something more elegant sounding, such as "engaging in intercourse" or "stimulating a release of oxytocin", but I assumed that "getting laid" is both socially acceptable and has a well understood definition. Apparently HN disagrees!

I don't consider sex as a subject to be taboo, especially from a health standpoint. People get degrees from major universities that are entirely sex-related. So I guess I'm just a little confused by all responses, especially the one from hi41. Thanks for explaining your reasoning, it's good to hear the process behind these differences in opinion.

Do you mean mental disease, or physical disease? It is a well researched understanding that sex is very important for both mental and physical health, and with the massive amount of money spent on sex education, I'd hope it's clear how to avoid physical disease. And I never mentioned promiscuous behavior. It's quite possible to have sex, with have sex with many different people.
This is a common problem. The book Deep Work goes into it in detail, and has some research-based techniques to fix it. Basically it boils down to removing distractions, and determination, gradually building up your brain's ability to focus again.

https://www.amazon.com/dp/B00X47ZVXM

Thank you for sharing, this looks like a nice reference (although the irony of obtaining this link via hacker news is not lost on me.)
If I try eliminating distractions, I just end up daydreaming. I spend most days at work just reading the same shit over and over with nothing sticking. I manage to get work done in rare bursts after exhausting my brain with distractions.

Fwiw, I'm in my mid 40s and have less than a year of treating my sleep apnea. I'm pretty sure I've had ADHD my whole life but blamed other things until recently. I'm trying to resist getting medicated, but I think I might it to get me focused enough to work on CBT and meditation.

Don't medicate but challenge yourself. Learn a new editor while doing your job. Try using Windows instead of using a Mac. Try typing only left handed. There are varioud challenges to overcome boring assignments. Also try a standing desk so if you skive then you must stand up for fifth teen mins.
the worst advice ever.

If you have medical issues - take care of medical issues in the best way possible.

Challenging yourself = denying yourself treatment.

Challenging yourself is not a treatment for something like sleep apnea.

Yes but the evidence for ADHD is dubious. We have a long way to go when it comes to understanding the human mind. Anyone, not just people with ADD, can increase their performance with narcotic stimulants. Just don't be disingenuous about it.
Just because ADHD is poorly understood and often diagnosed incorrectly does not make it any less debilitating to those who have it and certainly does not imply that they are not in need of medication. Yes the medication can be abused by those who don't need it to get an edge. This is what makes it a controlled substance. Opiates can be abused too and have the same effect on everybody. The difference is that people in server pain need opiates to function at a baseline normal level. People with ADHD need medication to function at a baseline normal level. The fact that some people abuse them to go beyond that is completely irrelevant
> People with ADHD need medication to function at a baseline normal level.

This statement is so out of line I had to chime in. I was diagnosed with ADHD as a child. I was medicated for a few years, before I stopped taking the pills without telling my parents (they were being pressured by the private school I was in to medicate, I was kicked out shortly after they found out I had stopped.)

Those years are a complete empty window in my memory, and left me with physical ticks that _decades_ later I still have to suppress, alongside no actual tools for dealing with the symptoms.

Medication is not a silver bullet. Not all ADHD cases need to be medicated. Find what works for you, whatever the hell that is, and don't listen to dogma. For me that was coping mechanisms combined with a realization that much of the "attention deficit" was because I _didn't want to pay attention to the shit I was supposed to_ and _that's completely reasonable._ To insinuate that I haven't tractably found success ("baseline normal") with a non-medicative approach in my life is frankly insulting.

In Adults outside a courtroom, mental health treatment focuses on easing distress because the person seeking out treatment is the patient.

Parenting is stressful and imposes a powerful incentive to reduce that stress. Consequently, too often in children, mental health treatment focuses on controlling behaviors because the person seeking treatment is the parent.

The first is a person seeking greater agency for themselves over a problem. The latter is a denial of agency of the child. I’m hearing you express your pain at having your agency suppressed and expressing skepticism at the tools used to suppress your agency.

But those same tools also grant some adults an agency they are desperately drowningly seeking for themselves.

1) have you ever had psychometric testing done? 2) what are you non-medication coping techniques?

Genuinely interested in hearing your story.

1. I might have when I was initially going through a ream of behaviorists, but I'd be lying if I said I recalled, this was around/after 3rd grade.

2. In large part, reminders. Notes, lists, alarms (calendar/phone alarms for _everything_, watering plants to finishing work shit), behaviorally trained prompts, anything to disrupt the "mental feedback loops" where I can find myself "unconciously" falling into something like tearing at my fingernails, reading HN, playing video games, or really any of the infinite things I'll come up with to not do what I should be doing.

e.g. even right now writing this, I'm being pinged to go back to reading PRs: after years of having automated browser alerts going "hey you shouldn't spend time in this video game/on hn, it's been 20 minutes and you have nothing to show." my brain has picked that up and is able to do it on its own. I found that hard blocking didn't work since I'd just find ways around it, but if I can remind myself this is something I _want_ in any way from pragmatism (mortgage) or emotional (getting wife nice things) whereas the games/Hn are actually _unwanted_ (despite what the dopamine might say) it's easier to force myself to focus on something I don't want, even in bursts. (getting myself to internalize and BELIEVE those facts took years and I still fight with sometimes when willpower is low.)

This was a bit of a ramble, and I'd be remiss to mention that the motivation to use the prompts would be missing without the philosophical context I assign to the things I do. (Disclaimer: I recognize not all people can use this technique, I simply use the fact that I have strong long-term motivations against my bad short-term focus) I mentioned it in passing (mortgage, wife, etc) but really finding things I _WANT_ and using my brain's likelihood to fixate on those, especially in periods of distraction, I can tie those things back to what I SHOULD be doing and create a virtuous cycle. Contrivedly: Distracted looking out a window at garden. Fuck, I can't afford this garden if I don't go back to coding. (Dang, this HN post is getting long. Better get back to work so I don't work late today and can spend time with the wife when she gets home :) )

You are spreading mis-information. Attention, and lack of it, is something humans have understood for thousands of years.

1) stimulants and narcotics are two very different things, with diametrically opposite effects.

2) Not everyone can improve performance, due to U-shaped response curve. Those that are functioning optimally will actually see their performance decline.

3) ADHD is one of the best understood disorders. Impaired working memory (i.e. ability to concentrate) can be easily diagnosed quite definitively with CPT: https://en.wikipedia.org/wiki/Continuous_performance_task

It is of course personal choice in the end to decide on treatment, but if the ability to concentrate is in the bottom 1-5% - it is going to be painfully obvious to any half-decent paediatrician.

I can say medication has done wonders for my professional life and has made me much happier overall. For anyone on the fence about trying medication, I would say it is certainly worth it.
Do you have any tips on finding a project manager whom you can videochat or email with and ask for help with the project of navigating the healthcare system?

(uk private in my case, though I imagine people in other places would benefit from wider answers.)

that's called a well-connected private GP, or, if you are rich enough - physician in chief of a large hospital/center.
That's bad advice for me. For instance, I was supposed to be working on finalizing a new feature. Instead I was obsessed with writing tool wrappers. I learn things all the time, but they seldom relate to the thing I'm supposed to be doing. I'll bounce around instead of deep diving through the codebase I'm responsible for.
Holy shit, same boat, same age, on CPAP for about a year now, can't remember what was said in the meeting I was just in this morning. And browsing HN while I should be writing tests ;)
Your apnea could be undertreated, or it could be a symptom of a bigger problem. Epiglottic collapse is not uncommon in apnea patinets (10-15% of cases), and is not treatable by positive pressure, as it is an inspiratory valve.

CPAP only works on the expiratory valve - which is your soft palate.

If you are having a large number of arousals, despite optimal CPAP pressure, and your respiratory waveform is jagged in certain ways (hard to describe in text) - you are very likely having epiglottic collapses.

However, epiglottic collapse can only be definitively diagnosed by sleep endoscopy.

Epiglottic collapse can only be treated surgicially.

Or simply go to a doctor specializing in it and check with him what they think. If they suggest trying something - try it and see if it works. I was in the same boat. Can't really say it was a miracle, but it definitely helped a bit.
Add a decade, subtract the apnea, but otherwise: Welcome to my world, or an exact replica thereof.

The thing is, I can focus on stuff I really want to do, but I can't fake the want. So - my personal pet projects are doing fine. As for work, my life has improved tremendously since I sort of gave up careerwise. These days I drive a bus and actually have some mental energy left over for my sparetime. No medication. I don't like the idea.

I would add that _almost anything_ that requires focus for a period of time can actually help you out. This book talks about the benefits of learning to memorize a deck of cards - just you and a deck of cards in the morning (no phone, no computer), can help "repair" your ability to focus.
Have you been able to put any of these techniques into practice? It's been a few years since I tried reading it, but I wasn't able to push through the fluff to make it to the practical advice.
Self-fulfilling prophecy, probably a reason you want to be in this trap
I had a similar issue recently and during one of my many afternoons dicking around on the internet to avoid work I actually found a solution that worked surprisingly well: FocusMate.com

It's kind of like chat roulette (before it got weird) for study buddies. I'll admit the premise is a bit odd butt don't knock it until you've tried it. Essentially, you're paired with a random partner for a 50 minute mostly-silent video chat. You spend the first minute or two saying a quick hello and each definitively stating what you're going to accomplish in the hour and then you get to work (separately - they're very clear it's not for collaboration). When time is up you both report back on whether you did what you set out to do. And while you get to work the corner of your screen has a little live stream of your partner diligently working.

Not only does it help keep me "accountable" and working pretty efficiently for the full 50 minutes but it's also made me so much better/more realistic about estimating what can be accomplished in that time.

I think you answered your own question. Ditch the social media. It only hurts for a bit, and then you start to feel better and wonder why you were so obsessed to begin with.
Could something else trigger your distractions and then spiral you into out of topic behaviours?

In my case, that's a chatty coworker. Every time he yanks me out of my flow, I get the urge to check social networks and have a really hard time going back to deep work.

Use the Pomodoro technique and limit your social media checks (if any) to the scheduled breaks that the technique provides for you. In general, it's also highly desirable to enter a flow state as quickly as possible (this is what "focus" really is, after all) and there are ways to make this more likely, such as starting your session with easier, lower-stress tasks and moving on to harder stuff incrementally.
Talk to your supervisors about how you aren't excited about your tasks at work. If there's nothing they can do about it, quit. Find something that excites you.

A family friend of mine used to play violin in a traveling rock band. He slept in the van and loved every minute of it. Later, when he had a family, he settled down and became a contracted fundraiser for non-profits. One day he came across a particular non-profit that he loved, identified with, and felt excited by. He took a 50% pay cut and settled into a permanent position with that company because "it felt like being back in the van again"

If you're the kind of intrinsically-motivated person who can trick their head into being excited about 12 hours of being immersed in a problem, then you'd be an incredible asset to any company that gets you excited.

If you have an idea of what company, industry, or problem that might be (good news: people usually do), pursue it like hell.

Momentum!

At least for me, productivity requires momentum and being at a standstill feels like quite a hurdle. So at least at first, work on something you're really excited about. Something that won't feel like work. It could be a small project you couldn't otherwise really justify spending time on, but do it anyway. Visualize the end result, get yourself excited, and finish it.

Once you've gained some speed you should be able to tackle other tasks and projects. Social media will start to seem less compelling than it does now.

The browser extension named Momentum is also useful for this.
I had to completely clear my mind of the past, and of what others may think of my project/code. It's a brand new day, you're breathing, you can truly work on whatever you want.

When I'm hot on a new video game, I can't wait to fill the most immediate next hours on doing it. What was that for you at some point? I enjoyed QBasic in gradeschool and revisiting after years in the field was really fun.

Whatever that code is, I suggest pursuing it even if it's not

a) something everybody on here says we need to be doing

b) related to your dayjob

c) related to that "big project" that you need to do perfectly

I wish you the best.

P.S. strike perfectionism out of your mind

P.P.S: Practically look into using the Pomodoro Method

P.P.P.S: Either start or stop doing drugs. Or see a board-certified medical professional if you think that would be dope. It is.

edit: formatting as always

Look to your health. Eat right. Exercise. Clean your workspace. Etc.
For me, this means that a) I am not interested in my work, b) I am tired, or c) I am burnt out. All of these can be greatly helped by getting some good sleep, and a + c can be helped by having some fun - especially social or physical activity somewhere new (weekend trip, drinks with friends, etc).

You can try and force yourself to focus with tools and mental tricks, but that always seems to make me feel worse. Better to get at the root cause, which is almost always stress or physical exhaustion.

Uninstall Instagram and Facebook from your phone. Block them and other time-wasting sites on your computer. Start training yourself to not be distracted. This is what has worked best for me.
Same here.

The quicker you distance yourself from social media the healthier you will be. There's literally everything to gain by leaving it behind.

Changing the color scheme in your editor can make your work seem novel again.

Electronic music also helps me, but this is counterproductive for some.

Yep, good underground techno mixes at 125 or higher bpm can give you the drive and concentration you need for extended periods of time.

My coworkers usually get a slight shock when they learn what kind of music I have in my headphones during work, being a 50 year old guy :)

Do you have a public playlist?
Digitally Imported is pretty great. I've subscribed for years alongside Spotify and prefer it for set and forget playback of a genre of electronic music. Spotify is for specific playback and discovery.

There may be a free trial, and I think there is a free tier with ads if you can tolerate them.

There's a lot of factors that might go into this. If you're hungry, sleepy, undersocialized, unkempt, unfit, depressed, in a loud, messy, and distracting office, with unclear and unexciting goals, large monolithic tasks without clear breakdowns on how to move forward, constrained by slow tools, management, or blocked on dependencies... well, it'd be no wonder you wouldn't want to work!

Eat well. Sleep well. Exercise. Dress for success. Socialize, integrate with the team, find the goals that will motivate you - be that helping out your work buddies, or building that sweet new piece of technology.

You want to have energy, health, mood, and motivation. When any of these suffer, your work suffers too.

Don't be afraid to use whatever tools you have at your disposal to cut out distractions and inhibit bad habits, but don't tyrannize yourself either. Working 8 hours straight isn't necessarily your most productive option. Consider scheduling breaks - but use your tools to limit your distractions outside of said breaks. Find a balance that will let you keep your mood up while also getting good work done.

I just want to add that coffee can make you feel like you are having enough sleep when you are not. If you drink it constantly, you may be just tired but not know it.
I can't tell if this is an endorsement of or warning against using coffee to mitigate a lack of sleep, so I will take it as reinforcement of the former which I am currently (and unfortunately) a practitioner of.
Why not both? It can help, but it does not fix the underlying issues. And those underlying issues may have long term health and energy conseqeunces.

That said, while I've cut back a lot, you can pry my energy drinks out of my cold, dead, caffinated hands.

If you want the best effects of caffeine you have to cycle it. Save it for those special occasions where the real positive benefits of caffeine can work for you and the negative long term effects don't work against you.
Caffeine has a half-life of about six hours so it can affect your sleep without necessarily knowing it leading to bit of a vicious circle. If you are interested, Dr. Peter Attia has a three part episode with Dr. Matthew Walker on sleep. It changed the way I prioritize getting sleep.
> Eat well. Sleep well. Exercise. Dress for success.

One of these is not like the others. I can understand emphasis on dressing up in contexts that involve selling -- a product, an idea or even yourself. But what does it have to do with focus?

Mayhaps not as necessary as the first two, but having separate wardrobes for work and free time can help trigger you into a work mindset.

That said, it doesn't need to be a 3 part suit.

I have found that if I wake up feeling like crap, I feel just a bit better if I put on a bowtie that day.
It doesn't necessairly need to be "dressing up" per se.

But if you're showing up to work poorly shaven, wearing stained sweatpants - you're going to be at least a little more self concious, a little less self confident. Nobody's going to lift your mood with "hey, looking sharp!". It's going to reflect in your body language, and the body language in your peers, even if it's largely on a subconcious level.

It's potentially the difference between being ready to walk to lunch, or a meeting, or an interview at a moment's notice - ready to actually tackle the work and the day - vs being ready to go to bed at a moment's notice.

Adderall. Truly.
Adderall is good for focus once you break the habit of social media / youtube, otherwise it just makes you really good at focusing on social media / youtube. I know this from experience.
Just treating the symptom, not the problem. It will come back to bite as all amphetamines do.
I wrote some things that help me focus here:

https://wiki.nikitavoloboev.xyz/focusing

Mostly it’s about practicing mindfulness throughout the day, setting goals, GTD approach of task management and using tools like Focus for automated Focus sessions throughout the day with breaks in which I am allowed to read news and relax.

Just logged in to say that the tips on your wiki are pretty good and overall all the info in there seems pretty valuable, bookmarked. Thank you for this!

Also, might give me some inspiration to make my own wiki ;)