Wednesday, September 24, 2008

Code Refactor Using Orcas

Refactor:


















Rename: Rename the fields variable or parameter of the class/method with appropriate change in the referenced places.


e.g.

private string mPageName;

  • You need to rename this variable, just right click on the variable name and in Refactor menu select Rename.
  • Oracas will ask you for new name, with below options:
    • Preview reference chages
    • Search in comments
    • Search in strings
  • After giving new name click on OK, it will show you the changes in the references of the variable selected for review.
  • If you click on APPLY the new name will get assign to all places.



Extract Method: You have a method/event with 100 lines or more (Just for e.g.), if Architect reviews it, he definietly going to say that it is not readable. In such case you can refactor it in two or three different methods easily, without scraching your head.

e.g.

  • See I have a method and I want to extract some part into a separate method.
  • Just select the number of lines, those you want to extract.
  • Right click, select Refactor menu à select Extract Method… item.

















  • It will show a dilog box to enter new method name:








  • After providing new method name Orcas will create new method as shown below.

private void NewMethodName(ArrayList deleteIds, Dto.AdverseActions adverseActions)
{
Dto.AdverseActions iDto = GenericAudit.AuditPage(new Dto.AdverseActions(), ConstantKey.AuditTrailPropertyName) as Dto.AdverseActions;
Session[ConstantKey.AuditTrailData] = iDto.AuditTrailLog; // Temporary - To show the report immediately in a seperate Page.
if (deleteIds.Count > 0)
{
if (Service.AdverseActions.DeleteAdverseActionsSearchData(deleteIds, adverseActions) > 0)
{
lblNoError.Visible = true;
lblNoError.Text = GetLocalResourceObject("DeleteMessage").ToString();
BindGrid();
}
}
else
{
lblError.Visible = true;
lblError.Text = GetLocalResourceObject("SelectCheckBox").ToString();
}
}



Encapsulate Fields: Similar to the above Orcas also provides feature to encapsulate fields. Suppose you declare a class level field/variable you can create property for those fields without wirting a single line of code.

e.g.

  • You have declare a field like below:

private string mName;

  • Right click the field and select Encapsulte Field from Refactor menu.















  • Change the property name if needed, else just click on OK.
  • It will show the PREVIEW of the property for the selected field. If you select APPLY it will create the property as shown below:

private string mName;
public string MName
{
get { return mName; }
set { mName = value; }
}




Extract Interface: This is a very unique and good feature provided by the Oracs to follow OPPS better, where can easily manage the fields those are going to repeate in multiple classes. Say you have a class as shown below:



public class BasePage : System.Web.UI.Page
{
#region Private Variables
private string mUserName;
private string mPageName;
private int mLenderId;
#endregion
#region Properties
///
/// Get or Set user name
///
/// string
///
///
public string UserName
{
get { return mUserName; }
set { mUserName = value; }
}
///
/// Get or Set page name
///
/// string
///
///
public string PageName
{
get { return mPageName; }
set { mPageName = value; }
}
///
/// Get or Set Lender Id
///
/// int
///
///
public int LenderId
{
get
{
return mLenderId;
}
set
{
mLenderId = value;
}
}
#endregion



Later you relize that you have to implement the LenderId in the multiple classes always. Here you can extract an Interface from the existing implementation.

  • Just right click within the class and select EXTRACT INTERFACE from the REFACTOR menu.
  • It will ask you for the Interface name, Oracs is so smart that it will first create the interface name as per the naming conventions of the Microsoft.
  • e.g. If class name is BasePage it will suggest you IBasePage as Interface name. see












  • If name is finalize then select the Property for which you want to create Interface from the list of public members shown.
  • Select at least one member to create Interface.
  • Click on OK .
  • Orcase will give you new interface, with new Interface class file, as below code shown:

using System;
interface
IBasePage
{
int LenderId { get; set; }
}



That’s it, you are ready to use this Interface.


Promote local variable to Parameter: This is something interesting feature by Orcas. Its not that you can not do this manually, but still Orcas has provided the feature to save the time.


  • You have a method having multiple vriables define.
  • Later you realize that some of them needs to be provided to the method by the calling method or event.
  • Select the variable, right cilck on it, select PROMOTE LOCAL VARIABLE TO PARAMENTER.
  • First it will do some study of the variable, if the variable is define some initial value in the method it will ask whether to continue or not.
  • If you continue Orcas will remove the local variable and add it in the method declaration.



Remove parameter: This is as simple & easy as the name suggest. Just select the parameter and right click select REMOVE PARAMETER and Orcas will do it for you.

Reorder parameter: Similar to the above feature just click on REORDER PARAMETER and you will get an interface to reorder the parameters.


No comments:

Hello

Thanks for checking out my post...

... Paresh Bhole