Saving time with refactoring
When I wrote about refactoring being free I mentioned only a typical situation where you have a (large) body of code and you want to modify it. This happens a lot, but refactoring is especially useful when you're enhancing small pieces of code that you just wrote, as in earlier the same day or somewhere last week. The point here being that the time you gain is not from the fact that you keep modifying your own code (which will always cost some time) but from the much greater amount of time you save from having a bigger chance that you're investing precious development time in the right features.
I remember that a couple of years ago, I was asked to write a small parsing application to visualize a messaging protocol for debugging purposes. The protocol defined several messages, but because the specs were not entirely clear, I decided to just jump in and parse what I could. The end-users did remark that the protocol consisted of incoming and outgoing messages and that it was probably a good idea to display those in seperate columns next to eachother.
I just started parsing the messages and defined a simple
Message-class. Every time I found a chunk of data in the stream that consisted of a complete message, I created an instance of this class that contained mostly some basic get-operations. Before attempting to parse anything else, I decided to create a basic GUI-application to go along with it, so I could deliver a first spike to my users to get some additional feedback on where the GUI should be headed in terms of design, since I'm fairly crappy at coming up with good user interfaces on my own.
I released the first version of this tool the same day I started working on it. The users were very happy with it, as they were looking at the data streams with a hex editor before that and figuring out where messages started and ended was their biggest problem - and it was solved already! Furthermore, they told me I should forget about the multiple column display they wanted earlier that day, because they now noticed that the communication was not always consistent in the order that messages were being sent and received. In other words, the single column view provided some interesting facts that a multi-column view may have obfuscated, unless it would have been much more sophisticated, in a way that I would never be able to deliver within a short period of programming anyway.
And that's the most important point about refactoring: sure, modifying your code always takes more time than writing it once and never changing it. But that's only possible if your specs are 100% clear up front and never change. And how often does that happen? It has never happened to me.