Discussion:
Some fun "unused import statement" stats....
(too old to reply)
Tom Copeland
2002-11-26 13:17:58 UTC
Permalink
...are available right here:

http://cvs.apache.org/~tcopeland/jakarta_bad_imports.htm

Not a bad percentage. If anyone can suggest any other large bodies of
open source code (with pserver CVS access), I'll run the same report on
those and see what comes up...

Yours,

Tom Copeland
InfoEther
703-486-4543

You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Attila Szegedi
2002-11-26 13:35:35 UTC
Permalink
Heh. Actually, with a reasonable IDE that manages the imports for you (i.e.
Eclipse), this percentage can be kept really low really easy nowadays...

Cheers,
Attila.

--
Attila Szegedi
home: http://www.szegedi.org


----- Original Message -----
From: "Tom Copeland" <***@INFOETHER.COM>
To: <ADVANCED-***@DISCUSS.DEVELOP.COM>
Sent: 2002. november 26. 14:17
Subject: [ADVANCED-JAVA] Some fun "unused import statement" stats....
Post by Tom Copeland
http://cvs.apache.org/~tcopeland/jakarta_bad_imports.htm
Not a bad percentage. If anyone can suggest any other large bodies of
open source code (with pserver CVS access), I'll run the same report on
those and see what comes up...
Yours,
Tom Copeland
InfoEther
703-486-4543
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Tom Copeland
2002-11-26 13:42:27 UTC
Permalink
Yup, IntelliJ's IDEA does the same sort of thing. But somehow unused
imports still seem to creep in here and there...

Tom
-----Original Message-----
From: Moderated discussion of advanced Java topics.
Sent: Tuesday, November 26, 2002 8:36 AM
Subject: Re: [ADVANCED-JAVA] Some fun "unused import
statement" stats....
Heh. Actually, with a reasonable IDE that manages the imports
for you (i.e.
Eclipse), this percentage can be kept really low really easy
nowadays...
Cheers,
Attila.
--
Attila Szegedi
home: http://www.szegedi.org
----- Original Message -----
Sent: 2002. november 26. 14:17
Subject: [ADVANCED-JAVA] Some fun "unused import statement" stats....
Post by Tom Copeland
http://cvs.apache.org/~tcopeland/jakarta_bad_imports.htm
Not a bad percentage. If anyone can suggest any other
large bodies of
Post by Tom Copeland
open source code (with pserver CVS access), I'll run the
same report on
Post by Tom Copeland
those and see what comes up...
Yours,
Tom Copeland
InfoEther
703-486-4543
You can read messages from the ADVANCED-JAVA archive,
unsubscribe, or
Post by Tom Copeland
subscribe to other DevelopMentor lists at
http://discuss.develop.com.
You can read messages from
the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Ken Gentle
2002-11-26 13:49:12 UTC
Permalink
Tom:

"Bad" imports would be "unused" imports, right?

Wouldn't the number of "bad" versus "good" imports be a better metric? I'm
afraid I don't see the correlation between LOC and number of imports.

What's your goal with this measurement?

Just a suggestion, and maybe a question...

