You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@geronimo.apache.org by Alex Blewitt <Al...@ioshq.com> on 2003/08/13 18:44:13 UTC

Coding standards: import thing.* or thing.Class?

Has anyone got any preferences between using:

import java.util.*

or

import java.util.Vector
import java.util.Enumeration

etc.

(Yes, I know I should be using List instead of Vector :-)

Despite some articles trying to claim otherwise, there's really very 
little difference between the two as far as the code is concerned. In 
fact, you could argue that the import package.Type is actually slower 
than the import package.* variant as there's more text to be parsed.

The only real difference IMNSO is that if you have

import java.util.*

then this says 'This type is dependent on types in the java.util 
package"

whereas

import java.util.Vector
import java.util.Enumeration

says "I specifically depend on Vector and Enumeration in the java.util 
package"

People who use powerful IDEs (i.e.,they write the import statements for 
you) generally prefer the more verbose and detailed import types; those 
using text editors will no doubt prefer to use the package wildcards.

Any votes on which one is better/worse/standard?

Alex.


Re: Coding standards: import thing.* or thing.Class?

Posted by Gareth Bryan <ga...@fastmail.fm>.
Definately a +1 for more specific imports.

In other projects I have worked on, we defined a number of classes
required to be imported from the same package before "import blah.*" was
considered acceptable.

On Wed, 13 Aug 2003 12:48:52 -0400, "Berin Loritsch"
<bl...@apache.org> said:
> Alex Blewitt wrote:
> 
> > Has anyone got any preferences between using:
> > 
> > import java.util.*
> > 
> > or
> > 
> > import java.util.Vector
> > import java.util.Enumeration
> > 
> > etc.
> > 
> > Any votes on which one is better/worse/standard?
> > 
> 
> 
> Yep:
> 
> I vote to be more specific.
> 
> Reason:
> 
> When Java 1.3 came out and introduced the java.util.Timer class it broke
> code bases that imported both java.awt.* and java.util.* (like ArgoUML).
> 
> When you are more specific you are less likely to run into these issues
> in the future.
> 
> -- 
> 
> "They that give up essential liberty to obtain a little temporary safety
>   deserve neither liberty nor safety."
>                  - Benjamin Franklin
> 
-- 
  Gareth Bryan
  garethbryan@fastmail.fm

-- 
http://www.fastmail.fm - Consolidate POP email and Hotmail in one place

Re: Coding standards: import thing.* or thing.Class?

Posted by Pasi Salminen <pa...@profitsoftware.com>.
I prefer using

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

I never use * to import anything. Fully qualified names are only used in the
code if there's a collission in names. I've found this quite natural.

Paci

"Noel J. Bergman" wrote:

> Alex Blewitt wrote:
> > Has anyone got any preferences between using:
> >
> > import java.util.*
> >
> > or
> >
> > import java.util.Vector
> > import java.util.Enumeration
>
> Personally, in most cases I find that the actual pattern is something like:
>
>   import java.util.Collection;
>
>   Collection mywhatever;
>
>   mywhatever = new java.util.SOME_COLLECTION_TYPE(...);
>
> I do not bother to import the specialized class used only once.  I do import
> the base type that is part of the interface.
>
>         --- Noel

__________________________________________________________________________

This message and its attachments have been found clean from known viruses 
with three different antivirus programs.
__________________________________________________________________________

Re: Coding standards: import thing.* or thing.Class?

Posted by James Strachan <ja...@yahoo.co.uk>.
On Thursday, August 14, 2003, at 08:31  am, Jason Dillon wrote:

>> Its one key press in a decent IDE - grouped by package & sorted 
>> alphabetically :)
>
> I will have to give a decent IDE a chance again.  I was using Eclipse 
> a while ago, and was mostly pleased, except that it kept crashing on 
> my powerbook from time to time, which was really pissing me off.

It works great now - especially 3.0 m2. Though I've found its good to 
start with an empty repository & reimport your projects back in

> I played with IntelliJ for a while too, but they did not have a 
> release of there latest baby for OS X, so I had to play with the old 
> version.

The latest & greatest works on OS X now.

> Also, I tend to edit more than just .java stuff, like .php, .sh and I 
> did not really find those ide's to helpful in that respect.
> Unfortunatly my crappy ISP in Thailand will never let me download 
> something as big as Eclipse with out disconnecting me, so I think I 
> may be stuck with JEdit for now.

Shame!

