I'm a little late to the party, but I wanted to give my perspective on some of the discussion surrounding the cost of refactoring I've been reading about in the past week, most notably some posts by Paul Gielens, starting with Refactoring is Not Free, where he writes:
A couple of months ago we where up for the challenge to refactor a large portion of a badly performing application. The application had no supporting unit tests, cruddy code and Swiss cheese specifications (its developers only had a sense of what the application had to do). It is in this example where refactoring became costly. At start we tried to cover as much existing code possible [...]. Although the confident level increased we weren’t able to preserve the behavior of the code while refactoring small pieces at a time. Eventually we had clear signs that the progress we made just wasn’t enough. We then decided to rewrite it from scratch and than refactor it.
What I don't understand in the described situation is what the goal of refactoring here was. Refactoring itself doesn't increase performance, in fact, often refactorings reduce speed in order to make the design more clear. What I do understand from the above was some kind of decision to just "refactor the whole thing" and then see what could be done with performance, which is a strange approach in my opinion.
When a system performs poorly, there's a basic approach to figuring things out: first find out what the bottlenecks are, find solutions to them, test them independently to verify them in the situation at hand and finally, modify the code to incorporate these changes. Now, refactoring comes along in the final step, because in order to modify the existing code, you'll probably need to do some refactoring. Not all of the existing code, just the pieces where you want to modify things to incorporate the higher performing approach. Now even then, writing from scratch can often be quicker, so the conclusion may be the same. But either way, the refactoring is never "costly", it's always the sanest way to change code. So that's why I'm stating: refactoring is free.
Suppose you have a class that works with a type code that modifies its behaviour. Every method may consist of a switch/case-construct that decides which behaviour to expose. Modifying this into a class hierarchy where polymorphism takes care of the type-specific behaviour does take time, but it's always better than just adding another case-label to all the existing switch/case-constructs. It only takes a couple of 2:00 AM debugging sessions to figure that out.