Ken
Post by Tom Copeland
http://cvs.apache.org/~tcopeland/jakarta_bad_imports.htm
Not a bad percentage. If anyone can suggest any other large bodies of
open source code (with pserver CVS access), I'll run the same report on
those and see what comes up...
Yours,
Tom Copeland
InfoEther
703-486-4543
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Tom Copeland
2002-11-26 13:57:42 UTC
Permalink
"Bad" == unused, duplicate, or unnecessary (i.e., imports of java.lang
or importing something that's in the same package).

There's not much of a correlation btwn LOC and bad imports... but when I
first ran this report without LOC it was hard to tell if a module really
had problems or not. I mean, 100 unused imports in 1K LOC is more of a
problem then 100 unused imports in 100K LOC. The whole thing was
prompted by someone's post to the jakarta-general list about Netbeans
(or some other big project like that) having a whole slew of unused
imports.

No goal really... just kind of fiddling around with source code metrics.


Yours,

Tom
-----Original Message-----
From: Moderated discussion of advanced Java topics.
Sent: Tuesday, November 26, 2002 8:49 AM
Subject: Re: [ADVANCED-JAVA] Some fun "unused import
statement" stats....
"Bad" imports would be "unused" imports, right?
Wouldn't the number of "bad" versus "good" imports be a
better metric? I'm
afraid I don't see the correlation between LOC and number of imports.
What's your goal with this measurement?
Just a suggestion, and maybe a question...
Ken
Post by Tom Copeland
http://cvs.apache.org/~tcopeland/jakarta_bad_imports.htm
Not a bad percentage. If anyone can suggest any other large
bodies of
Post by Tom Copeland
open source code (with pserver CVS access), I'll run the
same report on
Post by Tom Copeland
those and see what comes up...
Yours,
Tom Copeland
InfoEther
703-486-4543
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Ken Gentle
2002-11-26 14:31:59 UTC
Permalink
Cool... I didn't mean to sound critical, just wanted to know if someone had
come up with a new, meaningful metric.

PMD (http://pmd.sourceforge.net) might be a general purpose critic that
would be useful to the jakarta-general poster.

Ken
Post by Tom Copeland
"Bad" == unused, duplicate, or unnecessary (i.e., imports of java.lang
or importing something that's in the same package).
There's not much of a correlation btwn LOC and bad imports... but when I
first ran this report without LOC it was hard to tell if a module really
had problems or not. I mean, 100 unused imports in 1K LOC is more of a
problem then 100 unused imports in 100K LOC. The whole thing was
prompted by someone's post to the jakarta-general list about Netbeans
(or some other big project like that) having a whole slew of unused
imports.
No goal really... just kind of fiddling around with source code metrics.
Yours,
Tom
-----Original Message-----
From: Moderated discussion of advanced Java topics.
Sent: Tuesday, November 26, 2002 8:49 AM
Subject: Re: [ADVANCED-JAVA] Some fun "unused import
statement" stats....
"Bad" imports would be "unused" imports, right?
Wouldn't the number of "bad" versus "good" imports be a
better metric? I'm
afraid I don't see the correlation between LOC and number of imports.
What's your goal with this measurement?
Just a suggestion, and maybe a question...
Ken
Post by Tom Copeland
http://cvs.apache.org/~tcopeland/jakarta_bad_imports.htm
Not a bad percentage. If anyone can suggest any other large
bodies of
Post by Tom Copeland
open source code (with pserver CVS access), I'll run the
same report on
Post by Tom Copeland
those and see what comes up...
Yours,
Tom Copeland
InfoEther
703-486-4543
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Tom Copeland
2002-11-26 14:45:52 UTC
Permalink
Right on. Unused imports are not a big deal... more of a "huh, that's
interesting" thing. I'm usually much more concerned about the kind of
stuff that this scan:

http://pmd.sourceforge.net/cgi-bin/webpmd.pl

turns up.

Yours,

Tom
-----Original Message-----
From: Moderated discussion of advanced Java topics.
Sent: Tuesday, November 26, 2002 9:32 AM
Subject: Re: [ADVANCED-JAVA] Some fun "unused import
statement" stats....
Cool... I didn't mean to sound critical, just wanted to know
if someone had
come up with a new, meaningful metric.
PMD (http://pmd.sourceforge.net) might be a general purpose
critic that
would be useful to the jakarta-general poster.
Ken
Post by Tom Copeland
"Bad" == unused, duplicate, or unnecessary (i.e., imports of
java.lang
Post by Tom Copeland
or importing something that's in the same package).
There's not much of a correlation btwn LOC and bad
imports... but when I
Post by Tom Copeland
first ran this report without LOC it was hard to tell if a
module really
Post by Tom Copeland
had problems or not. I mean, 100 unused imports in 1K LOC
is more of a
Post by Tom Copeland
problem then 100 unused imports in 100K LOC. The whole thing was
prompted by someone's post to the jakarta-general list about Netbeans
(or some other big project like that) having a whole slew of unused
imports.
No goal really... just kind of fiddling around with source
code metrics.
Post by Tom Copeland
Yours,
Tom
-----Original Message-----
From: Moderated discussion of advanced Java topics.
Sent: Tuesday, November 26, 2002 8:49 AM
Subject: Re: [ADVANCED-JAVA] Some fun "unused import
statement" stats....
"Bad" imports would be "unused" imports, right?
Wouldn't the number of "bad" versus "good" imports be a
better metric? I'm
afraid I don't see the correlation between LOC and number
of imports.
Post by Tom Copeland
What's your goal with this measurement?
Just a suggestion, and maybe a question...
Ken
Post by Tom Copeland
http://cvs.apache.org/~tcopeland/jakarta_bad_imports.htm
Not a bad percentage. If anyone can suggest any other large
bodies of
Post by Tom Copeland
open source code (with pserver CVS access), I'll run the
same report on
Post by Tom Copeland
those and see what comes up...
Yours,
Tom Copeland
InfoEther
703-486-4543
You can read messages from the ADVANCED-JAVA archive,
unsubscribe, or
Post by Tom Copeland
Post by Tom Copeland
subscribe to other DevelopMentor lists at
http://discuss.develop.com.
Post by Tom Copeland
You can read messages
from the ADVANCED-JAVA archive, unsubscribe, or
Post by Tom Copeland
subscribe to other DevelopMentor lists at
http://discuss.develop.com.
Post by Tom Copeland
You can read messages
from the ADVANCED-JAVA archive, unsubscribe, or
Post by Tom Copeland
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Vlad Roubtsov
2002-11-26 14:30:54 UTC
Permalink
Opening many JDK source files shows plenty of redundant imports as well.



Please respond to "Moderated discussion of advanced Java topics."
<ADVANCED-***@DISCUSS.DEVELOP.COM>
Sent by: "Moderated discussion of advanced Java topics."
<ADVANCED-***@DISCUSS.DEVELOP.COM>
To: ADVANCED-***@DISCUSS.DEVELOP.COM
cc:

Subject: [ADVANCED-JAVA] Some fun "unused import statement" stats....

...are available right here:

http://cvs.apache.org/~tcopeland/jakarta_bad_imports.htm

Not a bad percentage. If anyone can suggest any other large bodies of
open source code (with pserver CVS access), I'll run the same report on
those and see what comes up...

Yours,

Tom Copeland
InfoEther
703-486-4543

You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.



You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Tom Copeland
2002-11-26 14:41:18 UTC
Permalink
That's a good idea... let's see what turns up on the JDK source.

[much typing]

OK, for the JDK 1.4 src we've got 347737 LOC and 3369 bad imports. Less
than one percent, not too bad at all...

Yours,

Tom
-----Original Message-----
From: Moderated discussion of advanced Java topics.
Sent: Tuesday, November 26, 2002 9:31 AM
Subject: Re: [ADVANCED-JAVA] Some fun "unused import
statement" stats....
Opening many JDK source files shows plenty of redundant
imports as well.
Please respond to "Moderated discussion of advanced Java topics."
Sent by: "Moderated discussion of advanced Java topics."
Subject: [ADVANCED-JAVA] Some fun "unused import
statement" stats....
http://cvs.apache.org/~tcopeland/jakarta_bad_imports.htm

Not a bad percentage. If anyone can suggest any other large bodies of
open source code (with pserver CVS access), I'll run the same report on
those and see what comes up...

Yours,

Tom Copeland
InfoEther
703-486-4543

You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.



You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Joel Kamentz
2002-11-26 15:21:17 UTC
Permalink
From my past glances at the jdk source, some of the bad imports appear to be a style of protecting against future reorganization changes. For exmaple, importing things in the same package (and possibly other "unnecessary" imports) *might* make it easier to move the class to a different package later?
I attempt to do this myself.

Joel

-----Original Message-----
From: Vlad Roubtsov [mailto:***@TRILOGY.COM]
Sent: Tuesday, November 26, 2002 9:31 AM
To: ADVANCED-***@DISCUSS.DEVELOP.COM
Subject: Re: Some fun "unused import statement" stats....


Opening many JDK source files shows plenty of redundant imports as well.
[...]

You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Tom Copeland
2002-11-26 15:33:27 UTC
Permalink
Huh. That's a new one on me... kind of interesting though...

Tom
-----Original Message-----
From: Moderated discussion of advanced Java topics.
Sent: Tuesday, November 26, 2002 10:21 AM
Subject: Re: [ADVANCED-JAVA] Some fun "unused import
statement" stats....
From my past glances at the jdk source, some of the bad
imports appear to be a style of protecting against future
reorganization changes. For exmaple, importing things in the
same package (and possibly other "unnecessary" imports)
*might* make it easier to move the class to a different package later?
I attempt to do this myself.
Joel
-----Original Message-----
Sent: Tuesday, November 26, 2002 9:31 AM
Subject: Re: Some fun "unused import statement" stats....
Opening many JDK source files shows plenty of redundant
imports as well.
[...]
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Nikolaos Giannopoulos
2002-11-26 12:28:55 UTC
Permalink
Why? When refactor tools (e.g. JBuilder) do a much better job of handling
all this for you. Maybe the SUN team did this once upon a time when these
tools were scarce but that time has passed IMO.

As well, we use JBuilder and it has an optimize imports command that not
only re-arranges your imports according to a template, eliminates
unnecessary ones and shortens ones that are specific. I'm sure other IDE's
have similar capabilities.

--Nikolaos
-----Original Message-----
From: Moderated discussion of advanced Java topics.
Sent: Tuesday, November 26, 2002 3:21 PM
Subject: Re: [ADVANCED-JAVA] Some fun "unused import statement"
stats....
From my past glances at the jdk source, some of the bad imports
appear to be a style of protecting against future reorganization
changes. For exmaple, importing things in the same package (and
possibly other "unnecessary" imports) *might* make it easier to
move the class to a different package later?
I attempt to do this myself.
Joel
-----Original Message-----
Sent: Tuesday, November 26, 2002 9:31 AM
Subject: Re: Some fun "unused import statement" stats....
Opening many JDK source files shows plenty of redundant imports as well.
[...]
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Vlad Roubtsov
2002-11-26 15:24:21 UTC
Permalink
Is your LOC count exclusive of comments?

On a related note, how does PMD compare with checkstyle? I've run
checkstyle a few times on a real project and it was neat. One feature all
such tools need is a built-in diff reporter: don't just report the current
stats, report the delta from a previous run -- would be awesome for team
projects... In other words, accept the codebase as it is initially but
detect and punish whoever makes it worse later...

Vlad.



Please respond to "Moderated discussion of advanced Java topics."
<ADVANCED-***@DISCUSS.DEVELOP.COM>
Sent by: "Moderated discussion of advanced Java topics."
<ADVANCED-***@DISCUSS.DEVELOP.COM>
To: ADVANCED-***@DISCUSS.DEVELOP.COM
cc:

Subject: Re: [ADVANCED-JAVA] Some fun "unused import statement" stats....

That's a good idea... let's see what turns up on the JDK source.

[much typing]

OK, for the JDK 1.4 src we've got 347737 LOC and 3369 bad imports. Less
than one percent, not too bad at all...

Yours,

Tom
-----Original Message-----
From: Moderated discussion of advanced Java topics.
Sent: Tuesday, November 26, 2002 9:31 AM
Subject: Re: [ADVANCED-JAVA] Some fun "unused import
statement" stats....
Opening many JDK source files shows plenty of redundant
imports as well.
Please respond to "Moderated discussion of advanced Java topics."
Sent by: "Moderated discussion of advanced Java topics."
Subject: [ADVANCED-JAVA] Some fun "unused import
statement" stats....
http://cvs.apache.org/~tcopeland/jakarta_bad_imports.htm

Not a bad percentage. If anyone can suggest any other large bodies of
open source code (with pserver CVS access), I'll run the same report on
those and see what comes up...

Yours,

Tom Copeland
InfoEther
703-486-4543

You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.



You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.



You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Tom Copeland
2002-11-26 15:29:49 UTC
Permalink
Yup, the LOC count was done using the excellent JavaNCSS utility -
http://www.kclee.com/clemens/java/javancss/.

PMD and Checkstyle are kind of similar - both are static analysis tools,
both use JavaCC, both have a bunch of rules to run on the code. I think
the main difference is that Checkstyle can do a lot of checking of
Javadoc and variable/method/class name formats, whereas PMD is more
focused on identifying semantic stuff like unused code, questionable
design (like using ArrayList rather than List), and stuff like that.
There's definitely some overlap, and the Checkstyle folks and I have
bounced around some thoughts on combining the two projects....

Yup, history would be kind of cool - one of the PMD developers put in a
similar feature request a while back:

http://sourceforge.net/tracker/index.php?func=detail&aid=583155&group_id
=56262&atid=479924

But no one's really laid down any code on that yet....

Yours,

Tom
-----Original Message-----
From: Moderated discussion of advanced Java topics.
Sent: Tuesday, November 26, 2002 10:24 AM
Subject: Re: [ADVANCED-JAVA] Some fun "unused import
statement" stats....
Is your LOC count exclusive of comments?
On a related note, how does PMD compare with checkstyle? I've run
checkstyle a few times on a real project and it was neat. One
feature all
such tools need is a built-in diff reporter: don't just
report the current
stats, report the delta from a previous run -- would be
awesome for team
projects... In other words, accept the codebase as it is initially but
detect and punish whoever makes it worse later...
Vlad.
Please respond to "Moderated discussion of advanced Java topics."
Sent by: "Moderated discussion of advanced Java topics."
Subject: Re: [ADVANCED-JAVA] Some fun "unused import
statement" stats....
That's a good idea... let's see what turns up on the JDK source.
[much typing]
OK, for the JDK 1.4 src we've got 347737 LOC and 3369 bad
imports. Less
than one percent, not too bad at all...
Yours,
Tom
-----Original Message-----
From: Moderated discussion of advanced Java topics.
Vlad Roubtsov
Sent: Tuesday, November 26, 2002 9:31 AM
Subject: Re: [ADVANCED-JAVA] Some fun "unused import
statement" stats....
Opening many JDK source files shows plenty of redundant
imports as well.
Please respond to "Moderated discussion of advanced Java topics."
Sent by: "Moderated discussion of advanced Java topics."
Subject: [ADVANCED-JAVA] Some fun "unused import
statement" stats....
http://cvs.apache.org/~tcopeland/jakarta_bad_imports.htm
Not a bad percentage. If anyone can suggest any other large bodies of
open source code (with pserver CVS access), I'll run the same
report on
those and see what comes up...
Yours,
Tom Copeland
InfoEther
703-486-4543
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Kenneth Kelley
2002-11-26 15:41:18 UTC
Permalink
Hmm,

Isn't an import sort of equivalent to a #define (which of course,
doesn't exist in java?)

Thus, if you have:

import com.foo.bar.Blat;

Wouldn't that be sort like:

#define Blat com.foo.bar.Blat

All an import does is tell the compiler that "Blat" is really
"com.foo.bar.Blat", right? I know you can remove all the import
statements and replace the class-names will fully defined names, and the
code will compile the same.

So having an unused import is sorta like having a #define that defines
something that never shows up.

But I have noticed that if I have an unused import, then it won't
compile if that class is missing, even though the class isn't used in
the file.

But it's not like C++ where unused header includes can lead to O(N^2)
compile times, right? And it has no effect on the final code, right?


-- Ken

You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Tom Copeland
2002-11-26 15:49:41 UTC
Permalink
Right on, imports are just compiler hints, there's no effect on the
class file:

=======================
[***@ul020-dmz tmp]$ cat Foo.java
import java.util.*;

public class Foo{}
[***@ul020-dmz tmp]$ cat Bar.java
public class Bar{}
[***@ul020-dmz tmp]$ ls -l *.class
-rw-r--r-- 1 build users 234 Nov 26 10:46 Bar.class
-rw-r--r-- 1 build users 234 Nov 26 10:46 Foo.class
[***@ul020-dmz tmp]$
=======================

I think the problems with bad imports are:

- as you pointed out, they introduce misleading dependencies
- they slow down compilation - they've got to be parsed and tokenized
and then resolved in the symbol table
- they take up space in the source code, so you always have to hit [page
down] to get to the actual code :-)

See ya,

Tom
-----Original Message-----
From: Moderated discussion of advanced Java topics.
Sent: Tuesday, November 26, 2002 10:41 AM
Subject: Re: [ADVANCED-JAVA] Some fun "unused import
statement" stats....
Hmm,
Isn't an import sort of equivalent to a #define (which of course,
doesn't exist in java?)
import com.foo.bar.Blat;
#define Blat com.foo.bar.Blat
All an import does is tell the compiler that "Blat" is really
"com.foo.bar.Blat", right? I know you can remove all the import
statements and replace the class-names will fully defined
names, and the
code will compile the same.
So having an unused import is sorta like having a #define that defines
something that never shows up.
But I have noticed that if I have an unused import, then it won't
compile if that class is missing, even though the class isn't used in
the file.
But it's not like C++ where unused header includes can lead to O(N^2)
compile times, right? And it has no effect on the final code, right?
-- Ken
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Danny Ayers
2002-11-26 16:26:37 UTC
Permalink
(I'm new to the list, but am already glad I joined, Tom's metrics are quite
funny, and PMD-WEB is very impressive.)
Post by Kenneth Kelley
Isn't an import sort of equivalent to a #define (which of course,
doesn't exist in java?)
...
Post by Kenneth Kelley
And it has no effect on the final code, right?
Yep & yep, as far as I know.

In one sense it is only a style matter, but then it does effect practice: if
the project includes two sets of packages that are meant to be usable
independently, then you need to be able to easily spot any crossovers. I
find it really handy with Eclipse to do the occasional 'Tidy Imports',
especially if I'm returning to something I've not looked at for a while -
it's like a cheap UML view of the interdependencies. Having the compiler
grumble when an unimported class is added also highlights the fact that
something new is being added, though usually after an initial check I'll
leave it at import xxx.*;

Some IDE generated code I was looking at not long ago used fully-qualified
class names throughout (NetBeans GUI?), and it made it really hard work just
to read it, let alone figure out what packages it was using.

btw, many bad words have been spoken when I've needed to use java.util.List
and the imports include java.awt.*;

Cheers,
Danny.

-----------
Danny Ayers

Semantic Web Log :
http://www.citnames.com/blog

"The lyf so short, the craft so long to lerne." - Chaucer

You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Randall R Schulz
2002-11-26 15:58:58 UTC
Permalink
Ken,

Man, is it good I have garlic close at hand. You really should be careful
about posting such heresy!

Seriously, though, #defines are completely unscoped and apply a blind
lexical transformation to the source code prior to parsing. Java imports
are much more selective in their application.

However, the Sun compiler does interrogate an imported class at the point
of the "import" statement (and complains if the class cannot be found), so
unneeded ones *do* impact compilation time. It doesn't seem that the
language would demand that and it could be done on-demand (but that's just
a hunch, I haven't investigated the language spec to confirm this feeling).

Furthermore, any class mentioned (either in an import or an actual use in
the code) for which only source code is available (no .class file) will get
compiled and that process occurs recursively through all such class
dependencies. That's why invoking "javac" on a single source file can cause
many source files to be compiled.

Randall Schulz
Mountain View, CA USA
Post by Kenneth Kelley
Hmm,
Isn't an import sort of equivalent to a #define (which of course,
doesn't exist in java?)
import com.foo.bar.Blat;
#define Blat com.foo.bar.Blat
All an import does is tell the compiler that "Blat" is really
"com.foo.bar.Blat", right? I know you can remove all the import
statements and replace the class-names will fully defined names, and the
code will compile the same.
So having an unused import is sorta like having a #define that defines
something that never shows up.
But I have noticed that if I have an unused import, then it won't
compile if that class is missing, even though the class isn't used in
the file.
But it's not like C++ where unused header includes can lead to O(N^2)
compile times, right? And it has no effect on the final code, right?
-- Ken
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Joel Kamentz
2002-11-26 15:36:17 UTC
Permalink
Whoa! Typo. That should have been that I don't bother trying to use redundant imports myself!

Joel

-----Original Message-----
From: Tom Copeland [mailto:***@INFOETHER.COM]
Sent: Tuesday, November 26, 2002 10:33 AM
To: ADVANCED-***@DISCUSS.DEVELOP.COM
Subject: Re: Some fun "unused import statement" stats....


Huh. That's a new one on me... kind of interesting though...

Tom
-----Original Message-----
From: Moderated discussion of advanced Java topics.
Sent: Tuesday, November 26, 2002 10:21 AM
Subject: Re: [ADVANCED-JAVA] Some fun "unused import statement"
stats....
From my past glances at the jdk source, some of the bad imports appear
to be a style of protecting against future reorganization changes.
For exmaple, importing things in the same package (and possibly other
"unnecessary" imports)
*might* make it easier to move the class to a different package later?
I attempt to do this myself.
Joel
You can read messages from the ADVANCED-JAVA archive, unsubscribe, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
Loading...