Order of import statements

Florian

Active Member
Contributor
Architecture
Intelij and Eclipse sort import statements per default differntly. I think we should configure both to sort them alphabetically for simplicity.

In Intelij it is per default:
  1. Import all other imports
  2. blank line
  3. import javax.*
  4. import java.*
  5. blank line
  6. import static all other imports
In eclipse the default is accoring to msteiger:

java and javax at the top, followed by org and com before everything else.

I think sorting alphabetically would be the best rule as it is simplest. Settings both for intelij and eclipse could be generated.
 

Cervator

Org Co-Founder & Project Lead
Contributor
Design
Logistics
SpecOps
I wonder if there is a convention and/or a setting for Checkstyle to catch anything? Would be nice to both generate IDE config to match as well as do checking for it.
 

prestidigitator

New Member
Contributor
I generally do it like in those google guidelines, with the exception that I only do 3 groups, from most to least "foreign" (so that—mostly—those which are likely to be least familiar come first):
  1. Third-party library packages (including those from the same company/organization as this library but packaged in different libraries, and also including non-standard vendor-specific packages like sun).
  2. Other packages within this library.
  3. Standard libraries (java, javax, and other namespaces specifically defined in the JavaSE/JavaEE API documents like org.omg, org.w3c, org.xml).
  4. (Classes from java.lang are so common and familiar and widely used that they not only come last, but don't have to be—and should never be—listed at all.)
And then within each group I do a lexicographic ordering by package segments (e.g. com.example.SomeClass comes before com.example.pkg.OtherClass because the packages are "com.example" and "com.example.pkg" whereas "SomeClass" is not part of a package name).

When touching a file I haven't authored I always try to stick to the existing style/patterns unless I have to change so much that I'm basically re-authoring the file. I sort of consider this a guideline that trumps all other guidelines.
 
Last edited:

msteiger

Active Member
Contributor
World
Architecture
Logistics
I don't really care how we sort imports, because it doesn't really affect anything but the difference display in git changes. Plus, IDEs collapse them, so you don't even see them.

We might want to consider using a style that is close to the existing one (IntelliJ default I assume) or create one giant mega-commit reorganize them all at once.

Unfortunately, the google style file for eclipse re-configures not only import orders, but everything else, too.

How about using the recommendation by CheckStyle? We could create config files for IDEs to match those ...

http://checkstyle.sourceforge.net/config_imports.html#ImportOrder
 
Top