James
-------
http://radio.weblogs.com/0112098/


Re: Coding standards: import thing.* or thing.Class?

Posted by Jason Dillon <ja...@coredevelopers.net>.
> Its one key press in a decent IDE - grouped by package & sorted 
> alphabetically :)

I will have to give a decent IDE a chance again.  I was using Eclipse a 
while ago, and was mostly pleased, except that it kept crashing on my 
powerbook from time to time, which was really pissing me off.  I played 
with IntelliJ for a while too, but they did not have a release of there 
latest baby for OS X, so I had to play with the old version.

Also, I tend to edit more than just .java stuff, like .php, .sh and I 
did not really find those ide's to helpful in that respect.

Unfortunatly my crappy ISP in Thailand will never let me download 
something as big as Eclipse with out disconnecting me, so I think I may 
be stuck with JEdit for now.

--jason


Re: Coding standards: import thing.* or thing.Class?

Posted by James Strachan <ja...@yahoo.co.uk>.
On Thursday, August 14, 2003, at 08:00  am, Jason Dillon wrote:
>> A bunch of people have said they've preferred to use the import 
>> fully.qualified.ClassName approach. I don't see that there is a 
>> problem with having fully qualified references in the code for a 
>> single use of a class (like an implementation of a class name); 
>> however, your class will then 'depend' on that implementation.
>
> Agreed.  I think the general rule is just to avoid glob imports, then 
> leave the usage of FQCNs up to the discretion of the developer.  Oh, 
> but I would suggest that FQCNs if used be minimized.  I have seen some 
> people (and IDEs) which like to code everything with FQCNs and that is 
> just an ugly, ugly mess to deal with.
>
> I do not agree that we should have any concrete rules on how to sort 
> imports though.  I would like to see imports grouped, but I do not 
> really care that they are alphabetical or whatever.  Furthermore I do 
> not want to spend time alphabetizing my imports or performing other 
> such superfluous code alterations.

Its one key press in a decent IDE - grouped by package & sorted 
alphabetically :)

James
-------
http://radio.weblogs.com/0112098/


Re: Coding standards: import thing.* or thing.Class?

Posted by Jason Dillon <ja...@coredevelopers.net>.
> A bunch of people have said they've preferred to use the import 
> fully.qualified.ClassName approach. I don't see that there is a 
> problem with having fully qualified references in the code for a 
> single use of a class (like an implementation of a class name); 
> however, your class will then 'depend' on that implementation.

Agreed.  I think the general rule is just to avoid glob imports, then 
leave the usage of FQCNs up to the discretion of the developer.  Oh, 
but I would suggest that FQCNs if used be minimized.  I have seen some 
people (and IDEs) which like to code everything with FQCNs and that is 
just an ugly, ugly mess to deal with.

I do not agree that we should have any concrete rules on how to sort 
imports though.  I would like to see imports grouped, but I do not 
really care that they are alphabetical or whatever.  Furthermore I do 
not want to spend time alphabetizing my imports or performing other 
such superfluous code alterations.

--jason


Re: Coding standards: import thing.* or thing.Class?

Posted by Alex Blewitt <Al...@ioshq.com>.
A bunch of people have said they've preferred to use the import 
fully.qualified.ClassName approach. I don't see that there is a problem 
with having fully qualified references in the code for a single use of 
a class (like an implementation of a class name); however, your class 
will then 'depend' on that implementation.

Since some developers like looking through a bunch of the imports to 
see what the class depends on, 'hiding' it by not listing it as an 
import may hide dependencies from some authors. (Plus, if an Eclipse 
user does 'Organise Imports' then it may get taken away accidentally 
...)

I don't know if we need to do an official vote or not, but the feedback 
seems to suggest that the import * shouldn't be used, and I've updated 
the Wiki  ApacheJ2EE/CodingStandards page to reflect this.

Aex.

On Wednesday, Aug 13, 2003, at 18:22 Europe/London, Noel J. Bergman 
wrote:

> Alex Blewitt wrote:
>> Has anyone got any preferences between using:
>>
>> import java.util.*
>>
>> or
>>
>> import java.util.Vector
>> import java.util.Enumeration
>
> Personally, in most cases I find that the actual pattern is something 
> like:
>
>   import java.util.Collection;
>
>   Collection mywhatever;
>
>   mywhatever = new java.util.SOME_COLLECTION_TYPE(...);
>
> I do not bother to import the specialized class used only once.  I do 
> import
> the base type that is part of the interface.
>
> 	--- Noel
>


