Re:When to refactor code
by steveklabnik on Thursday May 16, 2013 @10:45AM
(#5718847)
I use 'break out method object' from "Working Effectively with Legacy Code" in these situations:
1. Create a new class for the computation
2. Define a method on that class to do the new work.
3. Copy the body of the old method over, and change variable references to instance variables.
4. Give it an initialize method that takes arguments to set the instance variables used in step 3.
5. Make the old method delegate to the new class and method.
Now, at least, it's isolated into once place, and you can use your other refactoring tricks without affecting global state, like extract method.
I'm not sure if this is the _best_ example, but I wrote about this a few years ago, in Ruby: http://blog.steveklabnik.com/posts/2011-09-06-the-secret-to-...