Refactoring Support for Visual Basic 2005
by Wei-Meng Lee05/31/2005
Microsoft recently announced that they have teamed up with Developer Express Inc. to release Refactor! for Visual Basic 2005 Beta 2, a free plugin for Visual Studio that enables Visual Basic developers to simplify and restructure source code inside of Visual Studio 2005. This announcement came shortly after Microsoft indicated that, due to timing constraints, Visual Studio 2005 will not come with refactoring support for VB2005 (though it is supported in C#; see my earlier article here). This move has sparked a lot of developer displeasure, again raising the question of whether Microsoft ever took VB seriously. Now, with the release of Refactor! for VB2005, a lot of VB developers are smiling again.
In this article, I will take you on a tour of Refactor's refactoring feature and how you can use it to improve your code. One significant advantage of using Refactor is that all of the refactoring is done inline, with no pop-up dialog boxes to block your view of the code in question. So let the tour begin!
Extract Method
Suppose you have the following block of code:
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim num, sum As Integer
num = 10
For i As Integer = 0 To num
sum += i
Next
End Sub
You can extract the For loop as a method. To do so, highlight the block that
you want to extract as a method, right-click, and select Refactor -> Extract
Method (see Figure 1).

Figure 1. Extracting a block of code as a method
You will be asked for the position to which to insert the new method (see Figure 2). You
can use the cursor key on your keyboard to move up or down. Press Enter to
insert the new method as indicated by the arrow. Pressing Esc will cancel this
add new method operation.

Figure 2. Adding a new method
Refactor automatically uses an appropriate name for this newly inserted method. The code that originally contains this block of code would be replaced with a method call. The big red arrow indicates the newly added method (see Figure 3).

Figure 3. The newly added method
If you don't like the name suggested, you can always click on the method name (highlighted in green) and the corresponding method call will be changed as well (see Figure 4).

Figure 4. Changing the method name
Reorder Parameters
Suppose you have the following block of code:
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim radius As Single = 3.5
Dim height As Single = 5
'---Volume of Cylinder
Dim volume As Single = 3.14 * radius ^ 2 * height
End Sub
You can extract the statement that calculates the volume of a cylinder as a method, using the steps as described in the last section (see Figure 5).

Figure 5. Extracting the block of code as a method
You can change the name of the method to VolumeOfCylinder to better reflect the
purpose of the function (see Figure 6).

Figure 6. Changing the name of the method
Notice that the new method now has two input parameters (radius and height).
You can change the order of the parameters; for example, you might want to pass
in the height of the cylinder first, then followed by radius. In this case,
right-click on either of the parameters and select Refactor -> Reorder
Parameter. You can use the cursor keys to reorder the parameters and press Enter
when you are done (see Figure 7).

Figure 7. Reordering the parameters
When you press Enter, Refactor will update the calling statement and ask that
you confirm the changes (see Figure 8). Click on the "tick" icon to confirm the
change.

Figure 8. Confirming the changes
|
Related Reading Visual Basic 2005: A Developer's Notebook |


