Explain Codes LogoExplain Codes Logo

Intellij Organize Imports

java
intellij-ide
import-organization
coding-habits
Alex KataevbyAlex Kataev·Oct 11, 2024
TLDR

Devotedly use Ctrl + Alt + O (Cmd + Opt + O for macOS) in IntelliJ IDEA to optimize imports, which gets rid of unused imports and arranges remaining ones. Activate the always-on-clean mode via Optimize imports on the fly in Editor > General > Auto Import. Modify import rules by sailing to Editor > Code Style > Java > Imports.

Example of optimization:

// Before optimization import java.util.*; // wildcard import alias 'Johnny Bravo' import java.io.File; // a used import, the 'Golden Boy' of this code 😎 import java.util.Map; // an unused import, just like my gym membership 🙈 // After pressing magic key combo Ctrl + Alt + O: import java.io.File; // Only the 'Golden Boy' remains

Enable Iron Man mode:

// Jump into IntelliJ IDEA settings: Editor > General > Auto Import > Optimize imports on the fly // and flick the switch

To tackle imports that shyly refuse to auto-add, power up "Add unambiguous imports on the fly" in the Auto Import settings and get friendly with Ctrl + Space, your go-to buddy for unimported type resolution. Plugins and IntelliJ updates can be your surprise secret weapons. Regularly reassess settings for their worth—it's like spring cleaning for your IDE!

Key customizations for smooth sailing

Taming automatic import settings

Within Settings → Editor → General → Auto Import, there exist a couple of nifty tricks:

  • "Class count to use import with '*'" or as I call it, the 'import cluster unanimity count'. It decides when IntelliJ should resort to a wildcard import.
  • With "Optimize imports on the fly", IntelliJ becomes your personal cleaning service for imports—while you code, isn't that neat?

For your coding crew, align project defaults for imports via Code Style settings to maintain order on deck.

Dealing with the shy imports

Interrupt manual import interruptions:

  • Let Ctrl + Space come to your rescue for shy imports pouting in IntelliJ's corner.
  • Customize settings to auto-add the unambiguous and missing ones, saving time and keyboard health.

Plugins: Your secret toolkit

Step up your game with plugins for times when IntelliJ's built-in features aren't just enough. Some offer bells and whistles for organizing imports beyond IntelliJ's baseline.

Savvy steps and potential hiccups

Project-specific configurations juggle

Every project is a unique snowflake ❄️. Continually twist and turn your Auto Import configurations to match them with project standards, especially if you enjoy hopping between projects.

Being mindful of versions

Remember, import features perform a costume change with different IntelliJ versions. Scout the version-specific settings and consider a costume change (upgrade) for improved import acrobatics.

When exceptions play the villain

Those Topsy-Turvy imports

Every now and then, IntelliJ might throw you a curveball and refuse to organize your imports appropriately. Check for a couple of things:

  • Are any of your import statements tangled up with syntax errors or unresolved references? Untangle them.
  • Is your auto-import settings properly tuned to your coding style and needs?

Embracing a daily ritual

Integrating the Organize imports routine in your everyday coding workflow promotes a smoother and more productive development life. After all, coding is an art of habits 😉