How to get unstuck when coding
What I mean by "stuck"
Here's how I frame this kind of situation. Being stuck means that you're not able to finish or make progress in a task with your current knowledge, mental state or physical state. It happens every once in a while and requires special consideration to tackle it effectively. Some examples are: not being able to solve a bug or not being able to find a good design that satisfies all the constraints of a given problem.
How to tell that you're stuck
The first step towards getting unstuck is to recognize you're stuck. Here are some ways to determine this:
- When you're not making any progress for a little while. For me, this usually means half an hour to an hour
- When you're feeling deeply frustrated with a problem
- When you're unable to understand the problem and its factors. For example, when you need to implement a feature in a platform or with a library you're not familiar with
What to do when you're stuck
Usually, we grow the most when we're faced with challenges, so every time you get stuck, recognize that you found an opportunity to grow. It might be hard to get through it but it will be rewarding.
The physical aspect of it
Our minds need to be in a good physical state to solve problems. Some of the things that can make put it in a bad state are stress, lack of sleep or an unhealthy diet. It's critical for you to get into a positive mental state. Here are some techniques you can use to achieve that:
- Get away from the computer (if your problem presents there). This will help your mind rest from being exposed to the problem and achieve some relaxation, as well as allowing you to think of the big picture of where this problem fits. For me, practicing ping pong in the office worked like a charm. Taking a walk also works.
- Breathe mindfully
- Eat the right type of food
- Try to get the right amount of sleep
If you want to go deeper into this topic, I really recommend watching this
Do your homework
Always do some research on the problem. Try to identify what are its key elements: a specific layer of your application, given technology, a specific set of conditions or constraints. Then, google it. It's very likely someone already solved that problem (or a very similar one).
If you have just a thin idea of where a possible solution might be, spend some time learning everything you can around the related technologies/topics. Search for articles, documentation on the API/technology, common patterns, etc. In case you're wondering, yes, learning is part of your job.
Ask for help
If you're unable to find a solution on your own or need to solve the problem very urgently. You can ask somebody who is familiar with the problem such as a friend or colleague. Also, you can go into an online community such as StackOverflow and ask there for help.
If everything else fails
Apply the diffuse mode. This is an underused way of using your brain to solve problems in the background. Learn about it here
A note for people starting out in the world of programming
It's normal that at the start of your career you find these kinds of situations more often. It's normal. Even if you're very experienced, you'll still find these situations but you'll have better tools to tackle them faster and efficiently.
Feel free to share your experiences of getting stuck and how you overcame it.