Refactoring Tools for Visual Basic

What the tools are and how to use them

What is "refactoring" anyway? It's actually different things to different people. In this article, we will look at five tools that can be used for refactoring to see what the differences are. And just to show that I haven't given up on VB 6 (like Microsoft has), the first four can be used for VB 6!

The "mainstream" definition of refactoring can be found in Martin Fowler's book, Refactoring:

"Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure.

It is a disciplined way to clean up code that minimizes the chances of introducing bugs. In essence, when you refactor you are improving the design of the code after it has been written."

But it's easier to define refactoring than to actually do it. Most refactoring is done by what has been called Biological Development Units (BDU's). In other words, "programmers". The main reason is that it's just as hard, or maybe even harder, to "improve the internal structure" of a program as it is to write it in the first place. The question does come up, "Why didn't the original programmer structure the code differently?" And, very often, the answer is, "Because it doesn't work that way." ... for some very subtle reason that you never suspected.

The first "golden rule" in refactoring is "Test! Test! Test!" And when you have done that, "Test some more." The last thing you want to do is "improve" a program by breaking it.

The second rule is, "Back up your code before you change it!" The wrong time to find out that you have introduced a new bug is after you have made changes that you can't undo.

Most of the tools reviewed here can be used with VB 6. There is still a lot of VB 6 code out there and it's still a good development environment ...