Re: Coding standards: import thing.* or thing.Class?

Posted by James Strachan <ja...@yahoo.co.uk>.
On Wednesday, August 13, 2003, at 06:22  pm, Noel J. Bergman wrote:
> Alex Blewitt wrote:
>> Has anyone got any preferences between using:
>>
>> import java.util.*
>>
>> or
>>
>> import java.util.Vector
>> import java.util.Enumeration
>
> Personally, in most cases I find that the actual pattern is something 
> like:
>
>   import java.util.Collection;
>
>   Collection mywhatever;
>
>   mywhatever = new java.util.SOME_COLLECTION_TYPE(...);
>
> I do not bother to import the specialized class used only once.  I do 
> import
> the base type that is part of the interface.

There's always one :). Seriously, it just shows how subjective coding 
styles are really.

FWIW I prefer to have all imports at the top of the file, expanded and 
then never to use fully qualified class names in the actual code so that

* folks can see exactly what the dependencies of a class are by just 
looking at the imports rather than trailing through the whole code 
(this can help show some bad package hierarchies etc).

* folks that aren't interested in dependencies can skip the imports 
(most IDEs go straight to the meat of the class anyways)

* the actual code you write (the stuff you spend more time reading) is 
simpler and shorter due to lack of fully qualified class names

The only exceptions to this rule are when you get class names clashing 
- but typically thats a code smell which you can fix by refactoring.


The beauty of coding standards is we've so many to choose from... :)

James
-------
http://radio.weblogs.com/0112098/


RE: Coding standards: import thing.* or thing.Class?

Posted by "Noel J. Bergman" <no...@devtech.com>.
Alex Blewitt wrote:
> Has anyone got any preferences between using:
>
> import java.util.*
>
> or
>
> import java.util.Vector
> import java.util.Enumeration

Personally, in most cases I find that the actual pattern is something like:

  import java.util.Collection;

  Collection mywhatever;

  mywhatever = new java.util.SOME_COLLECTION_TYPE(...);

I do not bother to import the specialized class used only once.  I do import
the base type that is part of the interface.

	--- Noel


Re: Coding standards: import thing.* or thing.Class?

Posted by Berin Loritsch <bl...@apache.org>.
Alex Blewitt wrote:

> Has anyone got any preferences between using:
> 
> import java.util.*
> 
> or
> 
> import java.util.Vector
> import java.util.Enumeration
> 
> etc.
> 
> Any votes on which one is better/worse/standard?
> 


Yep:

I vote to be more specific.

Reason:

When Java 1.3 came out and introduced the java.util.Timer class it broke
code bases that imported both java.awt.* and java.util.* (like ArgoUML).

When you are more specific you are less likely to run into these issues
in the future.

-- 

"They that give up essential liberty to obtain a little temporary safety
  deserve neither liberty nor safety."
                 - Benjamin Franklin


Re: Coding standards: import thing.* or thing.Class?

Posted by Siva <si...@sivasundaram.com>.
+1 for individual imports.Its clear and unambiguous

This is the standard which i have seen in James,Jetty and few other
projects.
Also i use it for my work using Intellij Idea.

Siva

----- Original Message -----
From: "Alex Blewitt" <Al...@ioshq.com>
To: <ge...@incubator.apache.org>
Sent: Wednesday, August 13, 2003 10:14 PM
Subject: Coding standards: import thing.* or thing.Class?


> Has anyone got any preferences between using:
>
> import java.util.*
>
> or
>
> import java.util.Vector
> import java.util.Enumeration
>
> etc.
>
> (Yes, I know I should be using List instead of Vector :-)
>
> Despite some articles trying to claim otherwise, there's really very
> little difference between the two as far as the code is concerned. In
> fact, you could argue that the import package.Type is actually slower
> than the import package.* variant as there's more text to be parsed.
>
> The only real difference IMNSO is that if you have
>
> import java.util.*
>
> then this says 'This type is dependent on types in the java.util
> package"
>
> whereas
>
> import java.util.Vector
> import java.util.Enumeration
>
> says "I specifically depend on Vector and Enumeration in the java.util
> package"
>
> People who use powerful IDEs (i.e.,they write the import statements for
> you) generally prefer the more verbose and detailed import types; those
> using text editors will no doubt prefer to use the package wildcards.
>
> Any votes on which one is better/worse/standard?
>
> Alex.
>


