Posted by & filed under Visual Studio.


Print this document | Close window



Refactor! from Developer Express is a plug-in for Visual Studio .NET 2005 beta 2 that enables developers to simplify and shape source code, making their code easier to read and less costly to maintain.

What is Refactoring

Refactoring describes the process of making many small changes to working code, improving the internal structure without altering the external behavior of that code.

As an analogy, consider the math statement 2*4*5*3, you can covert this to 1*2*3*4*5 without changing its meaning and from there into 5!. They all mean the same thing but are increasingly easier to read. Refactor! does the same thing but with working source code.

There was a time not too long ago when just for fun developers would actually compete in obfuscated code competitions to produce the most difficult to read code. Well unfortunately there is a lot of code out there that is not much more readable than that intentionally obfuscated code – which is where refactoring comes in. The goal of refactoring is to take working code that is harder to read than it needs to be, and tweak its structure to make it more readable and more easily maintained.

Visual Basic was designed to self-document and encourage the development of readable, maintainable code – but even Visual Basic code can grow organically over time and become cluttered and overly complex. Refactor! has some features that can help you take that complex code and make it easier to read and less costly to maintain.

Why Refactor!

Refactor! does something no other tool does: it brings the power of an entire suite of refactoring tools to bear with just one key (”One Key Refactoring”) and it works directly on your code in your editor – so you’ll never have to leave your code just to restructure it.

Figure 1. One Key Refactoring – press Ctrl+~ at any point to see all available refactorings with a short description of each.

Refactor! is built upon a language neutral architecture that offers exactly the same functionality for Visual Basic .NET and C# developers. Refactor Pro even includes extensibility features to allow developers to build language services for any Visual Studio language.

Before I show you the list of refactorings, let me assure you that everything you can do with Refactor! can be rolled back using the standard Visual Studio Undo/Redo mechanism. So you really have the freedom to explore with this tool – simply hit ctrl-Z (or whatever key Undo is bound to) if the refactoring is not what you expected.

Refactoring: Reorder Parameters

Change the order of parameters in a Method signature, while also updating all calls to this method. Great when you need to change the parameter declaration order so that the most important parameters appear first, which will improve the usability of the method.

“Reorder Parameters” is probably the best example of how Refactor! is different, because the entire process is extremely visual and happens right there in your code. Here is how you reorder your parameters. Simply move the text cursor into the declaration of a Method with parameters and this refactoring will be available. You can see that it is available if you stay there for a moment, because the refactoring smart tag will show up with a list of all the refactorings available. But you don’t need to wait for anything – just hit the “One Key Refactoring” key (Ctrl+~) and now you can move all your parameters left and right simply using the left and right cursor keys.

Figure 2. Reordering parameters directly upon code in the Editor – no modal dialogs to slow you down.

This white paper can not possibly do justice to the visual impact of this refactoring, there really is no substitute to trying this one out yourself.

Refactoring: Rename

This refactoring renames a local variable or a private field, property or method. This refactoring affects both the declaration and any references within the class.

Choosing meaningful identifier names is one of the simplest ways to improve the readability of your code – Refactor! makes this process trivial. You simply put the text cursor into the identifier, and hit the “One key refactoring” key (Ctrl+~), and start changing your identifier. You will see all references to your identifier highlighted with a light green box. You can now cycle through each call using the Tab and Shift+tab keys, and if you change any one – the others all reflect the change.

Figure 3. “Rename” connects matching identifiers so that a change to one happens to the rest.

Refactoring: Extract Method

The most sophisticated of all class based refactorings is probably “Extract Method”. This refactoring allows you to select a piece of code from inside a method and extract it outside into a method of its own – while leaving a call to that new method in the code the method came from.

You are probably already several steps ahead of me in how to activate this refactoring by now; You simply select some code, put the text cursor in the code, hit the “One Key Refactoring” key (Ctrl+~), and now you have a new method containing your block of code, with a call where that code came from.

Consider this example code which calculates the volume of a cylinder. Suppose we decide to extract the code that calculates the area of a Circle ito a method of its own, for later reuse. To do this all we do is select the specific block of code and hit the “One key Refactoring” (Ctrl+~), the code is cut from the source method, and we are prompted for a location for the target method.

Figure 6. You can place the extracted Method precisely using the red Target Picker.

Once we have chosen the location for the new Method, Refactor! calculates how many parameters need to be passed, creates some default names for the new method and any parameters, generates the code for the new method and calls it, and finally invokes the “Rename” refactoring in case we wish to override any of the default names chosen.

Figure 7. The fully extracted method – Refactor has determined parameters to pass and even come up with a meaningfull name.

You know there is actually an even faster way to extract. Select the code – Cut the code – Paste it outside the Method but inside the class and Refactor! will extract the method for you automatically.

Notice that the MethodName and the call are both linked with green boxes – the “Extract Method” has automatically invoked the “Rename” refactoring for you. Also if there are any variables referenced outside as well as inside the block you selected, then Refactor! will automatically give your new method the appropriate parameters to pass in that data and if needed pass it back.

Refactoring: Create Overload

Sometimes you have a particularly useful method that you want to make available with alternate parameter signatures – the “Create Overload” method automates the process for you of creating an overload or copy with default parameters.

I’m sure you know the drill by now, Move the text cursor into the signature of the method to be overloaded, hit the “One Key Refactoring”, select where the overload will be (above or below the original), and select the parameters to be removed from the copy’s signature and set to default values. Refactor! will even try to come up with some appropriate defaults for you.

Refactoring: Extract Property

Moves the selected code to a new read-only property. Inserts the appropriate calling code into the source method or property.

Extract property is useful when you find you have an expression that you are using a lot, as it will take a chunk of code and move it into a new read-only property. Turning an often-used expression into a property eliminates code duplication and makes the code easier to read.

