Visual Studio Refactoring Basics


The Visual Studio refactoring tools work to ensure you see the promises of refactoring: increased reuse of your code, fewer rewrites, reduced duplication, and better readability. These tools work to instill confidence in the edits they make to your code. They do so by using a common refactoring engine based on the C# compiler rather than string matching and search-and-replace. The engine and compiler work together to cover the entire code base (and its references) to find all possible changes that need to be made as part of a given refactor operation. The engine even searches out code comments and tries to update them to reflect new type names. In addition, you can preview changes to your code before they happen. This adds further to your comfort level with the modifications these tools are making to your code.

Table 8.1 presents a high-level overview of the many refactoring operations that are possible with the C# editor. We will cover each of them in great detail in the coming sections. Prior to this, however, we will cover some of the common elements of the refactoring process. These elements include both invoking a refactoring tool inside Visual Studio and previewing the refactoring changes as they happen.

Table 8.1. Refactoring Tools Inside the Visual Studio 2005 C# Editor

Tool

Description

Rename

Renames fields, properties, methods, and variables.

Extract Method

Creates a new method using existing code within a different method or routine.

Promote Local to Parameter

Moves a local member of a method to a parameter of the method.

Reorder Parameters

Changes the order of parameters for a given method.

Remove Parameters

Removes a parameter from a method.

Encapsulate Field

Quickly creates a property from an existing field.

Extract Interface

Creates an interface from an existing class or structure.


Invoking the Refactoring Tools

The refactoring tools are available from wherever you work on your C# code inside Visual Studio. You can invoke them in a number of ways. For example, if you are working inside the code editor, you can invoke the Rename tool using a smart tag. You can also select and right-click code to reveal the refactoring options; these same options are available on the menu bar through the Refactor menu. Finally, you can refactor directly from the Class Designer as you edit and change various items within a given class.

Using the Refactor Menu (and Right-Click)

The most common place to invoke the refactoring commands is from the actual refactoring menu. This menu item is added to the IDE when you have a C# code window open and active. Figure 8.1 shows the menu being invoked. Note that the portion of code you are trying to refactor is highlighted in the IDE. In this case, we want to rename the class Product because we have determined that all our products are books. Therefore, we will rename the class Product to Book.

Figure 8.1. The Refactor menu in action.


This same menu is available via a right-click within the editor. Again, you either highlight a word or section of code (or simply position the cursor accordingly) and then right-click. The top item in this menu is Refactor. This menu folds out to reveal the full Refactor menu (as shown in Figure 8.2).

Figure 8.2. The Refactor menu invoked via a right-click with the mouse.


Refactoring in the Code Window via Smart Tags

The smart tag was introduced in Microsoft Office. Its goal was simple: Try to understand what the user was typing and offer additional, "smart" functionality. For example, as you type in the Word editor, it tries to understand when you've typed a name or address. In these cases, the editor provides you with additional functionally such as Create contact or Schedule appointment.

The C# code editor provides a similar, smart-taglike feature. The editor detects your typing and understands when you have made a change to your code that is the equivalent of a refactor. In these cases the editor creates a smart tag that can be used to invoke the refactoring tool. This allows you to stay in the code editor but still take advantage of the increased productivity and reduced error rate that the refactoring tool can provide.

As an example, suppose you have a property named ID. You want to rename this item to Identifier. You open the class file and position your cursor near the property name. You then type the new property name. The C# code editor detects your change and underlines the final character of the changed name. Figure 8.3 shows an example of this. Notice the small rectangle beneath the r in identifier.

Figure 8.3. Renaming a property invokes a smart tag for refactoring.


This small rectangle hovering underneath the change is your visual cue that the C# code editor thinks it can help you. You can position your mouse cursor next to the rectangle to reveal an in-line menu that indicates how the editor might help. Figure 8.4 shows the smart tag as it is invoked relative to the previous example.

Figure 8.4. Invoking a refactor via a smart tag.


Tip

You can also invoke smart tags without grabbing for the mouse. To do so, press the key combination Shift+Alt+F10.


Using the Class Designer to Refactor

Visual Studio 2005 provides a Visual Designer for working with classes. This Class Designer allows you to view the contents of your classes and their relationships. It can also be used as a productivity tool; you can create new classes and modify existing classes directly within the designer (see Chapter 21, "Modeling," for additional details.)

The Visual Studio Class Designer exposes the refactoring tool when you're working with C# classes. This ensures that code modifications made using this Visual Designer take full advantage of refactoring. For instance, suppose you want to rename a property from within the Class Designer but also want to make sure that the references to that property are automatically updated. You can do so by right-clicking the property within the Class Designer and choosing the Refactor menu's Rename option. Figure 8.5 shows refactoring from directly within the Class Designer.

Figure 8.5. Invoking refactoring from within the Visual Studio Class Designer.


Previewing Changes

As you become comfortable with the refactoring tools, you may decide to simply let them do their thing without much oversight on your part. However, if you are like most developers, then no one (or no tool) touches your code without your consent. Fortunately for us, the refactoring tools provide a preview option. This option lets you follow the tool through its changes and, in turn, accept or reject a given change.

The Preview Changes dialog box is invoked as an option (check box) on a given refactoring operation (or in the case of a smart tag, from a second menu item on the smart tag menu). Figure 8.6 provides an example of selecting the Preview Reference Changes option from the Rename refactor operation.

Figure 8.6. Invoking a refactor operation with Preview Reference Changes.


After the refactoring operation is invoked with Preview Changes, Visual Studio presents you with the Preview Changes dialog box. The top portion of this dialog box lists all of the changes the given refactor operation intends to make. This list is presented as a tree, with the outer branch representing where you intend to originate the change. The leafs under this branch are all files where changes will happen. Nested beneath the filenames are the actual places within the code that a change will be made. You use this list to select each item you would like to preview. Figure 8.7 shows an example of the many changes required when changing a class name from Order to CustomerOrder.

Figure 8.7. Previewing changes of a Rename operation.


As each item in the Preview Changes tree is clicked, the corresponding code is displayed below the tree in the Preview Code Changes section of the dialog box. This allows developers to quickly review where changes will be made. To prevent a given change, you can simply uncheck the item in the tree view. Of course, you can prevent entire file changes by unchecking further up in the hierarchy at the file level. When you are finished with your preview and satisfied with the proposed changes, you simply click the Apply button to commit the changes to your code.




Microsoft Visual Studio 2005 Unleashed
Microsoft Visual Studio 2005 Unleashed
ISBN: 0672328194
EAN: 2147483647
Year: 2006
Pages: 195

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net