When I review code from more junior developers or even tenured developers, I find a tendency to either overengineer or overcomplicate solutions. When I ask them to explain the rationale behind their implementation, I get an answer that feels like this was their first thought, and they implemented it without analysing why their approach is ideal.
I ran through the same issue for so long until I heard a quote that said — “think twice, code once”, — and I incorporated it into my process; given that I like to visualise my thoughts.
The thinking process
To think, you have to write!
Ideally, we should strive to produce maintainable and scalable solutions that are also easy to understand and test. Let’s say you have a task to add optimise a feature on a given app. At first glance, you may have an idea of what needs to be done and how, but then you decide to jump straight to implementation.
Going straight to implementation would be the best and fastest thing to do, assuming you have an idea of what needs to be done. Regardless of how much context I have on the feature, here is what I would do:
Gain context
Before doing any implementation, it is good to understand the context of why we are optimising the feature. This will enable you to define the impact of these changes.
You will retrieve all the requirements needed to implement your solution during this step. While understanding the context, ask yourself why five times.
Draw your assumptions
Based on the previous step, you will now be able to write down your assumptions about what needs to be done and how. Ideally, you should have at least two possible paths.
Limit the number of assumptions to avoid being distracted or losing focus. Based on your assumptions, you can now stack rank them and drawing the pros and cons of each and compromising whenever necessary.
This step should be iterative, and it should be repeated at least once!
Implement
Selecting one of the potential paths or solutions now becomes more effortless, given that you already know the pros and drawbacks of one compared to the other. The implementation process will be faster as you have defined what needs to be done and how.
Overall, the message here is that before you code anything, you should spend some time thinking and writing down your thoughts. This will not only make you a better problem solver, but also help you become more aware of your decisions when crafting a solution.
As a bonus, I recommend you spend some time watching — Thinking Above the Code by Leslie Lamport1.