What is Refactoring?
- Refactoring is a disciplined technique for improving the structure of existing code without changing the observable behavior.
As Danny Dig states on his website. In short: change the structure of your code, without changing behavior. Using Refactoring, you can easily move fields, methods or classes around without breaking things. Refactorings come as simple as Rename or more complex like Tease Apart Inheritance.
Why should I Refactor?
- Refactoring improves the design of software
- Refactoring makes software easier to understand
- Refactoring makes software easier to maintain
- Refactoring helps you find bugs
- Refactoring helps you program faster
What are the various techniques in refactoring?
Following are of some of the transformations that can be done in order to refactor your code:-
You can change the name of a package, class, method or field to something more meaningful. Netbeans will update all source code in your project to reference the element by its new name.
How to do it?
Step1 : Right-Click on the variable/method/class name, in the Refactor menu select Rename (Ctrl+R)
Step2 : In “New Name” enter the new name that you desire for the token. Then click on Refactor button complete the operation. Optionally you preview the effect of the refactoring.
Replace block of code with a method
You can generate a statement based on the selected code and replace that block of code with a call to the statement, in just a few clicks.
How to do it?
Step1 : Select the code you want to replace with a method. Choose the "Introduce Method..." in the Refactor menu.
File:Introducing a method
Step2 : Give the name of the method you want and specify its access. Click on 'Ok' and Netbeans will take care of the rest.
Netbeans can automatically generates a getter method and and a setter method for a field and optionally update all referencing code to access the field using the getter and setter methods.
How to do it?
Step1 : Right click anywhere in your code and choose the 'Encapsulate Fields' option in the Refactor menu.
Step2 : You'll be presented with a list of the fields that can be encapsulated. All you have to do is to mention which fields you want to encapsulate. Click on the 'Refactor' button to complete the operation or you can optionally preview the effect the refactoring will have on your code.
Other frequently used refactoring techniques
- Move Class: Moves a class to another package or into another class. In addition, all source code in your project is updated to reference the class in its new location.
- Safely Delete: Checks for references to a code element and then automatically deletes that element if no other code references it.
- Change Method Parameters: Enables you to add parameters to a method and change the access modifier.
- Extract Interface: Creates a new interface from the selected public non-static methods in a class or interface.
- Extract Superclass: Creates a new abstract class, changes the current class to extend the new class, and moves the selected methods and fields to the new class.
- Move Inner to Outer Level: Moves an inner class one level up in hierarchy.
The IDE's refactoring features simplify code restructuring by evaluating the changes that you want to make, showing you the parts of your application that are affected, and making all necessary changes to your code. Using these features will certainly help you save a lot of time and avoid errors in your code while improving the quality of your code.
Where to go from here?
- http://refactoring.com - Martin Fowler's refactoring page.
- http://refactoring.info - Danny Dig's refactoring pase.
- http://www.sourcemaking.com - Easy to understand explanations