Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any reason to clean up unused imports in Java, other than reducing clutter?

Is there any good reason to avoid unused import statements in Java? As I understand it, they are there for the compiler, so lots of unused imports won't have any impacts on the compiled code. Is it just to reduce clutter and to avoid naming conflicts down the line?

(I ask because Eclipse gives a warning about unused imports, which is kind of annoying when I'm developing code because I don't want to remove the imports until I'm pretty sure I'm done designing the class.)

like image 486
Kip Avatar asked Jun 11 '09 02:06

Kip


People also ask

Why should we remove unused imports?

An unused import still creates a dependency. If you don't realize that a dependency is only because of unused imports, you can waste time updating the module version, investigating vulnerability reports related to that module, etc.

Do unused imports affect performance Java?

Yes it impact a bit on performance, if we are referring unused import statement in our java class. The Java compiler will check for references mentioned into the import statement and at minute level it impact on the performance of the your class.

Do imports slow down Java?

No. Imports are purely a compile time construct ... syntactic sugar. The imports tell the Java compiler how to map identifiers in the source code to fully qualified class names.


2 Answers

I don't think that performance problems or something like that are likely if you are not removing the imports.

But there could be naming conflicts, in rare cases like importing the list interface.

In Eclipse you can always use a shortcut (depends on OS - Win: Ctrl + SHIFT + O and Mac: COMMAND + SHIFT + O) to organize the imports. Eclipse then cleans up the import section removes all the stale imports etc. If you are needing a imported thing again eclipse will add them automatically while you are completing the statement with Ctrl + SPACE. So there is no need in keeping unused code in you class.

As always unused code will distract you and other people while reading the code and leaving something in your active code because of maybe I need it later is mostly seen as bad practice.

like image 118
Janusz Avatar answered Oct 22 '22 20:10

Janusz


One would be that if you remove the class referenced by the import from the classpath, you won't get a silly compiler error that served no purpose. And you won't get false positives when you perform a "where used" search.

Another (but this would be very specific in nature) would be if the unused import had naming conflicts with another import, causing you to use fully qualified names needlessly.

Addendum: Today the build server started failing compilation (not even test running) with an out of memory error. It ran fine forever and the check-ins didn't have any changes to the build process or significant additions that could explain this. After attempting to increase memory settings (this is running a 64 bit JVM on a 64 bit CentOS!) to something well beyond where the clients could compile, I examined the checkins one by one.

There was an improper import that a developer had used and abandoned (they used the class, auto-imported it, and then realized it was a mistake). That unused import pulled in a whole separate tier of the application which, while the IDE isn't configured to separate them, the build process is. That single import dragged in so many classes that the compiler attempted to compile without having the relevant dependent libraries in the classpath, that this caused so many issues that it caused the out of memory error. It took an hour to solve this problem caused by an unused import.

like image 36
Yishai Avatar answered Oct 22 '22 20:10

Yishai