Refactoring

You will seldom work with the ideal code. Almost every code can be improved—variables or methods can have better names, and code can be better structured and less complicated. This means that you would like to refactor such code. Refactoring code is a very hard and complicated process but ReSharper makes it a bit less painful.

Let's go back to our code that was used at the beginning of this chapter and refactor it a bit.

Rename

Let's say that we would like to rename the Repository property to UserRepository. Move your cursor to the definition or usage of this property and press Ctrl + R, R, input a new name, and press Enter. ReSharper will rename all occurrences of this property.

ReSharper also checks your string values, comments, JavaScript code, and other string literals as there can be some values related to our code that should also be updated. As you can see in the following screenshot, ReSharper displays all the places that can be potentially updated and allows you to check what all should be updated:

Rename

Note

When renaming a class name, ReSharper will also change the name of the file that this class contains.

Moving to a separate file

Now it is time to clean up our project's structure. We have three classes: Program, UserManager, and UserRepository, and all are in the same file. This is not a good practice, so let's change this.

Move your cursor to the class named UserManager and press Ctrl + R, O. From the displayed menu, select Move To Folder. This will open a new window in which you can change some settings related to moving your class. Let's put in a new folder named Code. In the Target folder field, write ConsoleApplication1Code and click on Create this folder. Next, select Put classes into separate files and Fix namespaces. Finally, select the classes that you would like to move, UserManager and UserRepository in our case, and press Enter.

Quick, easy, and very useful.

There are two more options available after using the Ctrl + R, O shortcut:

  • Move To Another File: This moves your class to a separate file in the same folder that the current file is located in
  • Move Type To Another Namespace: This moves your class to a new namespace but it is still in the same file

Refactor this…

Rename and move to separate file are the two most used refactorings. More options are available through the Ctrl + Shift + R shortcut.

Let's move your cursor again to the name of the UserManager class, press Ctrl + Shift + R, and select Extract Superclass. This will allow you to create a base class for your UserManager class. In the new window, write the name of base class that you would like to create, select the members that should be moved to the new class, and press Enter.

If, instead of the base class, you would prefer to create the interface, just select the Extract Interface option.

Another very useful option is Extract Method. It is available after selecting the code that you would like to move to a new method. A sample window to extract the method is shown in the following screenshot:

Refactor this…

ReSharper comes with many other refactoring options. Every time you would like to refactor some code, press Ctrl + Shift + R and check if there is an option that can help you. You can find options such as Convert Abstract Class to Interface, Convert Property to Method, Extract Class, Introduce Variable, and many more.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.7.131