Writing code

IntelliJ IDEA can help you to write code quickly and efficiently while minimising compilation errors.

Writing code is an integral part of our jobs and it's worth knowing what IntelliJ IDEA can do here to help you write great code. We will look at:

  • Completing Code
  • Generating Code
  • Running Anything
  • Managing Dependencies

Completing Code

There are lots of different types of code completion in IntelliJ IDEA including basic, smart or type-based, statement, postfix and more.

Basic completion should be available out-of-the-box, but if not you can use ⌃␣ (macOS) / Ctrl+Space (Windows/Linux) to invoke it. For smart or type-based matching completion use ⌃⇧␣ (macOS) / Ctrl+Shift+Space (Windows/Linux). It's worth getting use to using type-based matching completion in your code base as IntelliJ IDEA will offer you the type applicable to your current context.

Statement completion ⌃⇧⏎ (macOS) / Ctrl+Shift+Enter (Windows/Linux) is a great way to keep your code green and compiling. When you use this shortcut, IntelliJ IDEA will do the minimum needed to make your code compile.

Postfix completion allows you to keep typing forward. It's a more linguistically natural way of thinking and arguably more efficient to type forward all the time.

Generating Code

IntelliJ IDEA is excellent at generating code constructs for you. It's always worth using ⌘N (macOS) / Alt+Insert (Windows/Linux) to see what the IDE can do for you in this respect. When it comes to Java code, here are just some of the common code constructs that IntelliJ IDEA can generate for you:

  • getters and setters
  • constructors
  • equals and hashCode methods
  • override methods

... and more. You can also use the same shortcut to generate new files in your Project tool window.

Running Anything

Similarly to Search Everywhere, Run Anything ⌃⌃ (macOS) / Ctrl+Ctrl (Windows/Linux) allows you to run any of your Run Configurations as well as scripts and commands such as mvn clean or gradle --status. You can also use Run Anything to open your recent projects such as open intellij-samples.

Managing Dependencies

Managing dependencies is something that we're all likely to need to do sooner or later and IntelliJ IDEA supports multiple ways of achieving it. Many of you will be used to using IntelliJ IDEA's completion capabilities in the pom.xml or build.gradle file.

IntelliJ IDEA has also introduced a Dependencies window which you can access with Search Everywhere ⇧⇧ (macOS) / Shift+Shift (Windows/Linux) and then by typing in "dependencies" and pressing Enter. In this window you can add, remove and update you dependencies. You can also take a look at Package Search if you don't have the IDE open.