ReSharper Help  

Refactoring Code

Overview

According to Martin Fowler in Refactoring: Improving the Design of Existing Code (Addison-Wesley, 2000), refactoring is "the process of changing a software system in such a way that it does not alter the external behavior of the code, yet improves its internal structure. It's a disciplined way to clean up code that minimizes the chances of including bugs." For more details, please refer to http://www.refactoring.com/.

ReSharper offers an ample range of C# code refactorings that track down and correct the code references automatically.

Note   Visual Studio 2005 provides its own Refactor menu that is available in both main and shortcut menus. You can remove the Visual Studio's Refactor submenu from the shortcut menu. Click the ReSharper | Options menu, and then in the General settings, select to replace VS menu items by respective ReSharper ones.

Performing Refactoring

Although each refactoring has its own settings and options, the basic procedure is practically the same.

To perform a refactoring:

  1. Select an object or a code fragment you want to refactor.
  2. Do one of the following:
    • On the ReSharper | Refactor menu, select the required refactoring.
    • Press the corresponding keyboard shortcut.
    • Click the ReSharper | Refactor | Refactor this menu or press Ctrl + Shift + R to open the list of refactorings that can be applied to the selected object or code fragment, and then select the necessary refactoring.
    • Right-click the selected object or code fragment, then on the shortcut menu select R# Refactor, and then select a refactoring or the Refactor this command.
  3. The refactoring wizard opens. Except for the first step, wizard's dialog boxes are not modal, so you can edit the code while the wizard is open.
  4. If the refactoring operation can affect several files, the wizard will have the Keep modified files open check box. If you select this check box, ReSharper will open the modified files in new editor tabs, and then you will need to save the changes yourself. Clear this check box if you want ReSharper to save the changes in files automatically, without opening the files.
  5. If the use of the refactoring might cause code conflicts (such as duplicate names, visibility conflicts, etc.), the wizard will display the list of conflicts on the last step. For some conflicts the wizard can also suggests Quick-Fixes.

Available Code Refactorings