Intellij Idea − Code Refactoring


Intellij Idea – Code Refactoring


”;


In this chapter, we will learn about Code Refactoring and how it works in IntelliJ. Code refactoring is restructuring of code without changing its functionality and usability. Code refactoring can be done to improve code readability, performance or to remove unused/duplicate functionality. IntelliJ provides great support for code refactoring. This chapter discusses various code refactoring actions.

Rename

Rename actions can be used to rename methods, its parameters, class attributes, local variables and so on. Let us create the following class in IntelliJ.

public class Employee {
   private String name;
   private String address;
   private int age;
   public Employee() {
      this("Jarvis", "Palo Alto", 35);
   }
   public Employee(String name, String address, int age) {
      this.name = name;
      this.address = address;
      this.age = age;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getAddress() {
      return address;
   }
   public void setAddress(String address) {
      this.address = address;
   }
   public int getAge() {
      return age;
   }
   public void setAge(int age) {
      this.age = age;
   }
   
   @Override
   public String toString() {
      return "Employee{" +
      "name=''" + name + '''''' +
      ", address=''" + address + '''''' +
      ", age=" + age +
      ''}'';
   }
   public static void main(String args[]) {
      Employee e = new Employee();
      System.out.println(e);
   }
}

Now, let us rename Employee class to Person. This action will do modifications in constructors and the main() method −

  • Select Employee word

  • Go to Refactor → Rename and rename it with Person.

Rename Actions

Replace Code Duplicates

This is one of the powerful refactoring actions. IntelliJ identifies code duplicates and
replaces it with appropriate code. Let us introduce code duplication and refactor it. Type
the following code in the Editor −

public class Employee {
   private String name;
   private String address;
   private int age;
   public Employee() {
      this("Jarvis", "Palo Alto", 35);
   }
   public Employee(String name, String address, int age) {
      this.name = name;
      this.address = address;
      this.age = age;
   }
   public void setData(String name, String address,  int age) {
      this.name = name;
      this.address = address;
      this.age = age;
   }
   public void showEmployeeDetail() {
      System.out.println("Name = " + name + ", Address = " + address + ", Age = " + age);
   }
   public static void main(String args[]) {
      Employee e = new Employee();
      e.showEmployeeDetail();
   }
} 

In this example, Employee(String name, String address, int age) constructor and public void setData(String name, String address, int age) method are exactly identical. After refactoring, the Employee(String name, String address, int age) constructor gets modified as follows −

public Employee(String name, String address, int age) {
   setData(name, address, age);
}

To replace the duplicates −

  • Go to Refactor → Find and Replace Code Duplicates.

  • Select refactor scope and follow on-screen steps to complete action.

Replace Code Duplicates

Copy Refactoring

In this section, we will understand how to copy one class to another. Let us copy Employee
class to Person class. We can copy it to the existing module or a new one. IntelliJ will do
the required changes depending on it. Follow these steps to perform copy refactoring −

  • Go to Refactor → Copy, it will open the dialog box.

  • Enter new name and destination package.

  • Click on the OK button and it will do the needful.

Copy Class

Move Refactoring

Move refactoring is similar to copy but instead of making another copy it moves the code to a different package or make it as inner class of another class.

Follow these steps to perform move refactoring −

  • Go to, Refactor → Move.

  • A new window will appear.

  • Select one of the options according to your choice and click on Refactor.

Move Refactoring

Safe Delete

The Safe Delete action will delete object only when it is not referenced anywhere in the
project. The target for this option can be class, interface, method, field or parameter.

Let us see this in action. Type the following code in Editor −

public class HelloWorld {
   static void sayHello() {
      System.out.println("Hello, World !!!");
   }
   public static void main(String[] args) {
      sayHello();
   }
}

Follow these steps to perform the safe delete action −

  • Select the sayHello() method.

  • Right-click on it and select the Refactor → Safe Delete option.

  • As the sayHello() method is being used it will show an error as in the following
    screenshot −

Safe Delete Action

Change Signature

The action modifies method signature. It can change the method name, its parameters,
types, return values and so on. Let us take a method from the above example and change
its signature.

Follow these steps to perform the Change Signature action −

  • Select method.

  • Right-click on it and select the Refactor → Change signature action

  • A new window will appear wherein you can perform the above actions.

  • At the bottom of the window, it shows the preview of new signature.

Change Signature

Type Migration

The Type Migration changes the type of the symbol. This symbol can be a method parameter or class attribute. Let us consider the following method before performing the required action −

static void sayHello(String name) {
   System.out.println(name);
}

Follow these steps to perform type migration −

  • Select the “String” data type.

  • Right-click on it and select Refactor → Type migration.

Type Migration

  • Enter the required data type in the given text box.

  • Choose scope and click on the Refactor button.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *