Motley says: "Refactoring is too hard - it's not worth the effort"
Summary
Motley: Every time I make a change in refactoring I either cause compile problems or break functionality. Refactoring is slow and not worth the effort.
Maven: Use tools to refactor and leverage unit tests to improve your confidence that your refactoring does not break functionality.
______________________________
[Context: Motley is refactoring some code but having a tough time with it.]
Motley: #$%#%! This refactoring thing is a load of crap. It's too hard and not worth the effort. You were definitely high on something when you gave me this little tidbit.
Maven: Maybe I'll take a teeny step back for a second and let you cool off.
Motley: Not if I chase you into the kitchen with my iron fists of fury!
Maven: Well, errr, what's the problem?
Motley: Every time I make a change to "improve" the design I end up breaking something. Sometimes functionality breaks with my change and I discover that when I run the application, and sometimes I just have compiler error after compiler error. It's REALLY frustrating!
Maven: You know what I'm going to say next?
Motley: Grrrr. Yeah. "I can help with that". Well give it to me before I wring your neck!
Maven: I can definitely help with that. Let's start with the compiler errors. Even doing simple things like renaming variables - which is a simple refactoring - can cause compiler problems. Using Find/Replace across all files in a project can help, but you can still miss a file - particularly if you change an interface. But that was the old days. Let's try a basic refactoring example in Visual Studio 2005 that will make your life easier. Double-click the name of the variable you want to rename.
Motley: Grrrr… Fine. Now what?
Maven: Right-click on the variable that is highlighted. See that little "Refactor" menu that appears? Mouse over it, look at the pop-up menu, and click "Rename". Type in the new name of the variable and click "Ok". VS gives you a preview of what it is going to do. When you accept the changes it goes through and takes care of all the renames in a safe way leveraging all of the symbol information it creates when it compiles your project. I've come to trust it quite a lot.
Motley: Now that is pretty cool, I must say. But I don't use VS all the time.
Maven: Most other modern IDEs these days have refactoring support built in. In fact, the idea has been around for quite some time. Smalltalk developers are quite familiar with the technique. It is a huge time saver.
Motley: Ok, and I see there are quite a few different refactorings available. I'll have to play with that. But what about breaking functionality? That's still getting in the way of refactoring!
Maven: You're doing Test-Driven Development, right? Which means you have unit tests?
Motley: Yeah, so?
Maven: Are you running your unit tests after each and every refactoring?
Motley: No. That seems like overkill.
Maven: Well, that's part of the problem. You've spent a bunch of effort creating unit tests. They definitely help drive the design, but they also give you confidence to refactor the code. Every time you make even a small change, passing unit tests give you confidence that you have not broken any functionality.
Motley: Fine. I can see how that would help, but unit tests only cover small increments of code. I could still be breaking other dependencies/clients.
Maven: I can't argue with you there. That's reality and a most excellent point.
Motley: I guess validating the unit has not broken still goes a long way in preventing breaking functionality. I'll stick with this a little while longer and try and prove you aren't as dumb as you look.
Maven: I appreciate that. I think.
______________________________
Maven's Pointer: Visual Studio 2005 offers several different refactorings - mostly from Martin Fowler's refactoring catalog. Examples include rename, extract method, encapsulate field, extract interface, promote local variable to parameter, remove parameters, and reorder parameters. Experiment on some sample code by highlighting chunks of code and applying the refactorings. Once you get used to it, you'll save huge amounts of time. Other IDEs like Eclipse for Java also offer refactoring support.
Maven's Resources: Refactoring C# Code Using Visual Studio 2005