Re: Coding standards: import thing.* or thing.Class?

Posted by Andy Jefferson <an...@ajsoft.net>.
On Wednesday 13 Aug 2003 17:44, Alex Blewitt wrote:
> Has anyone got any preferences between using:
>
> import java.util.*
> or
> import java.util.Vector
> import java.util.Enumeration

Fully specified sounds much better ...  :-)

FWIW, You can use Maven's checkstyle report to check on it for you ... 
attached is a checkstyle.xml file that I've used elsewhere (it may need 
adapting to include all of the coding standards on Geronimo). What you need 
to do to use it is edit your project.properties file and add the line
maven.checkstyle.properties=${basedir}/etc/checkstyle.properties
and put the checkstyle.properties in the top level etc/ directory.

That way, whenever you run the checkstyle report it will check the projects 
coding standards :-)

-- 
Andy

Re: Coding standards: import thing.* or thing.Class?

Posted by Henri Yandell <ba...@generationjava.com>.
People [ie) me] using text editors a lot prefer the explicit import rather
than wildcard. You ever looked at:

import com.foo.util.*;
import com.foo.prod1.*;
import javax.swing.*;
import java.util.*;
import com.foo.ejb.*;

and then seen a class called 'BeanHelper' in the code and thought 'WTF!'.

S'nightmare :)

On a sitenote, someone ought to use Geronimo as a "religious arguments you
can have while setting up a Java project" template. Though to the credit
of the community, it's all moving smoothly.

Hen

On 13 Aug 2003, Hiram Chirino wrote:

> +1 for expanded imports.
>
> Regards,
> Hiram
>
> On Wed, 2003-08-13 at 12:47, James Strachan wrote:
> > +1 for expand imports. It makes reading the code easier. Code is there
> > to be read by humans - so anything which helps comprehension (while not
> > going stale like too much documentation can do) is a very good thing.
> >
> > I often tend to do an 'organise imports' in eclipse when code starts
> > having unused imports in it etc.
> >
> >
> > On Wednesday, August 13, 2003, at 05:44  pm, Alex Blewitt wrote:
> >
> > > Has anyone got any preferences between using:
> > >
> > > import java.util.*
> > >
> > > or
> > >
> > > import java.util.Vector
> > > import java.util.Enumeration
> > >
> > > etc.
> > >
> > > (Yes, I know I should be using List instead of Vector :-)
> > >
> > > Despite some articles trying to claim otherwise, there's really very
> > > little difference between the two as far as the code is concerned. In
> > > fact, you could argue that the import package.Type is actually slower
> > > than the import package.* variant as there's more text to be parsed.
> > >
> > > The only real difference IMNSO is that if you have
> > >
> > > import java.util.*
> > >
> > > then this says 'This type is dependent on types in the java.util
> > > package"
> > >
> > > whereas
> > >
> > > import java.util.Vector
> > > import java.util.Enumeration
> > >
> > > says "I specifically depend on Vector and Enumeration in the java.util
> > > package"
> > >
> > > People who use powerful IDEs (i.e.,they write the import statements
> > > for you) generally prefer the more verbose and detailed import types;
> > > those using text editors will no doubt prefer to use the package
> > > wildcards.
> > >
> > > Any votes on which one is better/worse/standard?
> > >
> > > Alex.
> > >
> > >
> >
> > James
> > -------
> > http://radio.weblogs.com/0112098/
> --
> /**************************
>   * Hiram Chirino
>   * Partner
>   * Core Developers Network
>   **************************/
>


Re: Coding standards: import thing.* or thing.Class?

Posted by Hiram Chirino <hi...@coredevelopers.net>.
+1 for expanded imports.

Regards,
Hiram

