How productive you are as a developer isn't only the result of your habits, your talents, and your experience. Your environment plays a key role, especially if you work in a team, a company, or a shared workspace.
Some developers thrive in a noisy or busy environment, but those are usually exceptions to the rule. That rule is simple "Don't interrupt the developer."
Working From Home
Working from home is a luxury if you have the discipline to set clear boundaries for yourself. Unfortunately, it's a very, very common trap many people fall into, not only developers. When you make the switch from working as an employee in a large office to working from home in a small home office, if you're lucky to have one, you may naively think your productivity is about to skyrocket. The opposite is almost always true, especially in the first few days or weeks.
Making a drastic change, such as moving into a home office, means that you need to create new habits. Most of us believe it has everything to do with discipline, but that's only part of the equation. Take the time to create a schedule that works for you and the people you work and live with. That may mean that you start early or put in a few extra hours in the evening when the kids are asleep.
You need to define boundaries, not only for yourself, also for the people you live with. Working from home is still work.
Working In an Office
Working in an office has its pros and cons. The advantage is that everyone knows and understands that you're at work to, well, work. Make it clear that you don't want to be disturbed. Protect your productivity. This may sound harsh, but it's essential if you want to get meaningful work done.
As a developer, your most important asset is your attention. If you're working on a complex task, it takes time to gain momentum and be productive. Every time you're interrupted, you need to start from square one. Not only is this frustrating, it can be mentally taxing.
I once worked in an office that had a simple rule that said that a developer with its headphones on shouldn't be interrupted. A room filled with people wearing headphones isn't uncommon in the technology industry where open offices are the norm. You'd be surprised by the number of developers that wear headphones without actually listening to music. It's a trick I've used many, many times. This is especially effective if your headphones have noise cancellation.
While this rule looks great on paper, it doesn't hold up in most situations. Everyone is interrupted dozens of times a day. The number of productive hours we put in is much lower than you'd think. That's the reality we live in.
Minimizing Interruptions
Slack and email have become indispensable in most companies, large and small. They're great tools for communication if they're used wisely. They're not most of the time. They're surprisingly often used as distractions instead of productivity tools.
Make sure you're not distracted by a slew of, often unimportant, notifications. If you're constantly interrupted by notifications, then it's clear you have no intention of getting work done. Don't look for productivity hacks if you're not serious about the most fundamental elements.
Find a Quiet Place
If you have a deadline to meet or you absolutely need focus, then looking for a quiet spot in the office may be your last resort. This isn't a solution long term, but it's sometimes your only option. If you're in luck, you may find an empty meeting room, but a couch or the kitchen is fine too. Don't forget to bring your headphones.
Deal With It
Distractions and interruptions are part of the world we live in. Working in the technology industry means that you're in the eye of the storm. There's no perfect solution unless you work from a home office where you dictate the rules.