Introduction

Refactoring is a technique to rebuild an already existing body of code. It is a controlled technique for improving the design of existing code. Refactoring changes, rather we can say transforms, the internal structure without changing its external behaviour. Every refactoring makes a small alteration, but a sequence of alterations produces a noteworthy refactoring. Refactoring is difficult to go wrong since each refactoring is very small. The systems are kept in a fully working condition after each refactoring process, so as to ensure that systems are not affected seriously during a refactoring process.

Need to Refactor

  1. Software becomes understandable after refactoring.
  2. Bugs are easy to detect using refactoring.
  3. Programming becomes faster.
  4. Refactoring increases software maintainability.
  5. The design of the software is improved.

Technique of Refactoring

The following are some of the ways to refactor your code:
  1. Rename: We can change the name of packages, classes and variables to some meaningful names. The NetBeans IDE updates the names of all the elements in your source code.
To rename a variable, method or class name, consider the following steps:
16.1.jpg
16.2.jpg
16.2.2.jpg
  1. Change the existing block of code with a method: We can just create a statement on the basis of some selected text and replace that selected text with a specific method. Use the following procedure to do that.
16.3.jpg
16.4.jpg
  1. Encapsulating the Fields: NetBeans has the ability to automatically generate the getter and setter method for any field. The following procedure describes how to encapsulate the fields.
16.5.jpg
Other Techniques of Refactoring
There are many other techniques to refactor your code, they are the following:
  1. Change Method Parameters
  2. Extract super class
  3. Safely delete