The Extract Method refactoring lets you take a code fragment that can be grouped, move it into a separated method, and replace the old code with a call to the method.
note
This refactoring is also available as an intention action in the editor. Select code that you want to extract, press and select Extract Method.
When you extract the method you need to check for variables. If there is one output variable, it is used as a return value for the extracted method. In case there are multiple output variables, the Extract Method refactoring may not be applied, and the error message appears.
There are several workarounds to allow Extract Method work in this case. For example, you may introduce a special data-class that contains all output values.
note
The Extract Method refactoring has the following limitations:
Refactoring does not work when there are references to generic types.
Refactoring does not work with multiple output values in automatic mode. You have to change your code before applying the refactoring.
Extract method
Select a code fragment you want to extract to a method.
Press or go to Refactor | Extract | Method in the main menu.
If Aqua detects code that is only partially duplicated, it suggests extracting a parameter to proceed with the refactoring.
tip
To reverse the Extract Method refactoring, press to invoke the Inline refactoring.
Example
Let's extract the a+b expression into a method (function for Kotlin), and replace duplicates .