Refactoring: Introduce Local

Creates a new local variable initialized with the selected expression. Optionally replaces all occurrences of the expression inside this code block with a reference to the newly declared local variable.

Converting this contrived example;

Button1.Text = “Hello World”

To this:

Dim lNewVariable As String = “Hello World” Button1.Text = lNewVariable

Introduce local is useful when you have a complicated expression or calculation that has been repeated multiple time in a code block, and you want to consolidate these multiple calculations into a single call. This will help reduce the complexity of code management because future changes to the code will be isolated to one place. Introduce local is also useful when you want to replace the calculation with a local variable that has a name that better explains what the calculation does – improving the readability and maintainability of your code.

Refactoring: Inline Temp

Replaces all references to a local variable with its initial value, essentially the Inverse of the “Introduce Local” refactoring.

Converting this contrived example;

Dim AreaOfCircle As Integer = GetAreaOfCircle(Radius) Return AreaOfCircle * Height

To this;

Return GetAreaOfCircle(Radius) * Height

Inline Temp is also useful if you want to prepare a code block for extraction (to a method or property). If the selected code block references locals declared above that block, you can reduce the codes dependence on local-variables by inlining all those references.

Refactoring: Replace Temp with Query

Replaces all references to a local variable with a call to a property or method that returns the variable’s initial value. The “Replace Temp with Query” refactoring is useful when you have a variable local to a method or property that you would like to make available from other methods or properties inside your class. It is also useful for reducing the number of local variables in your methods, which will reduce interdependencies for extracting methods, and other refactorings.

Refactoring: Split Temporary Variable

This refactoring addresses the problem where a developer reuses a variable for a different purpose in the middle of a block rather than creating a new variable.

Figure 8. The variable lSayHello is being hijacked by this code to have a different behavior.

This particular anti-pattern is a bad one because the meaning of the variable changes depending on its position, making the code very hard to understand.

Figure 9. “Split Temporary Variable” creates a new variable gives it a new name and invokes the “Rename” refactoring.

The refactoring will introduce a new local variable instead of multiple assignments to the same variable, giving you the chance to change the name of the variable to something meaningful.

Refactoring: Move initialization to declaration

Combines the declaration of a local variable with its initialization or first assignment. This is the inverse of the “Split initialization from declaration” refactoring

Converting this contrived example;

Dim lSayHello As String lSayHello = “Hello World”

To this;

Dim lSayHello As String = “Hello World”

Refactoring: Split initialization from declaration

This refactoring breaks an initialized declaration for a local variable into a declaration and a separate initialization statement. This is the inverse of the “Move Initialization to Declaration” refactoring.

Converting this contrived example;

Dim lSayHello As String = “Hello World”

To this;

Dim lSayHello As String lSayHello = “Hello World”

Refactoring: Move declaration near reference

Moves the declaration statement for a local variable near the first reference of that variable. This refactoring optimizes the placement of a declaration for maximum readability. This is especially useful if you have a variable used exclusively inside a block, as the declaration can then move inside the block reducing possible complexity during refactorings like “Extract Method”.

Refactoring: Reverse Conditional

This simple refactoring reverses the condition of an if statement and switch the Then and Else blocks.

Converting this contrived example;

If Radius 0 Then Return 3.14159 * Radius * Radius Else Return 0 End If

To this;

If Radius = 0 Then Return 0 Else Return 3.14159 * Radius * Radius End If

Refactoring: Simplify expression

This refactoring uses the Quine/McClusky simplification algorithm to simplify a conditional statement. . This involves removing unnecessary parentheses, toggling negations, parameter reordering and so on without affecting the expression’s return value but making the entire expression easier to read.

eg: This can be as simple as converting not (A0) into A=0.

Conditions tend to accrete as code acquires functionality so being able to simplify conditions easily is a low cost way to quickly improve the readability of your code significantly.

Refactoring: Introduce Constant

This refactoring converts a string or number value into calls to a new constant identifier that has been initialized with the value. This gives you the chance to give the constant a meaningful name.

Converting this contrived example;

Private Function GetArea(ByVal Radius As Integer) As Integer Dim Area As Integer = 3.14159 * Radius * Radius Return Area End Function

To this;

Const Pi As Double = 3.14159 Private Function GetArea(ByVal Radius As Integer) As Integer Dim Area As Integer = Pi * Radius * Radius Return Area End Function

Introduce Constant is similar to Replace Magic Number with Symbolic Constant in Martin Fowler’s Refactoring book. However, Introduce Constant works on strings in addition to numbers.

Refactoring: Encapsulate field

Encapsulates a field into a read-write property and replaces occurences of this field throughout the class with the newly declared property.

Figure 10. Extract the field SayHello to a property.

This refactoring is useful if you need to add some accessor logic to every request of the data. Note that even though the new property has been named the same as the previous field, the “Rename” refactoring is invoked to link all references as this may be an opportunity to give the property a more meaningful name.

Figure 11. To get a new property SayHello that references a private field _SayHello.


Refactor! brings Visual Basic.NET 2005 developers a suite of powerful tools for simplifying code. Tools that are surfaced on your code in the editor, for immediate access rather than the traditional solution of modal dialogs that simply slow you down and take you away from your code.

Probably the most important feature of Refactor! is that it makes refactorings accessible, reducing the risk and thus cost associated with improving the structure of your code.

Even C# developers can take advantage Refactor! by purchasing Refactor! Pro that includes not only language support for both Visual Basic.NET and C#, but also support for Visual Studio 2002, and Visual Studio 2003., as well as additional refactorings and the tools to create your own refactorings in any Managed language.

For more information see

Comments are closed.