[RSS]

Refactoring Simplified



What is Refactoring?

  • Refactoring is the use of small transformations to restructure code without changing any program behaviour.

  • Refactoring helps in reducing program complexity, removing unnecessary repetition and making your code reusable.

  • It can also help in improving the performance of your code.

What are the various techniques in refactoring?

Following are of some of the transformations that can be done in order to refactor your code:-

Rename

You can change the name of a class, variable, or method 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 "Intoduce Method" in the Refactoring menu.
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.


Encapsulate Fields

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 Refactoring 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

  • Change Method Parameters : Enables you to add parameters to a method and change the access modifier.
  • 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.
  • Move Inner to Outer Level: Moves an inner class one level up in hierarchy.
  • 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.

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 and performance of your code.

Attachments

encapsulatefields.jpg Info on encapsulatefields.jpg 61956 bytes
intoduce_method.jpg Info on intoduce_method.jpg 47892 bytes
intoduce_method1.jpg Info on intoduce_method1.jpg 20716 bytes
intoduce_method2.jpg Info on intoduce_method2.jpg 13714 bytes
rename.jpg Info on rename.jpg 26156 bytes
rename1.jpg Info on rename1.jpg 16542 bytes