Quantcast
Channel: JetBrains Developer Community : All Content - IntelliJ IDEA Users
Viewing all articles
Browse latest Browse all 5481

Refactoring method parameters

$
0
0

Imagine following code:

 

     publicvoid method(String currentText) {          Matcher matcher = getMatcher(currentText);          while (matcher.find()) {               doSomething(currentText, matcher.start(), matcher.end());          }     }      privatevoid doSomething(String currentText, int start, int end) {          System.out.println(currentText.substring(start, end));     }      private Matcher getMatcher(String currentText) {          returnnull;     } 

 

I would like to refactor it so that the "matcher" will be passed as parameter into #doSomething

 

But how to do it easily?

 

When I try to use ctrl+alt+n, it makes this abomination:

 

     publicvoid method(String currentText) {          Matcher matcher = getMatcher(currentText);          while (matcher.find()) {               doSomething(currentText);          }     }      privatevoid doSomething(String currentText) {          int end = getMatcher(currentText).end();          int start = getMatcher(currentText).start();          System.out.println(currentText.substring(start, end));     }      private Matcher getMatcher(String currentText) {          returnnull;     }

 

What I want is this:

     publicvoid method(String currentText) {          Matcher matcher = getMatcher(currentText);          while (matcher.find()) {               doSomething(currentText, matcher);          }     }      privatevoid doSomething(String currentText, final Matcher matcher) {          int end = matcher.end();          int start = matcher.start();          System.out.println(currentText.substring(start, end));     }      private Matcher getMatcher(String currentText) {          returnnull;     }

Viewing all articles
Browse latest Browse all 5481

Trending Articles