Those solutions you provided seemed so obvious once I saw them, but I don’t know why I don’t think like that. Any recommendations on how to strengthen that skill?
Another developer I was working with asked me this question, and it really made me think. I’ll start with my answer, and then I’ll dive deeper into the topic.
A lot of that just comes with looking at code for a long time, but I like to think of it like writing any piece of text: there is always a first draft, and you need to edit and refine it before it’s complete. Code is the same way. It’s not like anyone writes it perfect the first try, but you get faster to reach the final version of the code more quickly.
The most practical advice I can give is to dedicate time to reviewing your own code with the lens of “How can I make this better, more readable code?“.
The question — and my answer to it — stuck with me throughout the day. The more I’ve reflected on it, the more truth I’ve found in the idea that writing code is like writing any text. There is a problem you want to solve — be it software development, developing a character, conveying a message — and a solution — the code, the story, the language — you’re trying to find. The goal is to find the best solution to the problem.
When I write an article, I start with an initial draft. Once that’s complete, the goal is to refine and curate the text until it’s concise, well-written, and clear. Now, this isn’t a novel idea; all writers (generalizing here) create a first draft. But the longer you refine your craft, the better your first drafts are, and the faster you identify the solutions to the problems you’re trying to solve.
Your first draft is always your first draft, no matter how skilled you get. What’s important, though, is you treat your code — or any text you create — as a solution that can always be improved. Take the time to refine and improve it, and you’ll quickly yield the benefits.