The other day as I was coding along I found myself getting deeper and deeper into ugly code. It all worked well enough and behaved as it should but the implementation was far from the clean, concise code it should be. Here we have a clear indication that it's time to roll up the sleeves and dig in with some serious refactoring.
I had been working on an application and trying out various implementations of an idea I had for it. As is often the case my original plan was to get the idea off the ground as a proof of concept and take it from there. I don't want to be fully documenting and constantly refactoring code that has a good chance of not even making the cut as I iterate through variations of solutions. I'll do it later. The problem is, "later" is a slippery slope and often comes further down the road than it should, if at all. You know this is the case when you finally do get around to refactoring and the code suddenly becomes so much easier to work with. I know, that's the whole point of refactoring, but I don't mean a little easier to get around, I'm talking about problems now having solutions popping out at you and bugs revealing themselves to be squashed. It should never get to the point where it's as difficult to work with as it was. By having the grime cleaned away and being able to see the forest through the trees, things get a whole lot clearer. It's also a great time to throw in those comments you've been meaning to add.
Refactoring during development reminds me of when I'm working in my wood shop. I'll work on the first phase of a project and make 90% of the cuts. Once I'm done this step I stop, clean up the shop, put all my tools away and get ready for phase two - assembly. Once assembly is complete, off I go again cleaning up the shop, ready for the finishing phase. This process continues until the project is done at which time I do my final pass on the area and get it ready for the next project. This final pass isn't nearly as painful as it could have been since I've been tackling the clutter as I went, keeping my tools accessible and the shop clean. Doing things in this manner I'm not carrying my mess around with me from phases I'm no longer in. I'm not tripping over cords from previously used tools or knocking over paint cans while I'm sanding. I start each process with a clean slate and have nothing to get in the way of the task at hand.
The point is, it can be wasteful to try and write perfect code all the time but can also be a real problem if you loosen the reigns but don't get back to what you know could use a little love. Give yourself room to get dirty but don't wait too long between refactors. It'll save you from headaches, produce stronger code and better yet, it'll actually get done.
Monday, March 9, 2009
Subscribe to:
Posts (Atom)