if you have the VB 6 compiler available to you. (Microsoft won't sell it anymore.) As Warren Sirota -- the author of the CodeShine tool -- writes:

"There is a tremendous amount of legacy code lying around, and not every application is worth rewriting. I have a client with a simply huge commercial VB6 app, for instance. Selling him on giving me umpteen thousand dollars to move it to .NET, with the only benefits being 'better structured code, more OO, possibly lower maintenance costs way down the road', is simply not going to happen until VB6 apps no longer run under Windows - and we know that VB 6 apps run under Vista, so that will be a long time."

Microsoft's Reverse Engineer UML Modeling

This VB 6 tool isn't strictly a refactoring tool. It might almost be called a "starting over" tool. It was provided free by Microsoft and if you're a large customer, you might have enough clout to get them to give it to you again. ... Even though it's against company policy these days to do anything nice for VB 6'ers.

The Visio Reverse Engineer UML Modeling tool installs as an add-in for VB 6.

After it's installed, you have a new toolbar with four new icons in your VB 6 development environment. (See the illustration below.) As of publication date (March 2007), MSDN still had some documentation for using this tool and VB 6 here. But they also have some much more recent instructions (but not the tool) for .NET here. I couldn't find it available for download anywhere but if you have been an MSDN subscriber for a while, you might find it on your CD's. If anyone knows how to get it, please drop me an email.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Visual UML

UML has a deep history and great credibility with a lot of programmers, so you can also buy products from third parties to do the job. One of the most popular is sold by Visual Object Modelers, a company from the Silicon Valley of the Rockies - Boulder, Colorado.

Visual UML also supports both VB 6 and VB.NET.

And, like most of the tools reviewed here, it also installs as an Add-in to the Microsoft development environment. But you have to have deeper pockets to get it. They're asking at least $500 for a copy. It's a reasonable price when you consider that a copy of Visio - necessary to do the UML diagramming with Microsoft's "free" tool - will cost you several hundred.

This tool is designed for UML believers and includes the ability to import UML models, create code from UML and export models in addition to being able to reverse engineer existing Visual Basic programs. So you're getting into the UML big leagues if you go for this tool. Like other vendors, they also provide a "free trial" version.

Reverse engineeringing into UML might be called the "gold standard" refactoring technique. "Gold standard" because it's likely to yield the most valuable result and also because it's really expensive. You need extensive training and experience to do UML right. And you also need a lot of time for the analysis, redesign, and reprogramming.

The idea is to create a UML model of the actual running code and, after a thorough analysis, write it again. In other words, "start over." This technique does have the added advantage that you don't have to write the next version in the same programming language. Since Microsoft is leaving us few choices about moving from VB 6 to VB.NET, this might be a more practical choice today.

Microsoft describe the four steps in using the tool as follows:

Step 1: Customize the Development Environment
Step 2: Open the Code Project to Reverse Engineer
Step 3: Select the Reverse Engineer Button
Step 4: Create your UML Diagrams in the Visio UML Model

That last step is the hard one. But the first three steps do give you a head start. The illustration above shows what the tool creates automatically (up to the hard step) for About Visual Basic's VB 6 TicTacToe program.

CodeShine

The CodeShine program is an example of a more typical refactoring process. This third-party utility also installs as an add-in to the VB 6 development environment and offers help in automating four standard techniques:

  • Extract Method
  • Introduce Explaining Variable
  • Extract Function
  • Localize Module Variables

In contrast, the "free" Refactor! tool (reviewed below) offers about fifty and the "Pro" version has seventy-five.

To see how well CodeShine performed, I picked on About Visual Basic's VB 6 TicTacToe program. At the top of the code, there is a short double nested loop that does nothing but clear the TicTacToe playing grid.

' Get ready to play another game
Sub InitPlayGround()
Dim i As Integer
Dim j As Integer
   ' Erase any playing grid symbols
   For i = 1 To 3
      For j = 1 To 3
         lblPlayGround((i - 1) * 3 + j - 1).Caption = ""
         iXPos(i, j) = 0
         iOPos(i, j) = 0
      Next j
   Next i
End Sub

The idea behind Extract Method is to simply replace this simple initialization routine with a subroutine and a call that does the same thing.

Here's the end result after using CodeShine:

' Get ready to play another game
Sub InitPlayGround()
ClearGrid
..........

Private Sub ClearGrid()
Dim i As Integer
Dim j As Integer
   ' Erase any playing grid symbols
   For i = 1 To 3
      For j = 1 To 3
         lblPlayGround((i - 1) * 3 + j - 1).Caption = ""
         iXPos(i, j) = 0
         iOPos(i, j) = 0
      Next j
   Next i
End Sub

As you can see, the process is one that you could do on your own. But CodeShine includes some integrated error checking to guard against mistakes and automates the process.

"Introduce Explaining Variable" simply substitutes a meaningful variable (like "summer_season") for confusing "magic numbers" in the code (like 6, 7, 8 for June, July, August).

Localize Module Variables checks to see if variables need to be local or global (or are needed at all) and makes the appropriate adjustments.

The good news is that CodeShine has an easy to use interface and direct, straightforward functions. The bad news is that it unfortunately still seems to be a "work in progress". While testing the system, the add-in code crashed for a variety of different reasons. But I encourage you to try out the system yourself. 

Aivosto Project Analyzer

This is a much more professional refactoring system (at four to thirteen times the price) offered by the toolmaking software company Aivosto. They have a variety of utilities that can help you write better code faster and they promise that their systems work on VB 6, VBA, and VB.NET. Although the cost is a lot more than CodeShine ($75 versus $300 to $990, depending on the version), you get at least that much more in function.

And it's a high-quality product. It runs fast and doesn't crash.

Martin Fowler writes that Project Analyzer is "not strictly a refactoring tool" and I agree. The reason is that it doesn't actually change anything. It just tells you a lot about the code that will help you figure out whether it needs changing and what to change. But then you have to do the actual coding.

There are so many features in Project Analyzer that it's not practical to try to cover them in an article like this. To give you an idea about what it does, the illustration below shows you the VB 6 TicTacToe program in Project Analyzer. The GamePlay procedure is selected and the Project Graph display shows exactly how this procedure is called and what procedures it calls.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The reports on VB.NET are just as fast and revealing ...

if a tad confusing sometimes. For example, Project Analyzer doesn't understand that form1.vb isn't actually dead code (unused code). But the reports and screens tell you volumes about your program that Visual Studio doesn't.

Refactor! for Visual Basic .NET 2005

If you search for refactoring tools at Microsoft, this is the one that you'll find.

It's actually a third party tool from another tool software company, Developer Express, Inc. The cynical among us might wonder whether Microsoft promotes it because it doesn't support VB 6. But the price is right -- it's free. (If you own the full product, Visual Studio .NET 2005. It doesn't work with Express Edition.)

Refactor! is another tool that installs as an integral part of Visual Studio. After the installation, your initial impression is, "Hey! Nothing changed! Where's the tool?" But then, like Intellisense or Code Snippets, you start to notice it. When you select a block of code, you may notice a new icon that appears in the display. If you select it, you can then see Refactor!'s graphics and options. Like Visual Studio itself, it takes a little getting used to. The big blue arrow in the illustration below is hard to miss.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The weakness of this approach is that you have to select the right block of code to see Refactor!'s suggestions. I found myself taking more time by selecting some likely code just to see if Refactor! had any suggestions. It would be nice if it had a global "analyze" function ...

but it doesn't.

For an extra $100 (currently), you can get the "Pro" version which supports two other languages and includes seventy-five refactoring techniques. To see what a "technique" is, see the example in the discussion of CodeShine earlier. Refactor!'s techniques are like that, but there are a lot more of them.