Rename


Renaming code elements is the most common refactoring operation. In a typical refactoring session, renaming often makes up the bulk of the work. However, most renaming happens outside the normal refactoring window. Developers do not typically wait until the code base is operational and say to themselves, "Okay, now I will go back and rename these 10 items for clarity." Although this does happen, the more likely scenario is that as you build your application, you consistently rename items to correct mistakes or make things clearer and more readable. Of course, as the code base builds, renaming classes, methods, fields, and the like becomes more and more difficult without introducing new bugs into your code.

Therefore, the ability to rename items with the confidence that you will not introduce bugs into your code is paramount. The C# editor in Visual Studio 2005 provides just this feature. With it, you can rename all relevant code items including namespaces, classes, fields, properties, methods, and variables. In fact, the Rename operation can even search through your code comments and update them accordingly.

Accessing the Rename Operation

You can rename from many places within the IDE. In the previous "Invoking the Refactoring Tools" section, we looked at accessing Rename from the Refactor menu, a right-click, a smart tag, and the Class Designer. You can also access Rename from the Object Browser and the Solution Explorer. In addition, if you use the Properties dialog box to change the name of a control you've placed on a form, the Rename operation is invoked behind the scenes and the control gets renamed appropriately.

From the Object Browser, you can access the Rename operation only from the Refactor menu. Of course, you need to be browsing your own types in your solution. You simply select the item you want to rename and then click the Refactor menu. Figure 8.8 shows an example of the Object Browser and Rename operation working together.

Figure 8.8. Accessing the Rename operation from the Object Browser.


You can rename within the Solution Explorer for filenames that equate to class names. For instance, if you select a file named Customer, right-click, and choose Rename, Visual Studio will rename the file. In addition, it will search the code within the file for any class that had the same name as the file. So if you had a Customer class and a Customer.cs file, a Rename operation will rename the file as well as the class (and all references to the class). Note that while an undo on the Rename operation will roll back a change, in the case of a filename change, Undo reverts the code but does not change the filename back to its original name.

Tip

You can access the Rename operation from a set of command keys. Past versions of Visual Studio introduced the concept of chords. They are like traditional keyboard commands, but you press (or play) them in sequence. For instance, to invoke the Rename operation without touching your mouse, position your cursor over what you want to rename. Then press the sequence Ctrl+R, Ctrl+R. Pressing this combination in sequence will bring up the Refactoring, Rename dialog box relative to the code element behind your cursor.


Working with the Rename Dialog Box

The Rename dialog box allows you to specify a few options when invoking a given Rename refactor operation. Figure 8.9 presents an example of the dialog box. The two text boxes on the form allow you to define the rename itself. In the New Name section, you indicate the new name of the element to be renamed. The Location text box indicates the namespace of the element to be renamed. Of course, all referenced locations will also be searched.

Figure 8.9. The Rename dialog box.


The Rename dialog box also presents developers with a few options (all off by default) when doing a rename. The three check boxes below the Location text box allow you to set the options as described in Table 8.2.

Table 8.2. The Rename Dialog Box Options

Option

Description

Preview Reference Changes

This option allows you to indicate whether you want to preview the changes before they are applied to your code. This capability can be especially useful if you are renaming for the first time or you intend to rename items inside strings or comments. Renaming strings and comments does not use the compiler. Instead it uses string matching. Therefore, it may not always be accurate or the intended result. Figure 8.10 shows the Preview Changes Rename dialog box with a string rename (specifically the text on a button).

Search in Comments

This option allows you to indicate whether the Rename operation should search your comments for possible textual references that should be renamed. Comments often refer to types and hence need to be synced with changes. The alternative is a set of very confusing comments. Figure 8.10 shows the Preview Changes Rename dialog box with a Comments folder and a sample comment that needs to be renamed.

Search in Strings

This preference allows you to indicate whether the Rename operation should search inside your literal strings for references to a given item name. String literals include constants, control names, form names, and so on. This capability is most useful if there is a tight coupling between your code and the elements within your user interface.


Figure 8.10. The Preview Changes Rename dialog box.





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