On Wed, 2003-08-13 at 12:47, James Strachan wrote:
> +1 for expand imports. It makes reading the code easier. Code is there 
> to be read by humans - so anything which helps comprehension (while not 
> going stale like too much documentation can do) is a very good thing.
> 
> I often tend to do an 'organise imports' in eclipse when code starts 
> having unused imports in it etc.
> 
> 
> On Wednesday, August 13, 2003, at 05:44  pm, Alex Blewitt wrote:
> 
> > Has anyone got any preferences between using:
> >
> > import java.util.*
> >
> > or
> >
> > import java.util.Vector
> > import java.util.Enumeration
> >
> > etc.
> >
> > (Yes, I know I should be using List instead of Vector :-)
> >
> > Despite some articles trying to claim otherwise, there's really very 
> > little difference between the two as far as the code is concerned. In 
> > fact, you could argue that the import package.Type is actually slower 
> > than the import package.* variant as there's more text to be parsed.
> >
> > The only real difference IMNSO is that if you have
> >
> > import java.util.*
> >
> > then this says 'This type is dependent on types in the java.util 
> > package"
> >
> > whereas
> >
> > import java.util.Vector
> > import java.util.Enumeration
> >
> > says "I specifically depend on Vector and Enumeration in the java.util 
> > package"
> >
> > People who use powerful IDEs (i.e.,they write the import statements 
> > for you) generally prefer the more verbose and detailed import types; 
> > those using text editors will no doubt prefer to use the package 
> > wildcards.
> >
> > Any votes on which one is better/worse/standard?
> >
> > Alex.
> >
> >
> 
> James
> -------
> http://radio.weblogs.com/0112098/
-- 
/**************************
  * Hiram Chirino
  * Partner
  * Core Developers Network
  **************************/


Re: Coding standards: import thing.* or thing.Class?

Posted by Jason Dillon <ja...@coredevelopers.net>.
+1

--jason


On Wednesday, August 13, 2003, at 11:47  PM, James Strachan wrote:

> +1 for expand imports. It makes reading the code easier. Code is there 
> to be read by humans - so anything which helps comprehension (while 
> not going stale like too much documentation can do) is a very good 
> thing.
>
> I often tend to do an 'organise imports' in eclipse when code starts 
> having unused imports in it etc.
>
>
> On Wednesday, August 13, 2003, at 05:44  pm, Alex Blewitt wrote:
>
>> Has anyone got any preferences between using:
>>
>> import java.util.*
>>
>> or
>>
>> import java.util.Vector
>> import java.util.Enumeration
>>
>> etc.
>>
>> (Yes, I know I should be using List instead of Vector :-)
>>
>> Despite some articles trying to claim otherwise, there's really very 
>> little difference between the two as far as the code is concerned. In 
>> fact, you could argue that the import package.Type is actually slower 
>> than the import package.* variant as there's more text to be parsed.
>>
>> The only real difference IMNSO is that if you have
>>
>> import java.util.*
>>
>> then this says 'This type is dependent on types in the java.util 
>> package"
>>
>> whereas
>>
>> import java.util.Vector
>> import java.util.Enumeration
>>
>> says "I specifically depend on Vector and Enumeration in the 
>> java.util package"
>>
>> People who use powerful IDEs (i.e.,they write the import statements 
>> for you) generally prefer the more verbose and detailed import types; 
>> those using text editors will no doubt prefer to use the package 
>> wildcards.
>>
>> Any votes on which one is better/worse/standard?
>>
>> Alex.
>>
>>
>
> James
> -------
> http://radio.weblogs.com/0112098/
>


Re: Coding standards: import thing.* or thing.Class?

Posted by James Strachan <ja...@yahoo.co.uk>.
+1 for expand imports. It makes reading the code easier. Code is there 
to be read by humans - so anything which helps comprehension (while not 
going stale like too much documentation can do) is a very good thing.

I often tend to do an 'organise imports' in eclipse when code starts 
having unused imports in it etc.


On Wednesday, August 13, 2003, at 05:44  pm, Alex Blewitt wrote:

> Has anyone got any preferences between using:
>
> import java.util.*
>
> or
>
> import java.util.Vector
> import java.util.Enumeration
>
> etc.
>
> (Yes, I know I should be using List instead of Vector :-)
>
> Despite some articles trying to claim otherwise, there's really very 
> little difference between the two as far as the code is concerned. In 
> fact, you could argue that the import package.Type is actually slower 
> than the import package.* variant as there's more text to be parsed.
>
> The only real difference IMNSO is that if you have
>
> import java.util.*
>
> then this says 'This type is dependent on types in the java.util 
> package"
>
> whereas
>
> import java.util.Vector
> import java.util.Enumeration
>
> says "I specifically depend on Vector and Enumeration in the java.util 
> package"
>
> People who use powerful IDEs (i.e.,they write the import statements 
> for you) generally prefer the more verbose and detailed import types; 
> those using text editors will no doubt prefer to use the package 
> wildcards.
>
> Any votes on which one is better/worse/standard?
>
> Alex.
>
>

James
-------
http://radio.weblogs.com/0112098/