Don Roberts and John Brant stated in the book Refactoring - Improving the Design of Existing Code:
"Refactoring with automated tool support feels different from manual refactoring".
Indeed - It is!
Having an automated tool that helps you to change the code without the fear of breaking it - is invaluable.
That's why, I wanted to summarize several available options for .NET developers.
Let's start with the obvious one: Visual Studio Refactoring Tool.
As usual Microsoft concentrates on the core business, leaving the field to other players.
Visual Studio Refactoring Tool
Visual Studio comes with a very simple Refactoring Tool, available from the Refactor toolbar or from the Refactor context menu.
As you can see, all the refactoring steps have keyboard shortcuts.
In addition, Visual Studio triggers some of the aforementioned refactoring steps behind the scene, when a certain change is detected.
Those changes are mostly underlined with a double red line, as here:
Using the combination Shift-Alt-F10 will fire the correct refactoring step menu, allowing smooth and quick refactoring.
(In the case above - Rename refactoring step).
The Refactoring Tool provides pretty basic refactoring steps:
- Rename -
The tool effectively changes the names (variables, methods, types and etc...) across projects in the same solution.
The changes can be easily reviewed (checked/unchecked) prior the modification through the Preview Changes menu.
- Extract Method -
Visual Studio provides a basic method extraction mechanism. Extract Method generates a new method with correctly inferred parameters (from the extracted scope) and substitutes the extracted block code with a call to the method. In addition, Extract Method identifies whether a method uses local class members and if not, suggests marking the extracted method as static.
In the Refactoring process this often indicates that the method should Move to a different class, but Visual Studio Refactoring Tool doesn't provide any suggestions to where such a move is possible.
(There are other tools that do provide the suggestions - patience, patience...).
- Encapsulate Field -
Creates properties for existing members.
- Extract Interface -
Enables extraction of an interface from a type.
- Promote Local Variables to Parameter -
Promotes a local variable to a method's parameter.
When a local variable declaration isn't selected correctly, the tool will prompt with an explanatory menu (first screenshot).
Also, the tool will alert when it cannot guarantee that all the callers pass a legal value to the newly promoted parameter (second screenshot).
- Remove Parameters/Reorder Parameters -
Adjusts a method's parameters.
Clearly, as stated above, this is a very simplistic refactoring tool, especially when comparing to Eclipse. As you can see below, Eclipse comes with much more refactoring steps available out of the box.
There are more than 20 refactoring steps in the Refactor menu.
In addition, one can utilize additional Visual Studio "Refactoring" features.
Visual Studio Additional "Refactoring" Features
- Source Code Length -
Adding Guides to the Vusial Studio IDE will allow to visually emphasis the length of the source code line.
In order to add Guides, you should edit the "HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\X.X\Text Editor" key in the registry (where X.X is the VS version).
Create a string key called "Guides" with the value "RGB(255,0,0), 80" in order to have a red line at column 80 in the text editor.
- Code Definition -
Using the Go To Definition each time you want to examine the referenced method or type is very tedious. That's why the Code Definition Window is handy! As you move the insertion point in the editor, or change the selection in Class View, the Object Browser, or Call Browser, the content of the Code Definition window is updated and displays the definition of a symbol referenced by the selection.
- Code Metrics -
Knowing the code complexity can help writing a more clean and refactored code. Visual Studio provides the Code Metrics Window that provides a better insight into the developed code. I would suggest to pay attention especially on the following metrics:
- Cyclomatic Complexity - Measures the structural complexity of the code.
A program that has complex control flow (and thus a high cyclomatic score) will require more unit tests to achieve good code coverage and will be less maintainable.
- Class Coupling - Measures the coupling to unique classes through parameters, local variables, return types, method calls and etc...
High coupling (and thus a high class coupling score) indicates a design that is difficult to reuse and maintain because of its many interdependencies on other types.
Here is the combined IDE view with all the additional "refactoring" features:
Of course, an experienced Software Engineer can create a clean and refactored code, even with the skim Visual Studio Refactoring Tool and additional "Refactroing" features. The experience though isn't going to be smooth and easy. The refactoring features are scattered and as overall not so user friendly.
- The Visual Studio Refactoring Tool is rather simplistic.
- Most used refactoring steps are: Rename and Extract Method. Those steps provide fairly good functionality (though extra behavior is possible when extracting a method).
- Other significant refactoring steps don't exist; Microsoft leaves the stage to other players.
- Additional refactoring features, as: Source Code Length, Code Definition and Code Metrics exist and provide complementary ways to understand and refine your code. The features, though, scattered across different places which makes it hard to simultaneously work with all of them.
In the next post we will examine additional automatic refactoring tools.