Strategies for Getting Unstuck
Learning Goals
- Identify strategies for getting unstuck
Introduction
It happens to all developers at some point: you're on a coding roll, and then you hit a rough patch. You start trying different solutions, but none of them work and you keep getting farther and farther off track. That's why it's useful to have a set of strategies for working through complicated issues and getting yourself unstuck.
Identify Strategies for Getting Unstuck
As we've talked about before, programming is, at its fundamental level, a process of solving problems. So, when our programming gets difficult or confusing, it usually helps us to think about generalized problem solving and how we can apply those techniques to our particular issue at hand. The behavior we should most seek to avoid is "trying stuff randomly." You wouldn't want a surgeon to take that approah when it comes to fixing one of your ailments, treat your code with a similar level of respect.
We have a few approaches to try when we get stuck. Let's take a look at each and how they apply when we're coding.
-
Simplify the problem. Check your goal. Is it too complex? Does it try to solve multiple issues at the same time? You might be able to dismiss one or two or a few issues and solve a more straightforward version of the problem. One of the reasons adding subroutines is so powerful is because it forces the code to become simpler. This will make finding your way out a lot easier.
-
Research similar problems and solutions. The internet is your friend. Search for examples of solutions trying to achieve the same goal as yours. While you obviously don't want to cut and paste code into your own solution (not only is this not going to increase your understanding, it probably won't fit your specific needs), looking at similar problems might suggest something new to try.
-
Look at the problem from a different angle. This is an easy trap to fall into if you're working on a problem like others you've done before. You might instantly apply the same solution. But if anything is different or more complex, that same solution isn't going to work. Remember to take a step back and reconsider your assumptions about the way forward.
-
Break the problem down into smaller parts. This is similar to our first approach, simplify the problem, but it's less about the problem itself than how we're working through it. Instead of trying to fix an entire app, can you focus on a block of code? A line of code? If fighting your way through too much code at once is what's getting stuck, you can adjust your context and take each piece one step at a time. Some developers escape this trap by starting with a clean slate and writing a simple program to do the simplest version of the task. They then "grow" their simple example's complexity until it get closer to the real problem and usually have their "A-ha!" moment in that process. This approach naturally joins to meet our following strategy as well.
-
Start with a solution and solve the problem backwards. This is the approach to take if you know exactly what your result should be but aren't sure how to get there. Start at the end and determine what it would take to get there.
-
Run Uphill: Do the "Hard Thing" You Don't Want to Do. When looking at code, we sometimes get the intuition or insight that the way the code is isn't the way it should be. Maybe the current code uses an
Array
and you're repeatedly doing afind()
instead of a lookup (somethingHash
es are ideal for). In your heart of hearts, you know you should be using aHash
instead. Y0ou know you and every programming that will come after you would wish for aHash
...but going through and changing the code to be aHash
feels like too much work and it's not really the cause of my bug so, #yolo, leave the old code in. This strategy says "do the opposite." By making the code have less friction, it will free up your mind to be able to attack the bug.
Conclusion
If you find yourself stuck in a coding problem, following a methodology of problem solving will help suggest different approaches to try and help you get yourself unstuck.