Sunday, December 2, 2007

Java Programming Style Guidelines

Java Programming Style Guidelines:

  • Files
    Java source files should have the extension .java.
    Point.java
    Enforced by the Java tools.
  • File content must be kept within 80 columns.
  • Special characters like TAB and page break must be avoided.
  • The package statement must be the first statement of the file. All files should belong to a specific package.

  • Imported classes should always be listed explicitly.
    import java.util.List; // NOT: import java.util.*; import java.util.ArrayList; import java.util.HashSet;
  • Class and Interface declarations should be organized in the following manner:
    Class/Interface documentation.
    class or interface statement.
    Class (static) variables in the order public, protected, package (no access modifier), private.
    Instance variables in the order public, protected, package (no access modifier), private.
    Constructors.
    Methods (no specific order).
  • Class variables should never be declared public.
  • Variables must never have dual meaning.
  • Arrays should be declared with their brackets next to the type
  • Variables should be kept alive for as short a time as possible.
  • Only loop control statements must be included in the for() construction

  • Loop variables should be initialized immediately before the loop.
    isDone = false; // NOT: bool isDone = false; while (!isDone) { // : : // while (!isDone) { } // : // }
  • The use of do-while loops can be avoided.
  • The use of break and continue in loops should be avoided.

No comments:

Post a Comment