In this post I’ll be writing about my general approach to problem solving, when programming. It will be in the context of a Hypermedia API project I’m working on (which I will call the “HAPI” project). I’d like to explain my thought process, and the steps I take to try to tackle different issues.
These are the high-level steps I take to solve a problem:
- Understand the problem
- Come up with an initial plan of attack
- Try to solve the problem
- Increase my understanding of the problem, and potential solutions
- Repeat steps  and  until I eventually solve the problem
In the following sections, I will cover each of these steps in more detail.
5 Steps to Solve a Problem
1. Understand the problem
The first step in solving a problem is to understand it. You need to understand what you’re working on, and what you expect to happen.
For example, for HAPI, I had the following acceptance criteria for a story:
Given that the user enters a value in the UrlBar
And the value is not a valid URL
When the user presses "Enter"
Then the border around the API UrlBar turns red
All you need to know here is that there is a URL bar in a website, and that the border of the bar should turn red when an invalid URL is entered into that bar (feel free to go here for more details: https://github.com/jdesrosiers/api-browser-web/issues/8).
In this particular case, I initially didn’t even have the acceptance criteria. So as part of “understanding the problem”, I drafted up what I believed the criteria should be, and checked it with the project owner. It was good that I did that, because I actually had to make several edits to the criteria.
If I didn’t do this, and instead just jumped straight into working on the problem, then even if I solved it, I would have been incorrect, in that I didn’t solve the correct problem.
2. Come up with an initial plan of attack
Now that you understand the problem, and what is expected, you must draw on your existing knowledge and experience, as well as look up relevant information, to begin to attack your problem.
With HAPI, I knew I needed to do several things. I needed to learn how to change the border color of an input element. I also needed to figure out how to trigger that behavior when the user pressed <Enter>, and the URL was invalid. That involved 1) validating the URL, as well as 2) triggering an event to change the color. However, I had no idea how to do any of that.
My plan of attack at this point was to do a spike to see how to change the color, poke around and change the code of my project to see what changes I would have to make, learn how to validate a URL (using ajv and JSON Schema), and combine all these things together at the end.
3. Try to solve the problem
Finally you’re at the point where you can jump in, start coding and hacking, and see what happens. Here, you just try to solve the problem. However, it might not be that easy.
For HAPI, I executed the plan of attack outlined in the previous section. It took me a whole day, but eventually I was able to figure out all the pieces of the puzzle. However, there were 2 issues that arose. There was still a bug in the code, and I hadn’t implemented the code using TDD.
Nonetheless, this was still a good first step. I learned more about the problem, made progress, and had potential paths to move forward from here. My initial attempt at solving the problem was effective and helped me move forward.
4. Increase my understanding of the problem, and potential solutions
At this point you’ve now tried solving the problem. But you’re stuck. Other problems have probably come up, and you’re not quite sure how to deal with them. You have probably made some progress, but aren’t quite there yet.
What you do now is to take a step back, see what you can learn or research, and increase your understanding of what is going wrong, and what you need to change.
Here are a few things you can do at this stage:
- Read short answers and articles (use StackOverflow, articles, etc.)
- Read more extensive texts (a book, a full online tutorial)
- Watch videos (YouTube, or from other outlets)
- Ask fellow colleagues for help
- Ask people online for help (through StackOverflow, or forums)
- Use a debugger for your project (might have to set one up, depending on the stack & technologies involved)
Sometimes it also helps to just step away from the problem, take a break, and trust that your subconscious mind will continue churning away at the problem. You never know when or where inspiration will come from.
5. Repeat steps  and  until I solve the problem
Now, we can just iterate over steps 3 and 4, continuously learning more about the problem and how to solve it (4), and chipping away at the problem (3).
There will almost always be new issues that arise along the way, and each issue can be addressed similarly using the 5 steps presented here (although you would probably want to solve them as quickly as possible, so that you don’t end up with a huge tree of problems that you’re tracking and trying to solve).
As I continue to learn more and gain more experience, I’m sure this process will change and be refined. I look forward to developing a robust and foolproof way to approach software-related problems.