5

I was designated to make refactoring on a project, and I came across this situation

 this.path = DESTINY + deploy.name() + FILE_SEPARATOR + delivery.getSystem().getCode()
      + FILE_SEPARATOR + delivery.getName() + FILE_SEPARATOR + delivery.getEnviroment().getName();

I want to refactor it to something like this:

StringBuilder tmpPath = new StringBuilder();
tmpPath.append(DESTINY);
tmpPath.append(deploy.name());
tmpPath.append(FILE_SEPARATOR);
tmpPath.append(delivery.getSystem().getCode());
tmpPath.append(FILE_SEPARATOR);
tmpPath.append(delivery.getName());
tmpPath.append(FILE_SEPARATOR);
tmpPath.append(delivery.getEnviroment().getName());
this.path = tmpPath.toString();

Or

StringBuilder path = new StringBuilder();

...

this.path.append(DESTINY);
this.path.append(deploy.name());
this.path.append(FILE_SEPARATOR);
this.path.append(delivery.getSystem().getCode());
this.path.append(FILE_SEPARATOR);
this.path.append(delivery.getName());
this.path.append(FILE_SEPARATOR);
this.path.append(delivery.getEnviroment().getName());

Whats is the fastest way to do that ?

1 Answer 1

9

Just hit Alt + Enter when having cursor on your original code:

enter image description here

Then you can choose the variant you prefer the most such as replacing it with String Builder. IDEA will do all the work for you.

enter image description here

2
  • I suspect the yellow highlight on StringBuilder after the refactor is IntelliJ recommending to replace the StringBuilder with String concatenation?
    – njzk2
    Oct 14, 2019 at 8:06
  • Yes, it may not be best to use stringbuilder unless in a cycle. Oct 14, 2019 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.