Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hotkey in IntelliJ IDEA to extract all return statements in method

For example, if we have method:

private A getA(A a1, A a2) {
    if (a1 != null) {
        return a1;
    } else {
        return a2;
    }
}

Does IDEA have some hotkey (like Ctrl+Alt+V for extracting variable) to make from this code something like this:

private A selectA(A a1, A a2) {
    A result;
    if (a1 != null) {
        result = a1;
    } else {
        result = a2;
    }
    return result;
}
like image 687
SoulCub Avatar asked Jan 30 '26 20:01

SoulCub


1 Answers

Put the caret at the method name, press Alt+Enter, then choose "Transform body to single exit-point form". It will convert the method to a single return form.

The setting can be found in Settings | Editor | Intentions, then Java | Control flow | Transform body to single exit-point form. It should be enabled by default.

like image 150
Pang Avatar answered Feb 01 '26 10:02

Pang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!