Refactoring VB in Visual Studio 2005, Page 2
Refactoring: Extract Method
One of the most common problems with code is having functions that are too long. Small functions can be reused and more easily re-orchestrated into new behaviors. Long, monolithic functions often can be used only under very tight constraints. To make code more reusable, you can extract chunks of code into named methods. In addition to these named methods being more reusable, the names more clearly explain the purposes of the methods.
To use Extract Method, select a fragment of code that you'd like to convert to a named method, right-click over the selected code, and pick Refactor!|Extract Method. To demonstrate, I use a simple for loop that writes HeartRate to the Console 100 times.
If you have only one available Refactoring, use One Key Refactoring by typing Ctrl+~, which invokes the Refactoring and shows an Action Hint (see Figure 2). Again, move the target picker above or below its current location and press Enter. After pressing Enter, the method PseudoLongMethod and the new extracted method are revised, as shown in Listing 2.
Figure 2: The Currently Available Refactoring and the Action Hint, Extract Method
Listing 2: The Method PseudoLongMethod Is Refactored, Making BunchOfCode a Separate Method
Imports System.ServiceProcess Public Class WillRefactor Private HeartRate As Integer Public Property HeartRate1() As Integer Get Return HeartRate End Get Set(ByVal value As Integer) HeartRate = value End Set End Property Public Sub PseudoLongMethod() ' a bunch of code BunchOfCode() End Sub Private Sub BunchOfCode() Dim I As Integer For I = 1 To 100 Console.WriteLine(HeartRate) Next End Sub End Class
Refactoring: Create Overload
Suppose that BunchOfCode were defined to accept a Count parameter. (Count is used as the upper limit for the loop.) Further suppose that in some cases a default value is known. You can right-click BunchOfCode (see Listing 3) and select Refactor!|Create Overload to create a new method that overloads BunchOfCode and calls the original method with a default value (see Listing 4).
Listing 3: A New Version of Bunch of Code with a Single Parameter
Private Sub BunchOfCode(ByVal count As Integer) Dim I As Integer For I = 1 To count Console.WriteLine(HeartRate) Next End Sub
Listing 4: The Old and New Overloaded BunchOfCode Method Created by Refactor!
Private Sub BunchOfCode(ByVal count As Integer) Dim I As Integer For I = 1 To count Console.WriteLine(HeartRate) Next End Sub Private Sub BunchOfCode() Dim lCount As Integer = 0 BunchOfCode(lCount) End Sub
Refactored Code Is Good Code
Refactoring is important not because the underlying techniques are all new, but because the techniques are described and the motivation and desired outcomes are clearly state. Refactoring can improve the internal structure without changing the external behavior of existing code. It removes the subjectivity of what is good code and what is less-good code: Refactored code is deemed good, and code that is not Refactored is deemed less good. (Perfect code probably does not exist.)
More important than whether code is good or bad is whether it works. Refactoring increases the likelihood that code will continue to work as it grows and evolves.
About the Author
Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his new book UML DeMystified from McGraw-Hill/Osborne and his upcoming book C# Express for the Professional Programmer from Addison Wesley (Spring 2006). Paul is an architect for Tri-State Hospital Supply Corporation. You may contact him for technology questions at email@example.com.
If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org.
Copyright © 2005 by Paul T. Kimmel. All Rights Reserved.