AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why so many imports instead of java.io.* ?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Timothy Madden
Guest





PostPosted: Tue Aug 14, 2012 10:23 am    Post subject: Why so many imports instead of java.io.* ? Reply with quote



Hello

Is it just me or people tend to enumerate all the needed classes for a
Java source file each in its own import line ?

Why is it better to use:
import java.io.IOException
import java.io.FileNotFoundException
import java.io.FileOutputStream
import ...
instead of just:
import java.io.*
?

Which, by the way, also needs no maintenance in case a new class or
exception is needed in the file at a later time.

Thank you,
Timothy Madden
Back to top
Eric Sosman
Guest





PostPosted: Tue Aug 14, 2012 10:45 am    Post subject: Re: Why so many imports instead of java.io.* ? Reply with quote



On 8/14/2012 8:23 AM, Timothy Madden wrote:
Quote:
Hello

Is it just me or people tend to enumerate all the needed classes for a
Java source file each in its own import line ?

Why is it better to use:
import java.io.IOException
import java.io.FileNotFoundException
import java.io.FileOutputStream
import ...
instead of just:
import java.io.*
?

Which, by the way, also needs no maintenance in case a new class or
exception is needed in the file at a later time.

On the other hand, it *will* need maintenance if the next
Java release adds a java.io.Wotsit class, and your file already
has its own Wotsit. Import them one by one and the new Wotsit
won't clash with yours; import them en masse and it will.

The rest of the answer, I think, is that Somebody Somewhere
decided that it was "better style" to import individually than
by wildcard, and SS' opinion got codified into the out-of-the-box
settings for tools like Eclipse and NetBeans. Bowing to SS' ideas,
these tools complain about wildcard imports, while at the same time
making it easy to generate the individual imports. Most people
don't tinker much with the out-of-the-box settings, so the code they
create with these IDE's follow that style.

--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to top
Jeff Higgins
Guest





PostPosted: Tue Aug 14, 2012 11:05 am    Post subject: Re: Why so many imports instead of java.io.* ? Reply with quote



On 08/14/2012 08:23 AM, Timothy Madden wrote:
Quote:
Hello

Is it just me or people tend to enumerate all the needed classes for a
Java source file each in its own import line ?

I suspect it boils down to one of: "I want to.", I have to.", or "Huh?".
Back to top
Patricia Shanahan
Guest





PostPosted: Tue Aug 14, 2012 11:16 am    Post subject: Re: Why so many imports instead of java.io.* ? Reply with quote

On 8/14/2012 5:45 AM, Eric Sosman wrote:
Quote:
On 8/14/2012 8:23 AM, Timothy Madden wrote:
Hello

Is it just me or people tend to enumerate all the needed classes for a
Java source file each in its own import line ?

Why is it better to use:
import java.io.IOException
import java.io.FileNotFoundException
import java.io.FileOutputStream
import ...
instead of just:
import java.io.*
?

Which, by the way, also needs no maintenance in case a new class or
exception is needed in the file at a later time.

On the other hand, it *will* need maintenance if the next
Java release adds a java.io.Wotsit class, and your file already
has its own Wotsit. Import them one by one and the new Wotsit
won't clash with yours; import them en masse and it will.

The rest of the answer, I think, is that Somebody Somewhere
decided that it was "better style" to import individually than
by wildcard, and SS' opinion got codified into the out-of-the-box
settings for tools like Eclipse and NetBeans. Bowing to SS' ideas,
these tools complain about wildcard imports, while at the same time
making it easy to generate the individual imports. Most people
don't tinker much with the out-of-the-box settings, so the code they
create with these IDE's follow that style.


I don't think the out-of-box settings in e.g. Eclipse should drive
actual programming style. A programmer who prefers wildcard imports
should use them, and tell the IDE not to warn on it.

I would perhaps be too lazy to do single class imports without IDE support.

Personally, I prefer specific class imports because that way the imports
give a quick overview of the external features the class uses.

Patricia
Back to top
Eric Sosman
Guest





