Tuesday, March 1, 2016

My Favourite IntelliJ IDEA Features

Of course the choice of IDE is very personal; many of us have worked their way through multiple applications, all of which have some great pros and great cons. In this blog post i want to share some of the features that make IntelliJ IDEA my IDE of choice and whose existance makes me happy that people have such incredibly useful and ingenious ideas. Some of these have been around only since IntelliJ 15, others have existed for longer.


Java is obviously the top dog when it comes to feature richness in IntelliJ. Due to java's static type system and simple semantics it is an excellent match for IDE support.

Show Type

Select any complete statement or expression with the mouse or the keyboard and press Ctrl+Shift+P. The IDE tells you which type the selected expression has. This might have been a less useful feature before java 8, but since java got Streams this feature has become a marvellous helper whenever you need to do complex stream transformations. Stream, being a monadic type, allows its contained type to change, collectors transform streams into Maps, Lists or other Collections and sometimes it's easy to get lost in all the transformations.

Extract Variable, Constant, Field, Method, Parameter, Functional Parameter

These are some of my most used shortcuts when refactoring. i just love them! seriously! you just select whatever you would like to be something else and press Ctrl+Alt+V, Ctrl+Alt+C, Ctrl+Alt+F, Ctrl+Alt+M, Ctrl+Alt+P or Ctrl+Shift+Alt+P respectively. If you ever need to pull apart big chunks of spaghetti code, Ctrl+Alt+M and Ctrl+Alt+P are life savers.

The fact that type inference works so splendidly is just the icing on the cake: sometimes when working with streaming operations, you might want to assign a collected Stream into a variable for debugging. So just select the expression until after the

call, hit Ctrl+Alt+V and you get a correctly typed variable (it can be made final directly, too) to inspect when debugging.

Note that there is also an Extract Field refactoring menu item, but i think i have never used it since i try to avoid instance variables as much as possible. if i use them at all, i make them final.

Replace Lambda With Method Reference

This is an inspection that is available via the Alt+Enter popup. It might depend on personal preference but i prefer method references to the lambda syntax in many cases. There are some pitfalls[1] with method references that one should be aware of, but from an aesthetic point of view i just like them better :)

When i refactor code which i find needlessly long or complicated, this is an example of the steps i do:

import java.util.stream.IntStream;
import static io.sourcy.streamslang.StreamCollectors.toGuavaImmutableList;

public class Blah {
    public Blah() {
        // version 1: too complicated to read, inline logic in a lambda
        final List<Integer> a1 = IntStream.range(1, 100)
            .filter(i -> i > 3 && i % 3 == 0 && i - 99 < 4)
        // version 2: use Ctrl+Alt+M to extract method matchesMyWeirdConditions
        final List<Integer> a2 = IntStream.range(1, 100)
            .filter(i -> matchesMyWeirdConditions(i))
        // version 3: use Atl+Enter to replace lambda with method reference
        final List<Integer> a3 = IntStream.range(1, 100)

    private boolean matchesMyWeirdConditions(final Integer i) {
        return i > 3 && i % 3 == 0 && i - 99 < 4;

[1]: for example a method reference on a null object will throw a nullpointer even if that line is never executed due to short circuiting.

Add Selection For Next Occurrence

What a useful feature: select any chunk of text and Alt+J will add the next occurrence of that word to the selection. Do it again and the next occurrence is selected. You can then edit all occurrences at once. This is more useful in languages with weaker type systems, but i have also found myself using it quite a bit.

Breakpoints Inside Lambda Expressions

Another very practical thing. This already existed in IntelliJ 14, but in version 15 it was perfected to the point that i can no longer remember how i used to live without it. Whenever you set a breakpoint on a line containing a lambda expression, the IDE will ask you if you want to break inside the lambda or in the enclosing scope. This is especially useful since Stream construction happens at an earlier time than stream execution.

Extract Method Object

I don't think there's a keyboard shortcut for this by default but of course IntelliJ easily lets you assign one if you want it. Sometimes you might want to encapsulate a block of code into a completely separate class, but a static utility method doesn't seem to fit. In such a case Method Objects can be a good solution. IntelliJ lets you create such objects easily with the rightclick -> refactor menu.

Reformat Code

This is my single most used shortcut after maybe Ctrl+S. The default shortcut is Ctrl+Alt+L which collides with locking my desktop in KDE, so i have remapped it to Alt+Shift+F. The great thing about it is that it can also clean up your imports. I don't use the Rearrange Code feature though.



My most favourite feature when working with scala in IntelliJ is the worksheets. They're like a REPL except they don't suck :D. Ok, ok, the REPL is useful for really short snippets and paste mode exists, but all in all i prefer experimenting in a real multiline editor.

The usual disclaimer: i am not affiliated with JetBrains or IntelliJ IDEA in any way, i also don't care if they invented those features or not, i'm just a big fan of the product :)