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

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube