Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick 'if-else' flipping in Intellij shortcut

I am trying to refactor existing legacy code and I noticed that I have many places with below pattern:

if (condition) {
    // lot of code
} else {
    throw new SomeException();
}

I was wondering if there is a fast way of flipping if-else construct to easily refactor current form to something like this:

if(!condition) throw new SomeException();
// lot of code

I want to remove unnecessary nesting and making checks at the begining of the function.

like image 497
Tomasz Bawor Avatar asked Mar 12 '18 15:03

Tomasz Bawor


People also ask

What is Ctrl h in IntelliJ?

Ctrl + H = "Type Hierarchy" view = shows a tree of parent and child classes of this class. Ctrl + Shift + A = "It does a search as you type through all the commands in intellij. Not only that but when you find the command you want it also displays the corresponding shortcut key next to it!"

What is Ctrl Shift O in IntelliJ?

In Eclipse, you press CTRL + SHIFT + O “Organize Imports” to import packages automatically. For IntelliJ IDEA, if you press CTRL + ALT + O “Optimize Imports”, it just removes some unused imports, never imports any package.

What is Ctrl Alt L in IntelliJ?

Sometimes code formatting can get out of sync, but there's an easy fix in IntelliJ IDEA. You can use ⌘⌥L (macOS), or Ctrl+Alt+L (Windows/Linux) to reformat a selection of code according to your reformat settings.

What does Ctrl Shift F do in IntelliJ?

In past IntelliJ versions, ctrl+shift+f would search the entire project (no matter whether you had at some point used "find in path").


1 Answers

Put the caret on 'if', press Alt+Enter, select "Invert 'if' condition" from the menu.

like image 176
yole Avatar answered Sep 20 '22 09:09

yole