Refactor your code with Vs.net 2005.

Source: Internet
Author: User
Tags include variables variable
   I. Introduction

As programmers, we are all writing all kinds of code, while some programmers write code that is more excellent. The difference is obvious. Writing good code is not a skill that all developers can master. However, it also means that this technique can be improved. One of these skills involves refactoring code written by you or someone else. Vs.net 2005 includes a set of features that will help you master this approach. This article just wants to discuss what refactoring is and how Vs.net 2005 helps you refactor your code.

   two. What is refactoring?

As a developer, we all write code in one or more languages. However, the code we write is often not the best code in terms of organization, scheduling, readability, and structure. As we review our code again, we often realize that there is a need to make further improvements and even rewrite them in some places. This process of improving our code is called "Refactoring." Note that the term "refactoring" means that we are improving the code rather than changing its functionality or meaning.

Some common refactoring tasks include:

· Change the signature of a method

· Renaming variables

· Create an interface based on existing code

· Turn a piece of code into a separate method

   three. Refactoring (refactor) menu

Now let's discuss the most interesting content. Vs.net 2005 includes a menu "refactor" that you can use to implement some common refactoring tasks. The following figure shows the Refactor menu and its menu items.


   four. Renaming variables, properties, and methods

In many cases, you'll find that you should change the names of certain variables, properties, and methods when you've written a lot of code. The most common reason is to improve its readability. The traditional approach is to use the Find and replace option in the Edit menu. Unfortunately, our code is not a working document, so invoking find and replace may not be very effective. Moreover, the likelihood of accidental substitution of unwanted symbols is great. The "Rename" menu option under the Refactor menu can help you accomplish this task easily.

To do this, you need to go to the property or method name you want to change, and then select the "Rename" menu option. This opens a dialog box that appears as follows:


After that, you can enter a new name for the variable, property, or method, and then click OK to change it in every place. Note that you can preview the changes before you actually apply them. The following illustration shows the "Preview" dialog box. You can observe the changes that you make when you do not select certain locations.



In addition, you can control whether you want to change the name in the annotation and the string symbol.

  Five. Extract a method

Let's consider a typical situation. You start writing a very long way-it contains a collection of very complex algorithms. After completing this method, you may realize that it becomes too big and too complex for other members of the team to understand it easily. So, you decide to split it into smaller functions. This will not only simplify your code but also improve its readability and maintainability. The "Extract method" option under the "Refactor" menu is right for this job.

To use this menu option, you must select a block of code that you want to detach as a method, and then click the "Extract Methods" menu option. The following illustration shows the Extract Method dialog box.


Once you have entered the name of the new method you want to create and click OK, it does two things for you:

· It creates a new method based on every choice you make and replaces all of the selected code

· It replaces the selected row with a call to this newly created method

   Six. Create the properties of the wrapper class-level variable

Let's accept the fact that many developers are accustomed to exposing class-level variables (fields) to the outside world. Since each object belongs to object-oriented programming, developers should allow access to variables through attributes or methods. This situation can be handled using the "Encapsulate Field" option under the Refactor menu.

To do this, select the class-level variable you want to wrap in an attribute and select the Encapsulate Field option. This opens a dialog box as shown in the following illustration:


You need to enter the name of the attribute and decide whether you want to update the reference to the variable from outside the class or within the class. Just like the Rename dialog box, you can preview the changes before you apply them.

  Seven. Extraction interface

Sometimes, after you create a class, you find that there are other classes that have similar constructs but have only a different implementation. This is a typical case of using an interface, and the "Extract Interface" option under the Refactor menu can help you accomplish this task.

To do this, you need to be in this class and select "Extract Interface" to pop up a dialog box as shown in the following illustration:


You need to specify the interface name, the file name of the storage interface, and the members to include in the interface. Click OK to create a new file and create a method in it. Also, the class from which you extracted the interface will be automatically labeled to implement this interface.

   Eight. Improve the local variable to the method parameter

Another common place is to convert a local variable in a method to a method parameter. In this case, the "Promote local variable to parameter" option under the Refactor menu helps to achieve this goal. It just needs to put your customizations on the line of code you've declared variables and click this option. You will find that the variable now looks like a method parameter, and all calls to the method are updated accordingly. Note that to make this option work, it is necessary to initialize the variable to a value.

   Nine. Delete method parameters

You may also need to delete some or all of the method parameters. In this case, the Remove Parameters option under the Refactor menu is the complete task.

To do this, you need to be inside the method whose parameters need to be removed, and select the "Remove Parameters" option. This will produce a dialog box as shown in the following illustration:


You can then delete the required parameters. All method calls, along with interface definitions, will also be updated-based on the new signature. As always, you can preview the changes before you apply them.

   10. Rearrange the order of method parameters

After you add method parameters, it is easy to delete them. But is it possible to sort them again? Don't worry. There is a "Reorder Parameters" option to achieve this goal. This option can only work on methods that have two or more parameters. When you select it, a dialog box opens as shown in the following illustration:


You can move the parameters up or down, and look at the preview results. As with the delete parameter option, all method calls are automatically updated with each new parameter sequence.

   11. Summary

Refactoring is very important and is the most common requirement in many development sites. To do this, Vs.net 2005 provides the tools necessary to quickly refactor your code and improve its entire organization, structure, and readability.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.