Kermit the Frog used to say that it’s not easy being green and he was right. In TDD we want to stay in the green as much as we can. When our tests are green life is good.
Turning the red bar green can be easy at first but keeping it green can be very difficult. Refactoring code can be the most difficult part of writing software but it is also the most important because this is when we make our code more robust and turn an idea into a product.
It is ok to hack something together as long as we atone for our sins later and clean up the mess we made. Just getting something to work is not enough. Once it works it must be cleaned up and made maintainable. Some developers feel they can do both these things at once and if you can then that is great! However, I almost always see opportunities later to clean things up a little bit more.
I find it way easier when working in new territory to break out the “getting it to work” and “making it maintainable” into separate steps. Really, they are separate concerns. When I am figuring out implementation I try lots of things; some work and some don’t. I weigh options and see if I can come up with alternate approaches.
Once I decide on an implementation I go back and look for ways to make my code clear; I rename variables, refactor for quality by eliminating redundancy, making code more cohesive, and try to encapsulate as much as I can.
By separating out the steps of “getting it to work” and “making it maintainable” I feel I can find better implementations quicker and it gives me the opportunity to make things as clean as I can.