Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Code Refactoring in VS.NET 2005 : Page 2

Find out how to use VS.NET 2005's Refactoring engine to improve the structure and readability of your C# source code while preserving its functionality.


advertisement
Extract Method
Sometimes you realize that a method's code has become lengthy, making it a nightmare to read and maintain. The answer is usually to decompose the lengthy method into shorter sub-methods—which is where the "Extract Method" option fits in. You can use this option to extract a sub-method from another method, converting a selected section of code from within a long method into a new and shorter private method. The Extract Method replaces the selected code in the original method with a call to the newly created private method containing the selected code from the original method.

 
Figure 4. Extract Method Dialog: This dialog gives you the option to assign a specific name when extracting a sub-method.
When you invoke the "Extract Method" option from the Refactor menu, you'll see the Extract Method dialog shown in Figure 4. To complete the operation, enter a name for the new method and click on the OK button.

For example, assume that you had a lengthy method containing the following code.

public int Add(int i,int j) { ... int k = 0; k = i + j; return k; }

You want to extract the three lines between the ellipses into a separate method. Select the three lines, and select the Extract Method option from the Refactor menu. In this example, I simply accepted the default name ExtractedMethod, but normally you'd provide a more explicit name. Note that VS updates the method definition in the original method to reflect the call to the newly extracted private method.

public int Add(int i,int j) { ... return ExtractedMethod(i, j); } private static int ExtractedMethod(int i, int j) { int k = 0; k = i + j; return k; }

Encapsulate Field
Encapsulation is an object-oriented programming technique that isolates class members (fields) from the external world by encapsulating them within a common unit. The Refactor menu helps in this by offering the "Encapsulate Field" option, which encapsulates access to a class field by creating property accessors. The option simply generates a property wrapper around a specified class member.

You can set the property visibility, specify the property name, and even choose whether to update references to the class member from outside the class, inside the class, or both. As usual, you can preview your changes.

Extract Interface
 
Figure 5. Extract Interface Dialog: The dialog lets you provide an interface name and choose the members from which to extract the interface.
Applications often have two or more classes that have identical structure but differing implementations. In such cases, it's often desirable to provide a common interface. You can use the Refactor menu's "Extract Interface" option to create a new file that contains the specified interface. The option also alters the class declaration to indicate that the class implements the specified interface. When you choose this option from the Refactor menu, the window in Figure 5 pops up.

Specify the name of the interface, preview the changes, and click OK to allow the changes to take effect. As an example, consider the class shown below:



public partial class MainForm : Form { //Some code public int Add(int i,int j) { int k = 0; k = i + j; return k; } }

Extracting an interface for this class creates interface and updates the class definition accordingly; in other words, the process implements the extracted interface shown below:

using System; namespace Test { interface IMainForm { int Add(int i, int j); } }



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date