Refactoring

Refactoring is one of the biggest advancements in modern IDEs. It significantly cuts down on time and reduces the margin of error by the way in which it handles changes in the code and automated operations. Visual Studio comes with great out-of-the-box refactoring functionalities such as renaming and the creating method from a selected piece of code.

The renaming functionality can really help with potential errors in code, such as when changing the name of an element. There might be instances in the codebase where the old name is still used. Let's have a look at the following code:

class foo:
    """
    Documentation of the class.
    It can be multiline and contain any amount of text
    """
    @classmethod
    def bar(self, first=0, second=0):
        """This is the documentation for the method"""
        return first + second

print(foo.bar())

In this code, there's a class, foo, that has a method called bar. If bar is renamed, it will create an error by referencing to a nonexistent method.

Visual Studio's refactoring functionality helps the renaming process by taking all the references of the element into account. Select the element that you wish to demand and then access the refactoring function in the code contextual menu by right-clicking on it:

Refactoring

Access refactoring function by right clicking on the element

Select the Rename function to start the two-step process:

Refactoring

You are prompted to enter a new name for the element. There's also a checkbox that permits you to preview the references of the element to be renamed. When the checkbox is unchecked, all the references that are found will be renamed; you will not be able to preview which references are going to be renamed.

Refactoring

In a situation where you wish to preview the reference change, a preview window will be shown. As shown in the preceding screenshot, in this window all references of the old element name can be found at the top panel and the code preview will be at the bottom. All the files of the project will be analyzed to refactor the element correctly. A checkbox near each reference provides the option to activate the refactoring of that reference. Clicking on the Apply button will rename the element and all the selected references.

The other refactoring function is Extract Method. This comes in handy when you wish to reuse a piece of code somewhere else as a function or a method. Visual Studio can generate it as a function/method. As an example, refer to the following screenshot:

Refactoring

In the code, the highlighted code aims to create a generic method that calculates the sum of two elements. Select the code and then select Extract Method from the Refactor submenu in the contextual menu. This brings up the Extract Method dialog box, which is shown in the following screenshot:

Refactoring

In the Extract Method window, you can define the name of the method in the New method name field and provide a path for the method to be created in the Extract to field. A Preview panel also shows the generated code. Click on OK to create the new method based on the selected code.

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

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