PostPosted: Tue Aug 14, 2012 12:52 pm    Post subject: Re: Why so many imports instead of java.io.* ? Reply with quote

On 8/14/2012 9:16 AM, Patricia Shanahan wrote:
Quote:
On 8/14/2012 5:45 AM, Eric Sosman wrote:
On 8/14/2012 8:23 AM, Timothy Madden wrote:
Hello

Is it just me or people tend to enumerate all the needed classes for a
Java source file each in its own import line ?

Why is it better to use:
import java.io.IOException
import java.io.FileNotFoundException
import java.io.FileOutputStream
import ...
instead of just:
import java.io.*
?

Which, by the way, also needs no maintenance in case a new class or
exception is needed in the file at a later time.

On the other hand, it *will* need maintenance if the next
Java release adds a java.io.Wotsit class, and your file already
has its own Wotsit. Import them one by one and the new Wotsit
won't clash with yours; import them en masse and it will.

The rest of the answer, I think, is that Somebody Somewhere
decided that it was "better style" to import individually than
by wildcard, and SS' opinion got codified into the out-of-the-box
settings for tools like Eclipse and NetBeans. Bowing to SS' ideas,
these tools complain about wildcard imports, while at the same time
making it easy to generate the individual imports. Most people
don't tinker much with the out-of-the-box settings, so the code they
create with these IDE's follow that style.


I don't think the out-of-box settings in e.g. Eclipse should drive
actual programming style. A programmer who prefers wildcard imports
should use them, and tell the IDE not to warn on it.

Whether they "should" or not, I rather think they tend to.
You've got this IDE with about a jillion configurable behaviors,
and you only tweak those that are actual pain points. If you
prefer wildcard imports strongly enough to spend the time finding
the proper knob you'll do so -- but if it doesn't bother you that
much you won't, and the default will win.

Case in point: Me, the exemplar of lazy programmers. Some
years ago NetBeans would use wildcards if you imported N or more
classes from the same package, individual imports for fewer. At
the time, I spent the small effort to find where N was configured,
and adjusted the threshold a little. But in recent NetBeans
versions that configuration has either vanished or moved somewhere
else, and guess what? I haven't spent the time to try to find it,
but have simply gone with the flow and let NetBeans manage the
imports as it chooses. It means that in some of my .java files you
need to scroll a fair way down before you find anything interesting,
which is a little irritating -- but not irritating enough to get me
to try to do something about it.

