Automating Refactoring

Juan Peña from Artinsoft responds to a recent article on refactoring with suggestions for how to achieve more automated refactoring, and a reader asserts that software engineering should be difficult.

Letters to Visual Studio Magazine are welcome. Letters must include your name, address, and daytime phone number to be considered for publication. Letters might be edited for form, fit, and style. Please send them to Letters to the Editor, c/o Visual Studio Magazine, 2600 El Camino Real, Suite 300, San Mateo, CA 94403; fax them to 650-570-6307; or e-mail them to [email protected].

Automating Refactoring
I was pleased to see Danijel Arsenovski's article on VB.NET post-migration code refactoring in a recent issue ("Refactor Your Way to Migration Success," VSM September 2006). The author is correct when he describes several key steps that can be taken after migrating from VB6 to VB.NET to improve code quality and maintainability. However, I was surprised by the author's assertion that there is no way to implement some of the refactoring techniques through automated translation.

I work as a consultant at ArtinSoft—the company that created the Upgrade Wizard that is mentioned in the article—and I have participated in several migration projects, applying to some extent many of the techniques mentioned in the article. We have produced an improved version of the Upgrade Wizard called the Visual Basic Upgrade Companion Enterprise Edition, which provides some automated code refactoring, such as type inference for late-bound variables (producing explicitly typed variables) and conversion to structured error handling, among many other tweaks that improve code quality. In fact, we are constantly looking for new ways to increase the "migration wizardry" as the article called it, to improve the code as much as we can automatically.

Using artificial intelligence algorithms, it is possible to recognize certain code patterns that can be reorganized into more structured versions, yielding higher quality .NET code. For example, we've been able to recognize commonly used patterns of "On Error GoTo" automatically, analyze them, and convert them to code blocks that use "Try Catch" instead of the legacy error handling model—in many cases with no human intervention. This automated code refactoring and improvement has proved highly successful in migration projects, and has been applied to several applications that are currently being used around the world. This experience highlights the advantages of code refactoring and heralds the reality and potential of automatic refactoring.

Juan Peña ArtinSoft, San José, Costa Rica

Thank you for pointing out that ArtinSoft's improved version of Upgrade Wizard goes beyond the basic version that is distributed together with Visual Studio. I find many of the automated migration options this tool supports very useful, including the ones implementing type inference, or converting "On Error Goto" constructs to structured exception handling.

Still, not all of the refactorings I described can be automated. For example, converting error codes to exception type hierarchies cannot be automated beyond a very basic level, such as replacing error code vbObjectError + 101 with a 101Exception type. Realistic refactoring would involve organizing your exception classes in the form of inheritance hierarchies. Or, to give another example, refactoring code automatically so inheritance facility is used instead of delegation. In VB6, delegation was sometimes used to simulate inheritance.

Refactoring is an extremely useful technique, and its application goes far beyond the migration issue. In a certain sense, migration is just a point in the life of some code. Refactoring encourages programmers to take control of the code and to improve the design without fear. Nothing can replace getting your hands dirty in the process.—D.A.

Don't Go Digital Only
I often read Visual Studio Magazine at lunch, at home before I go to sleep, or in the library—none of these locations have easy access to a computer.

I really like the magazine and adding a PDF version in addition to the print version is fine, but providing it in digital format only is not a good idea.

Joel Hirschman
Received by e-mail

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube