TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
TIOBE - Java Coding Standard
Search

Rule:  ImportStatement101Checked automatically with code checker

Synopsis:Order Import Statements.
Language:Java
Severity Level:7
Category:ImportStatement


Description:

The ordering of import statements is:

  1. Android imports
  2. Imports from third parties (com, junit, net, org)
  3. java and javax

To exactly match the IDE settings, the imports should be:

  • Alphabetical within each grouping, with capital letters before lower case letters (e.g. Z before a).
  • Separated by a blank line between each major grouping (android, com, junit, net, org, java, javax).

Originally, there was no style requirement on the ordering, meaning IDEs were either always changing the ordering or IDE developers had to disable the automatic import management features and manually maintain the imports. This was deemed bad. When java-style was asked, the preferred styles varied wildly and it came down to Android needing to simply "pick an ordering and be consistent." So we chose a style, updated the style guide, and made the IDEs obey it. We expect that as IDE users work on the code, imports in all packages will match this pattern without extra engineering effort.

This style was chosen such that:

  • The imports people want to look at first tend to be at the top (android).
  • The imports people want to look at least tend to be at the bottom (java).
  • Humans can easily follow the style.
  • IDEs can follow the style.

The use and location of static imports have been mildly controversial issues. Some people prefer static imports to be interspersed with the remaining imports, while some prefer them to reside above or below all other imports. Additionally, we have not yet determined how to make all IDEs use the same ordering. Since many consider this a low priority issue, just use your judgement and be consistent.



Literature References:
Google Android Guidelines