Saturday, November 20, 2010

The term refactoring simply means changing the structure of your source code. For example, you may decide to change the name of a class, a method, or a field inside one of your source files. Or perhaps you need to move a class from one package to another.

Refactoring is generally about making source better, including

* Improving the code's maintainability
* Making your source code more portable and reusable
* Implementing a design change due to a new software feature


NetBeans comes with a solid amount of refactoring support built in.NetBeans presently supports four primary refactoring approaches:

* Renaming fields, methods, classes, or packages
* Encapsulating fields
* Changing method parameters
* Moving classes

You can select these options from the Refactoring menu at the top of the NetBeans IDE, from context-sensitive menus inside the editor.

For more details you can view a listing at http://refactoring.netbeans.org/. Here are some refactoring features coming in future versions of NetBeans.

* Extract interface/superclass
* Pull up, push down method
* Inline method
* Extract method
* Move method
* Convert nested to top-level class
* Extract/inline local variable

Some Sample Source Code

Main.java:

package sample;

import sample.Adder;

public class Main {

public Main() {
Adder a = new Adder(4,5);
System.out.println("4 + 5 = " + a.firstPlusSecond);
}

public static void main(String[] args) {
Main m = new Main();
}

}



And here is the source code for Adder.java, a class that adds two integers.

package sample;

public class Adder {

public int firstPlusSecond;

private int first;
private int second;

public Adder(int arg1, int arg2) {
first = arg1;
second = arg2;

firstPlusSecond = first + second;
}

public String toString() {
return Integer.toString(firstPlusSecond);
}

}





Simple Refactoring: Renaming a Package

With NetBeans you can use the built-in refactoring tools to change the name of a field, a method, a class, or even a package. This change will then propagate throughout the source tree to all the classes that reference that particular item, instead of forcing you to hunt down references by yourself or through compilation errors. Each of these tools is listed under the Refactor menu at the top of the NetBeans IDE.

For example, let's say that we don't like our source classes to be in the sample package. Instead, we want them to reside in a package called functions. First, we change the name of the folder in the Projects window in the upper-left corner. This will immediately bring up the dialog box .

At this point, the preview of the refactoring tasks is displayed in the bottom output window of the IDE.You can review the refactoring information presented in the output window and select and clear various changes that the IDE proposes to make. If the changes look good, press the Do Refactoring button to save the modifications.

Thus,we have we have shifted our source code to be in another package.