
Working with Code Refactoring
• C# projects developed within Visual Studio 2005 have intrinsic support for code refactoring.
• Simply put, ‘refactoring’ is the process of changes an existing code base so it is somehow ‘better’.
• A ‘better’ code base may entail adhering to better OO design, performance gains, maintainability, etc.
• Visual Basic 2005 projects developed with Visual Studio 2005 do not have support for code refactoring!
• For whatever reason, Microsoft chose to leave out refactoring capabilities for the VB 2005 crowd.
• Thankfully, Microsoft does offer a free add-on named Refactor! (developed by Developer Express Inc).
• http://msdn.microsoft.com/vbasic/downloads/2005/tools/refactor
• For this section of the chapter, we will focus on the built in C# refactoring support.
• If you are VB 2005 developer, you should have no problem understanding the refactoring process using
the Refactor! add-on.
• Do know that while there is no ‘master list’ of refactorings, most in the development community do
agree upon a well-known set of refactorings.
• Many of the common refactorings come from the book Refactoring by Martin Fowler.
• This book is supported by the web site http://www.refactoring.com
• Visual Studio 2005 supports the following built in refactorings:
C# Refactoring Technique Meaning in Life
Extract Method This allows you to define a new method based on a selection of code statements.
Encapsulate Field Turns a public field into a private field encapsulated by a .NET property.
Extract Interface Defines a new interface type based on a set of existing type members.
Reorder Parameters Provides a way to reorder member arguments.
Remove Parameters As you would expect, this refactoring removes a given argument from the current list
of parameters.
Rename This allows you to rename a code token (method name, field, local variable, and so on)
throughout a project.
Promote Local Variable to Parameter Moves a local variable to the parameter set of the defining method.
• All of these refactorings can be activated using any of the following techniques:
• The Refactor main menu item.
• The Recactor right-click context menu.
• Related keyboard shortcuts.
• If you desire a detailed walkthrough of each refactoring (and the refactoring process in general) consult
the article "Refactoring C# Code Using Visual Studio 2005" from either of these locations:
• MSDN Online (www.msdn.com)
• The .NET Framework 2.0 SDK Documentation.
Refactoring
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials