|
|
NET Refactor - Extract MethodAutomates Extraction of Methods to Modularize Code MethodsExtract Method automates the process of refactoring large, unmanageable methods into a series of small, logical and functional methods.Extract Method is one of the most useful features of NET Refactor. This feature allows you to select a block of code from a large method and create a new method. This refactoring feature allows you to simplify large, unmanagable methods into a completely modularized series of methods.
Extract Method makes the extraction of a code block, creation of a new method, and insertion of a call to the new method, in place of the extracted code block completely automated.
To use this feature, select a block of code that you want to move to a new method and then select the Extract Method option from the NET Refactor menu. A dialog will be displayed, as shown in Figure 1. Before the dialog is displayed, NET Refactor will analyze the code being refactored and pass the objects required by the refactored code. The dialog contains the selected block of code and a calling line of code, displayed in the Calling Line textbox. The NewProcName text box is highlighted so that you can enter the name for your new method. You must change the default name to a name of your choice before clicking OK or Net Refactor will prompt you to enter a new name.
How variables are passed
An understanding of Value and Reference Types is invaluable in .NET programmng. Under certain conditions, all variables could be passed by value. In order to pass all variables by value, your new method must meet several conditions. First, if a variable is of value type, such as Integrals, Structures, and Enums, and String, which are a hybrid type, you cannot modify its contents in the called method and expect to reflect the modification in the calling method when control is returned to it. Secondly, you can modify the members of a reference type, even if it is passed by value, and the changes will be reflected in the calling method. However, you cannot set the object to a New instance and expect the new instance to be reflected in the calling method.
NET Refactor cannot determine the type of a variable other than primitive types that are either passed into the method you are refactoring or they are declared locally in the method being refactored. Again, these variables would be types such as String, Integer, Double, Single, etc.
Consequently, to ensure that it does break your code, NET Refactor defaults to passing all variables by reference. You can override this behavior in two ways. First, if you program in such a way, a good way by the way, you can check the Pass Reference Types By Value checkbox. The first time you check this box, a message box will display informing you of the ramifications of passing by value previously discussed. Additionally, the dialog will respond and change the passing methodology automatically. Unchecking the box wil revert to passing by reference. The value of the checkbox will be saved in a configuration file each time that you change the state of the checkbox and it will be remembered as a preference. You can also have the dialog pass all variables by value by checking that checkbox. It will override the other checkbox, but only for the current refactoring instance. Please keep in mind that if all variables are passed by value, changes may not be reflected in the calling method, depending on the type variable being passed, value or reference.
Once you have determined that the new method contains the desired code and that all parameters are being passed correctly, click the OK button. The selected code block will be replaced by a call to the new method, which will be placed immediatly underneath the current method.
Figure 1 - Extract Sub Dialog.

Figure 1 shows that Extract Method can create a Sub (VB.NET) or a void function (C#). Figure 2 shows that you can create a Function (VB.NET) or a function that returns a type(C#). When the Extract Method Dialog is displayed, you can switch between a Sub/void method and a Function/return type by clicking the respective option button. The Dialog does not analyze the extracted code to attempt to determine the desired return value. It simply populates the Return Value combo box with the passed parameters and variables local to the extracted code. If multiple variables are found in the combo box, you can choose the return value in the Return Value combo box. Selecting a different return value, after the function code has been generated in the code window of the Extract Method Dialog, will cause the newly selected value to be returned.
Figure 1 - Extract Function Dialog.

Try NET Refactor Free for 30 days or purchase now by clicking Download or Purchase.
Top of Page
|
|