WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Refactoring in Visual Basic 2005

by Matthew MacDonald, author of Visual Basic 2005: A Developer's Notebook
07/26/2005

Ever since Visual Basic developers heard about C# refactoring--a set of features that help developers restructure code--they've been green with envy. Surely it makes just as much sense to give VB'ers a similar set of refactoring tools for managing and modifying their code.

Although VB 2005 refactoring was a top developer request, the VB team had no way to fit in such a significant change late in the beta cycle. (Instead, they're hard at work putting the finishing touches on edit-and-continue and a library of more than 500 code snippets.) However, they came up with an innovative solution that might be just as good--partnering up with another company to include support for basic refactoring. Today, you can download a free version of Refactor, the Developer Express refactoring software. By the final release of .NET 2.0, Refactor will be included with Visual Studio, and you'll be able to buy a more configurable and extensible version of the same tool from www.devexpress.com.

Refactoring Basics

Refactoring is the one-size-fits-all name given to a set of coding shortcuts that help you rework code on the fly. The distinguishing characteristic of refactoring is that it's intelligent. For example, you're already familiar with basic IDE features like cut and paste or search and replace. The refactoring versions of these features understand VB syntax. That means they can dynamically tailor other portions of your code to keep it synced up and simplify your work.

The best way to learn to work with refactoring is to try it out. So download the free Visual Basic 2005 refactoring tool, and then experiment with some of the techniques in the following sections.

Encapsulate Method

This refactoring trick is a great way to tame large blocks of code by splitting a portion off into a separate method. It's also one of the easiest refactoring tools to get under your belt.

Imagine you start with the following console application:


Module InvestmentCalculator

    Sub Main()
        Dim InterestRate, Years, FinalValue, MonthlyPayment As Double
        Console.Write("Interest Rate: ")
        InterestRate = Val(Console.ReadLine())
        Console.Write("Years: ")
        Years = Val(Console.ReadLine())
        Console.Write("Final Value: ")
        FinalValue = Val(Console.ReadLine())
        MonthlyPayment = Pmt(InterestRate / 12 / 100, _
            Months, 0, -FinalValue, _
                DueDate.BegOfPeriod)
        Console.WriteLine("Result: " & MonthlyPayment.ToString("C"))
    End Sub

End Module

The code here is quite straightforward. It asks the user to enter three pieces of information (an interest rate, number of years, and final value) and calculates the final value with compounded interest using the Pmt() function. The problem is that the calculation code is intermingled with the code for getting the user input and displaying the result. Although it's still manageable in this example, it's not hard to imagine a much more complicated set of calculations that would introduce some real organizational headaches. To start simplifying this code, you can use refactoring.

Once you've installed Refactor, you have three ways to trigger it. First, position your code to a place where refactoring is available. This part takes a little getting used to, but as you play with the tool you'll start to learn where the refactoring points are in your code (for example, in a variable declaration, a method declaration, and so on). When your code is in the right place, you'll see a small smart tag marker appear. In this case, start by selecting the content of the Main() method.

Now, simply click on this marker to pop open a menu with your current refactoring choices (see Figure 1). Alternatively, you can press Ctrl-tilde (~), or right-click on the program element and look for the Refactor submenu.

Figure 1
Figure 1. The refactoring tag

If you don't see the refactoring marker when you expect to, the problem could be an error in your code, as the refactoring smart tags won't appear if there's an active IntelliSense tag flagging an error.

Pages: 1, 2, 3

Next Pagearrow