(An ironic oddity: In NetBeans, if you refactor a class by moving
it from oldpackage to newpackage, NB adds `import oldpackage.*;' so
any oldpackage classes -- that used to be imported automatically
under the "same package" rule -- are still available in newpackage.
Then when you edit the refactored file, NB complains about the
wildcard import ...)

Quote:
I would perhaps be too lazy to do single class imports without IDE support.

For me, you can delete the "perhaps." Lacking IDE help, I'd
use wildcard imports all over the place to duck the effort of
staying up-to-date with every single class. And I might get unlucky
when the java.io.Wotsit class appears next year ...

Quote:
Personally, I prefer specific class imports because that way the imports
give a quick overview of the external features the class uses.

When it's a shortish list, yes. When you're writing Swing code,
you may find that the import list is so long as to be obfuscatory
rather than explanatory.

--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to top
Roedy Green
Guest





PostPosted: Tue Aug 14, 2012 10:46 pm    Post subject: Re: Why so many imports instead of java.io.* ? Reply with quote

On Tue, 14 Aug 2012 15:23:02 +0300, Timothy Madden
<terminatorul (AT) gmail (DOT) com> wrote, quoted or indirectly quoted someone
who said :

Quote:
Which, by the way, also needs no maintenance in case a new class or
exception is needed in the file at a later time.

My IDE, IntelliJ lets me configure how many classes it should take to
collapse to *.

It pretty well handles inserting them automatically. It is nice to
have the list spelled out. You can tell quite a bit about what a
class does just by looking at the import list.

* can get you in trouble e.g. List which has two completely different
meanings.
--
Roedy Green Canadian Mind Products http://mindprod.com
A new scientific truth does not triumph by convincing its opponents and making them see the light,
but rather because its opponents eventually die, and a new generation grows up that is familiar with it.
~ Max Planck 1858-04-23 1947-10-04
Back to top
Lew
Guest





PostPosted: Tue Aug 14, 2012 11:31 pm    Post subject: Re: Why so many imports instead of java.io.* ? Reply with quote

Timothy Madden wrote:
Quote:
Is it just me or people tend to enumerate all the needed classes for a
Java source file each in its own import line ?

There is a strong indication that import-on-demand is not as good a
practice as single-type imports.

<http://javadude.com/articles/importondemandisevil.html>
<http://www.javabestpractice.com/java/disadvantage-of-using-wildcards-in-java-import-statements.html>

"Using single-type imports is quite useful and an absolute requirement in the open source community as this code is usually really reviewed. Using single-type imports makes it easy for the code reader to quickly find out the package a particular type is in: You just search for the type name from the start of the source file."
<http://jalopy.sourceforge.net/existing/imports.html>

For arguments on both sides:
<http://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad>

And in general, why not do a search yourself across the InterWebz?
<http://lmgtfy.com/?q=java+import+on+demand+vs.+single-type+import>

Quote:
Why is it better to use:
import java.io.IOException
import java.io.FileNotFoundException
import java.io.FileOutputStream
import ...

instead of just:
import java.io.*
?

Less confusion, elimination of simple-name collisions, more explicit
documentation of what types it uses.

Quote:
Which, by the way, also needs no maintenance in case a new class or
exception is needed in the file at a later time.

Not true.

It will need maintenance if that type name conflicts with another.

You don't actually need any import directives in a program at all.

The maintenance required for single-type import is negligible and not
worth avoiding.

--
Lew
Back to top
Timothy Madden
Guest





PostPosted: Thu Aug 16, 2012 11:39 am    Post subject: Re: Why so many imports instead of java.io.* ? Reply with quote

On 08/15/2012 02:31 AM, Lew wrote:
Quote:
Timothy Madden wrote:
Is it just me or people tend to enumerate all the needed classes for a
Java source file each in its own import line ?

There is a strong indication that import-on-demand is not as good a
practice as single-type imports.

http://javadude.com/articles/importondemandisevil.html
http://www.javabestpractice.com/java/disadvantage-of-using-wildcards-in-java-import-statements.html

"Using single-type imports is quite useful and an absolute requirement in the open source community as this code is usually really reviewed. Using single-type imports makes it easy for the code reader to quickly find out the package a particular type is in: You just search for the type name from the start of the source file."
http://jalopy.sourceforge.net/existing/imports.html

For arguments on both sides:
http://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad

And in general, why not do a search yourself across the InterWebz?
http://lmgtfy.com/?q=java+import+on+demand+vs.+single-type+import

Why is it better to use:
import java.io.IOException
import java.io.FileNotFoundException
import java.io.FileOutputStream
import ...

instead of just:
import java.io.*
?

Less confusion, elimination of simple-name collisions, more explicit
documentation of what types it uses.

Which, by the way, also needs no maintenance in case a new class or
exception is needed in the file at a later time.

Not true.

It will need maintenance if that type name conflicts with another.

You don't actually need any import directives in a program at all.

The maintenance required for single-type import is negligible and not
worth avoiding.

Thank you,
After reading the links (one of wich is currently broken) I see that
import lists are a work-around for a language-design problem...

Though I still find import-list a problem for /all/ Java-language
programmers in the world (which is only made a little easier by proper
IDEs), I agree with the them now. They should be used for future
compatibility between your code and the Java run-time classes.

I guess this issue has been a minus for Sun/Oracle since JDK 1.2 onwards.

Timothy Madden
Back to top
Eric Sosman
Guest





PostPosted: Thu Aug 16, 2012 1:51 pm    Post subject: Re: Why so many imports instead of java.io.* ? Reply with quote

On 8/16/2012 9:39 AM, Timothy Madden wrote:
Quote:
[...]
After reading the links (one of wich is currently broken) I see that
import lists are a work-around for a language-design problem...

Not sure what "problem" you think they work around. The
import directive is a convenience, nothing more: It allows you
to abbreviate java.io.BufferedReader as just BufferedReader,
but that's all.

Off-hand, I can't think of a language that has namespaces
or namespace-like things that doesn't also have some way to
allow abbreviations of this kind. If there is such a language,
I bet it's verbose. Remember the Monty Python skit in which
an announcer speaks of the composer

Johann Gambolputty de von Ausfern- schplenden- schlitter-
crasscrenbon- fried- digger- dingle- dangle- dongle- dungle-
burstein- von- knacker- thrasher- apple- banger- horowitz-
ticolensic- grander- knotty- spelltinkle- grandlich-
grumblemeyer- spelterwasser- kurstlich- himbleeisen-
bahnwagen- gutenabend- bitte- ein- nürnburger- bratwustle-
gerspurten- mitz- weimache- luber- hundsfut- gumberaber-
shönedanker- kalbsfleisch- mittler- aucher von Hautkopft
of Ulm

.... and recites the entire thing at every mention? Hey, Java
could have been like that!

--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to top
Timothy Madden
Guest





PostPosted: Fri Aug 17, 2012 10:08 am    Post subject: Re: Why so many imports instead of java.io.* ? Reply with quote

On 08/16/2012 06:51 PM, Eric Sosman wrote:
Quote:
On 8/16/2012 9:39 AM, Timothy Madden wrote:
[...]
After reading the links (one of wich is currently broken) I see that
import lists are a work-around for a language-design problem...

Not sure what "problem" you think they work around. The
import directive is a convenience, nothing more: It allows you
to abbreviate java.io.BufferedReader as just BufferedReader,
but that's all.

Off-hand, I can't think of a language that has namespaces
or namespace-like things that doesn't also have some way to
allow abbreviations of this kind. If there is such a language,
I bet it's verbose.

I see what you mean. A simple test shows that the same problem exists
with C++ namespaces and its using directive.

FreePascal documentation also says that a duplicate identifier is
imported from the unit last specified in the uses clause. I think this
approach produces the same problem as the open imports from Java (or
C++), and even worse ones.

Python is a little smarter: even after import, you still have to use the
qualified name. Or you can import the qualified name explicitly, than
you can use the simple name.

I also tried perl, but I am not good with it. Perl modules look like
they need explicit exports inside the module, if the import directive is
to import anything other than the module name. And these explicit
exports are scarce. All other module symbols need to be accessed as
qualified names, like in python, so it's still good.

I believe Ruby also exhibits no such "import of an open namespace"
problem. At least not directly, because in Ruby a module can serve as a
base class for a derived one, which is also a form of importing (and
extending) an existing namespace. But that is a different problem,
related to OOP, and which does not produce conflicts the same way
imports do.

So I see some languages have this problem when trying to import an "open
namespace", while other languages do not allow the "open namespace" to
be imported directly.

Maybe what Java needs is some new syntax like:
import com.oracle.multiprocessing.* into mp;
and than any function f() from com.oracle.multiprocessing can only be
refferred to as mp.f();

P.S. Python wins (and it rocks, too) !

Thank you,
Timothy Madden
Back to top
Gene Wirchenko
Guest





PostPosted: Fri Aug 17, 2012 5:53 pm    Post subject: Re: Why so many imports instead of java.io.* ? Reply with quote

On Fri, 17 Aug 2012 11:26:14 -0700 (PDT), Lew <lewbloch (AT) gmail (DOT) com>
wrote:

Quote:
Timothy Madden wrote:
P.S. Python wins (and it rocks, too) !

Language War!

It's futile to do Language War with Java partisans because we agree
with the criticisms of the language, and respond with, "Y'think *that's*
bad? What about <insert favorite Java feature to deprecate here>?"

It's what keeps Java vibrant - its community complains more about it
than its opposition, and its fanboys engage in the opposite of the sort
of religiously fervent proselytizing that distinguishes the fanboys of
other computer languages.

But you save it for the formatting and variable naming
conventions.

Sincerely,

Gene Wirchenko
Back to top
Lew
Guest





PostPosted: Fri Aug 17, 2012 6:26 pm    Post subject: Re: Why so many imports instead of java.io.* ? Reply with quote

Timothy Madden wrote:
Quote:
P.S. Python wins (and it rocks, too) !

Language War!

It's futile to do Language War with Java partisans because we agree
with the criticisms of the language, and respond with, "Y'think *that's*
bad? What about <insert favorite Java feature to deprecate here>?"

It's what keeps Java vibrant - its community complains more about it
than its opposition, and its fanboys engage in the opposite of the sort
of religiously fervent proselytizing that distinguishes the fanboys of
other computer languages.

--
Lew
Back to top
Gene Wirchenko
Guest





PostPosted: Fri Aug 17, 2012 6:27 pm    Post subject: Re: Why so many imports instead of java.io.* ? Reply with quote

On Fri, 17 Aug 2012 12:58:18 -0700 (PDT), Lew <lewbloch (AT) gmail (DOT) com>
wrote:

Quote:
Gene Wirchenko wrote:
Lew wrote:
Timothy Madden wrote:
P.S. Python wins (and it rocks, too) !

Language War!

It's futile to do Language War with Java partisans because we agree
with the criticisms of the language, and respond with, "Y'think *that's*
bad? What about <insert favorite Java feature to deprecate here>?"

It's what keeps Java vibrant - its community complains more about it
than its opposition, and its fanboys engage in the opposite of the sort
of religiously fervent proselytizing that distinguishes the fanboys of
other computer languages.

But you save it for the formatting and variable naming
conventions.

I don't understand your remark. I can't recall having complained against
the Java code conventions. What are you attempting to communicate there,
Gene?

Lew, that is rather disingenuous of you. Whenever anyone uses a
different convention, they get put down and rather harshly. It is
quite fanatic what is done.

Sincerely,

Gene Wirchenko
Back to top
Patricia Shanahan
Guest





PostPosted: Fri Aug 17, 2012 7:39 pm    Post subject: Re: Why so many imports instead of java.io.* ? Reply with quote

On 8/17/2012 12:58 PM, Lew wrote:
Quote:
Gene Wirchenko wrote:
Lew wrote:
Timothy Madden wrote:
P.S. Python wins (and it rocks, too) !

Language War!

It's futile to do Language War with Java partisans because we agree
with the criticisms of the language, and respond with, "Y'think *that's*
bad? What about <insert favorite Java feature to deprecate here>?"

It's what keeps Java vibrant - its community complains more about it
than its opposition, and its fanboys engage in the opposite of the sort
of religiously fervent proselytizing that distinguishes the fanboys of
other computer languages.

But you save it for the formatting and variable naming
conventions.

I don't understand your remark. I can't recall having complained against
the Java code conventions. What are you attempting to communicate there,
Gene?


Your attempts to encourage universal use of one set of Java coding
conventions are sometimes difficult to distinguish from religiously
fervent proselytizing.

Patricia
Back to top
Lew
Guest





PostPosted: Fri Aug 17, 2012 7:58 pm    Post subject: Re: Why so many imports instead of java.io.* ? Reply with quote

Gene Wirchenko wrote:
Quote:
Lew wrote:
Timothy Madden wrote:
P.S. Python wins (and it rocks, too) !

Language War!

It's futile to do Language War with Java partisans because we agree
with the criticisms of the language, and respond with, "Y'think *that's*
bad? What about <insert favorite Java feature to deprecate here>?"

It's what keeps Java vibrant - its community complains more about it
than its opposition, and its fanboys engage in the opposite of the sort
of religiously fervent proselytizing that distinguishes the fanboys of
other computer languages.

But you save it for the formatting and variable naming
conventions.

I don't understand your remark. I can't recall having complained against
the Java code conventions. What are you attempting to communicate there,
Gene?

--
Lew
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.