Working the problem
It’s extremely rare that the first implementation that I write to solve a problem is the best one I’m able to come up with.
My process involves getting a working solution as soon as I possibly can so I can quickly discover any roadblocks, then take a step back and look at everything involved. Sometimes, I might even check out a clean branch and start it again! Except this time I know more than I did the first time round.
Even after doing this, I still might not have the best solution to the problem, so what do I do?
I work the problem.
I’ve got a solution in place. I hopefully even have a (system) test covering the fact that it’s working. Now I’m free to work the problem.
So what is working the problem?
For me I take a really critical look at everything I’ve just written. I begin to trim the fat, reducing the solution down to its essence. This helps you really see what is required to solve the problem, and also end up with possibly the smallest implementation required to solve it. I also work hard at naming to really make it clear what this thing is you’ve just created. A side benefit is it makes your Pull Requests much easier to review for your peers, as your code won’t contain any distractions.
When you’re working the problem, you might find that you just need to rewrite a big chunk of it! That’s all good. As I said at the top, rarely is the first implementation the best one.
I usually find that as I keep working I discover an entirely different solution, a better name, or a simpler architecture. Or there’s an opportunity to introduce a new model and relationship to really make everything a bit cleaner. Everything has to be on the table. Just because you didn’t make that decision on your first or even second time round, doesn’t mean that it isn’t something you can do now.
You’ll definitely find this process a lot easier if you have a test covering the overall behaviour you’re trying to work on. It won’t help if you have a lot of tightly coupled unit tests, as you’ll feel compelled to keep those in place because you spent a lot of time working on them.
So that’s it. Get a solution in place as soon as you can. And then just keep working on it. And keep working on it.
Combined with operating at different zoom levels, this is the way I can produce my best work.