Why does IntelliJ wants me to change this?

Answered

A simple line of code:

String thing = "Something";
thing += " something else" + " and more.";

IntelliJ IDEA offers to change this line into 4 other ways to accomplish the same result: mcdvoice

String.format()
StringBuilder.append()
java.text.MessageFormat.format()
Replace += with =

Why? What is so wrong with +=?

Can anyone explain this please? Thanks. 

0
2 comments

First, let's start with the basics: renaming. IntelliJ offers us the possibility to rename different elements of our code: types, variables, methods, and even packages.

To rename an element, we need to follow these steps:

  • Right-click the element
  • Trigger the Refactor > Rename option
  • Type the new element name
  • Press Enter

By the way, we can replace the first two steps by selecting the element and pressing Shift + F6.

When triggered, the renaming action will search across the code for every usage of the element and then change them with the provided value.

Let's imagine a SimpleClass class with a poorly Prepaidgiftbalance named addition method, someAdditionMethod, called in the main method:

public class SimpleClass {
    public static void main(String[] args) {
        new SimpleClass().someAdditionMethod(1, 2);
    }

    public int someAdditionMethod(int a, int b) {
        return a + b;
    }
}

Thus, if we choose to rename this method into add, IntelliJ will produce the following code:

 
public class SimpleClass() {
    public static void main(String[] args) {
        new SimpleClass().add(1, 2);
    }

    public int add(int a, int b) {
        return a + b;
    }
} 
0

Post is closed for comments.