Marc, himself, his blogs, and you reading them.

May 03, 2004
-1 on wildcard imports

Over at Planet Apache I saw Justin Erenkrantz ask about the current recommended practice on doing .* imports in Java.

My first reaction was a slight: "Uh, who's still bothering with all these IDE's that CTRL-O/organize imports for you?" IMHO: There has only ever been one pro for using wildcard imports IMHO: and that is (on short term only) reduced typing and code management involved when doing the .* trick. This advantage (still IMHO) has gone with the new wind of the refactoring IDE's .

The oldest contra-indication I catched (well ahead of these IDE's) was about clarity in debugging/reading your code. If there is 3 .* imports on top and you see there is a problem surrounding some class of type Whatever, then you'ld actually need to get into all of those 3 packages to go and search for the actual class/file to continue your interesting read... Again, with nowadays IDE's that file is just a ctrl-click away. The odd hardcode vi user (perhaps you in remote connection hacking mode) might still be gratefull for the time saver in finding full qualified imports that hint him directly to the exact file locations.

In any case the true reason not to use them any more is compilation speed: 'Not having to look around in 'possible' locations for the dependencies of the files' is an equally appreciated time saver for the compiler. A very torough explanation of this can be found in the most complete (albeit most dull) book I've ever read about Java: Java Rules.

*Update* Uh, I should 've known! When you think there is no reason to discuss, someone will stand up and make some random noise Pun (but no offense) intended on the well chosen blog name. So let's have one more round of counter-arguments:

  • Code get's read (at least) 100 times more then it gets written. (Forgot where I catched this quote) So the bit of time *you* win in typing is asking for a tough pay-back on wasted time on reading/interpretation. Please abondon the idea that you are doing someone a favor.
  • When working in teams, you don't want to be CTRL-O'ing in different styles to limit the amount of noise on cvs-diff lists.
In any case, we already agree on the fact that it's the IDE making the real type-speed difference, and why wouldn't you then configure it so that it also optimises reading speed (where the benefit is 100-fold, remember). And just to show I'm not a hard-liner on this: just make sure you and your team can agree on an optimal modus vivendi, in my book that could be agreeing on 'some' allowed * imports (like java.util and java.awt) where the clarity is often bought by the fact that these classes are known enough.

# Posted by mpo at 10:17 PM | TrackBack
Comments

I agree about not using .* imports, but not for the reasons you outlined here. I have been bitten twice by expansions in the Java platform where there is more than one class that has the same name. Many people would not think twice about combining java.awt.* and java.util.*, but when Java 1.3 came out there was a new java.util.Timer to conflict with java.awt.Timer. Now there is a conflict between an event queueing library and Java 1.5's java.util.Queue. This hit me because a popular coverage tool seems to add some .* imports for me when it instruments my code (C*over--replace the * with 'l'. The comment filtering is a bit zealous). Explicit imports is about the only way to really protect yourself with future compatibility issues.

Posted by: Berin Loritsch at May 7, 2004 06:04 PM

Dull?

Posted by: Doug Dunn at August 24, 2004 04:14 AM
Post a comment









Remember personal info?