Monday, August 13, 2007

The NetBeans Refactor [part 1]

If you have been programming for a while now, you will agree with me that implementing a change in an otherwise huge project can be a nightmare especially if the needed change will affect the project as a whole.

I would give anything to get around such a problem and that’s when I’m most grateful to the Refactor methods provided in the NetBeans IDE.

Refactoring in a more simple term means changing the structure of codes without changing the program as a whole. It makes an otherwise complex code more simpler to understand and readable as well. It reduces unnecessary repetitions and improves the over all performance of the project.

Once a while when working on a project, you'd come to find out that a class you wrote for a specific purpose turns to be needed or used by other classes in respective ways and hence the idea of interfacing comes into play. There is no better way to get around this problem than to call in The Refactor.

Aside it's great features, it is also very easy to use.
Here is a quick look at the Refactor menu



Now we will go through them each but I’ll leave the obvious ones out.

Rename
Allows you to rename a class or a variable and NetBeans does the rest by applying all possible changes

To do this you would have to select the class or the variable you want to rename.
Next you click on the Refactor > Rename button, this dialog will appear



Type in the new name, check the Apply Rename on Comments field and click on Refactor.

Introduce a method
Based on the selected code, it’s able to generate a method for it and place a method call to the procedure.

To do this..

Select the code fragment as I have done here



Click on Refactor > Introduce Method or Refactor > Extract Method as in older versions
A dialog appears requiring few information from you



Here you specify a unique name for the method in the Name field. Check the appropriate access field and click on Ok.

You should see something like this in somewhere in your codes

A new Method has been created for you... it sure does take some loads off.

My Next favorite is:

Extract Interface
this creates an interface class from a list of selected public non-static methods

Suppose I created a database connection class named SqlCon to be used by a specific class and I later realized there other classes in my project that will utilize the SqlCon methods differently then it’s obvious that I have to create an interface

NetBeans makes that easier,

To Do this, Open the class you wish to extract its methods to an interface.
Now click on Refactor > Extract Interface button



Type in the Interface name, select the methods you wish to put inside the interface and click on the Refactor button.

And there you have your interface class



You also notice that the declaration of the class SqlCon now looks something like this

public class SqlCon implements NewInterface {

}

So easy…….

Let’s end here for now, I’ll continue exploring it with you on my next post…..Don’t miss out on this.

2 comments:

Aziz Abdul said...

Thanks a lot James, I'll get back to you any time soon.

Ivan said...

Great post!

I always wondered, what "introduce method" does. Now I see. It's very handy!

Thank you!

 

© New Blogger Templates | Webtalks