Wednesday, November 28, 2012

Refactoring › Composing Methods -Extract Method

  • You have a code fragment that can be grouped together.

Turn the fragment into a method whose name explains the purpose of the method.

Example: No Local Variables

before
 // print banner
    System.out.println ("**************************");
    System.out.println ("***** Customer Owes ******");
    System.out.println ("**************************");


after

printBanner();

void printBanner() {
    // print banner
    System.out.println ("**************************");
    System.out.println ("***** Customer Owes ******");
    System.out.println ("**************************");
}

http://sourcemaking.com/refactoring/extract-method



  • The Extract Method refactoring has the following limitations:


    Refactoring does not work with multiple output values in automatic mode. You have to change your code before applying the refactoring.
    Refactoring does not work for a code fragment which conditionally returns from the containing method and is not placed at the end of it.

before (eclipse)

  public class ExtractMethod1 {

public static void main(String[] args) {
method1();
}



 static void method1() {
   int a=1;
   int b=2;
   int c=a+b;
   int d=a+c;
}
}




after (eclipse)

public class ExtractMethod1 {

public static void main(String[] args) {

method1();
}



 static void method1() {
   getSum();
}



private static void getSum() {
int a=1;
int b=2;
int c=a+b;
int d=a+c;
}
}


In Eclipse you select the code fragment you want to refactor,right click,refactor and extract method


before (IntelliJ IDEA 11.1)

  public class ExtractMethod1 {

public static void main(String[] args) {
method1();
}



 static void method1() {
   int a=1;
   int b=2;
   int c=a+b;
   int d=a+c;
}
}


after (IntelliJ IDEA 11.1)

public class ExtractMethod1 {


public static void main(String[] args) {

method1();
}



 static void method1() {
   int a=1;
int b=2;
int c=add(a,b);
int d=add(a,c);
}


private static int add(int a,int b) {
return a+b;
}
}



before (IntelliJ IDEA 11.1)

ArrayList method2()
{
String[] strings={"a","b","c","d"};
ArrayList list=new ArrayList();

for (int i = 0; i < strings.length; i++) {
list.add(strings[i]);
}
return list;
}

}


after (IntelliJ IDEA 11.1)

private ArrayList add(String[] strings)
{
ArrayList list=new ArrayList();

for (int i = 0; i < strings.length; i++) {
list.add(strings[i]);
}
return list;
}


http://www.jetbrains.com/idea/webhelp/extract-method.html#h2example



  • Extract Method


The Extract Method refactoring allows you to select a block of code and convert it to a method. Eclipse automatically infers the method arguments and return types.
This is useful when a method is too big and you want to subdivide blocks of it into different methods. It is also useful if you have a piece of code that is reused across many methods. When you select one of those blocks of code and do a refactoring, Eclipse finds other occurrences of that block of code and replaces it with a call to the new method.

Listing 3. Before Extract Method refactoring

@Override
public Object get(Object key)
{
TimedKey timedKey = new TimedKey(System.currentTimeMillis(), key);
Object object = map.get(timedKey);

if (object != null)
{
/**
* if this was removed after the 'get' call by the worker thread
* put it back in
*/
map.put(timedKey, object);
return object;
}

return null;
}



Listing 4. After Before Extract Method refactoring

@Override
public Object get(Object key)
{
TimedKey timedKey = new TimedKey(System.currentTimeMillis(), key);
Object object = map.get(timedKey);

return putIfNotNull(timedKey, object);
}

private Object putIfNotNull(TimedKey timedKey, Object object)
{
if (object != null)
{
/**
* if this was removed after the 'get' call by the worker thread
* put it back in
*/
map.put(timedKey, object);
return object;
}

return null;
}
http://www.ibm.com/developerworks/opensource/library/os-eclipse-refactoring/index.html

No comments:

Post a Comment