Everybody has encountered it, the programming problem that makes NO sense. This problem has no fix, it just cannot be done. Then all of a sudden, a moment of clarity, and 🎉 tada! A major breakthrough! Rarely, if ever, does one accomplish this but continuously slamming their head against the problem.
Being gritty has its merits. Grit is what gets us through all kinds of tough problems in life when applied appropriately. Unfortunately, grit can also be an enemy to a programmer. We strive so hard to accomplish the task, to fix the problem, or close the issue that we neglect to keep our minds in optimal working condition.
Whether we know it or not we likely employed one of these techniques or something similar when dealing with a hard problem. When you are stuck it is important to get out of the programming head-space and do something different that will activate your mind and let your brain figure that lingering problem out for you. It is critical to provide the right environment, nutrition and get the blood flowing for your brain to be working its best.
I have experienced this many times throughout my programming career and all of these techniques have helped me at one point or another. Step away, get a clear mind, and come back to tackle the problem with some fresh energy.
Stop Working
The first step is to stop working. It seems counter-productive but there is rarely a problem where the more you work at it and the longer you stare at your screen the more likely you'll be to finish it.
Take a minute to stand-up, do some quick stretches. How long has it been since you've had a glass of water or gone to the restroom?
Are your eyes tired? Maybe stand-up, close them for a minute and concentrate on your breathing.
Go for a walk
Some of my best ideas come to me when I'm walking to and from work. Getting away from the computer, breathing fresh air and letting my brain work without distractions or a screen.
You don't even need to have a destination in mind. Talk a stroll around the neighborhood, head to the grocery store, or the local coffee shop. It doesn't matter where you are going just go somewhere.
Let your brain breathe for a bit and it'll start doing the hard work for you.
Vigorous exercise
If you've got a difficult problem on your hands then it is time to step it up and get the blood flowing.
Weightlifting, running, cycling or any other activity that gets the takes a lot of energy is a great way to get your mind off of the problem. The activity will help get rid of the stress, make you feel better and coincidentally you'll get the benefit of being in better shape.
The activity doesn't have to be an all-day event if you are tight on time. Take 10 minutes to do some jumping-jacks, push-ups, and sit-ups. Run around the block a few times or go do the backyard chore you've been putting off for weeks.
Nutrition
Eating right is important for having a healthy brain. Functioning at a high level is important for solving problems. If you are eating only chips and sodas then likely you'll be going through various peaks and troughs. Eat more balanced meals for more consistent energy levels throughout the day.
Even worse still is not eating at all. Getting wrapped up in a problem for hours and forgetting to eat is a surefire way to burnout and struggle with the problem and make no headway.
Make sure you are drinking plenty of water, eating well-balanced meals that aren't full of sugar. When you get stuck and struggle with the problem until 1:30PM it is time to step away grab lunch. Once you have eaten come back and attack the problem with new found energy and enthusiasm.
Minimal reproduction case
There is a reason that open-source repository maintainers ask for minimal reproduction cases. You are forced to think about the problem and reduce it to its most basic of components. By reducing the problem it makes it easier for you to see what is happening and where you've gone wrong.
This technique is most useful when the problem isn't clear or the landscape is too complex. Start breaking down the problem into a component, function, or even a single line that is giving you problems. From there work on building a narrower scope until the problem becomes clear.
Talk it out
Whenever I have a problem and want to discuss it with a colleague I always seem to get some clarity about the issue.
Why is this? I'm forced to start from the beginning. Explain the steps required to get to where I am. I have to think simply and critically about each individual step.
If you don't have any within earshot you can try "rubber-duck debugging." Do the same exercise but with some inanimate object. If that feels to awkward try explaining it to a non-technical person. Whether they understand or not you will be forced to explain the problem in simple terms in a straightforward way which may help illuminate the issue.
Sleep
Finally, sleep is the most important of these techniques. If you are tired, or groggy, you will not be working optimally. Staying up until 3AM working on a problem may feel productive but, likely, you are wasting your time.
Make sure you are getting the sleep required by your body whether that is 7, 8 or 9 hours a night. Get your sleep! And let your brain work while you are sleeping. The number of times I have left a problem unsolved and came back to it the next day and solved it quickly and better than if I stayed up working on it is staggering.
Conclusion
Hopefully, you will use some of these techniques the next time you run into a programming problem. And, these techniques will help you break through.
If you have other techniques you like to use or have feedback send me a message on Twitter 🐤
Top comments (17)
Good article. I usually turn to Rubber Duck Programming myself. Sometimes, my wife plays the part of the Duck. She just sits there silently with a vacant expression and a slight smile. Usually I'll get a part where I found I goofed and I'll stop talking and turn back to my screen. She'll then kiss me on the forehead and walk away. Love that woman.
Ha! That's sweet. I'll find I normally get a similar response or a "I have no idea what you just said but I hope I helped"
Hahaha! Yep! I've heard that too!
I love rubber duck programming, works great!
"Go for a walk" is my go-to response to getting stuck. I work in an office, so I take a walk around the building, get a drink of water, and often the mindless activity of taking my mind off my work will help to shake loose some ideas. :)
After stopping working and taking a walk, I try to find another 'frame' to help me understand the dataflow in the situation. Sometimes I think of 'roads with traffic' or 'items on store shelves with a mad shop owner looking for expired goods' or whatever fits the problem. Then I think of solutions as if this was a physical problem. Then ideas get flowing and I can find an angle to tackle the programming. Even if its rough and the solution changes, it still helps get things going to put the problem into another frame and think of it that way.
Sleep is really important. When I sleep late, mostly the next day is wasted.
It's really fun getting stuck sometimes lol and I hope I'm not sounding weird but I anticipate those times. Drew was right in my mind, everything he listed here is what I do except one thing, I grab a drink and it helps too sometimes.
Push-up ?? No, it only takes out my energy, although it makes my upper arm muscle larger/harder. After doing push-up, I feel I lose my energy pretty much, and cannot think well.
High quality meal is much better, as it gives energy rather than taking the energy out of my body.
Taking a walk or watching something funny works like magic for me. It relaxes and reinvigorates you.
Taking the focus out of the 'persisted' problem have helped me alot. Taking a shower, eating and watching Friends then come back to the problem, and bam suddenly the solution is right there
"if you have a problem, just leave it" this what i learnd in this post
I actually do all of these. I can’t tell you how right you are about sleep. I can finish something in so much less time in the morning that was giving me trouble the day before.