Style Rules and Guidelines

Beauty is more important in computing than anywhere else in technology because software is so complicated. Beauty is the ultimate defense against complexity.
Good programmers know what's beautiful and bad ones don't.
-- David Gelernter, Machine Beauty

Programming is best regarded as the process of creating works of literature, which are meant to be read.
-- Donald Knuth, Literate Programming

The rules and guidelines on this page are generally consistent with the much more detailed rules used by Sun Microsystems (the designer and maintainer of Java), which they recommend for all Java programmers.

Always obey the rules, prefixed below by R. There will be a significant grade penalty for breaking them. Like grammatical mistakes in English, if you break the rules it is quite distracting, and may cause serious confusion for your reader.

Guidelines, prefixed below by G, should be generally followed or your program becomes hard to read, but violations are not as serious as breaking rules, and exceptions are sometimes appropriate.

  1. R: Start every assignment file with a special comment line containing your lab start time, the assignment number, and the names and usernames of all your assignment team members. For example: // 9:30 lab, a2 by Wilma Wiz, wwiz, and Harry Hacker, hhacker
  2. G: Use descriptive names for all identifiers (variable, method, and class names). Generally avoid single letter names such as a and x . There are a couple times when short names are appropriate:
  3. R: Use the standard Java naming conventions.
  4. Make good use of white space.
  5. R: Use a standard indentation convention consistently. If you start lines in standard places, DrJava will do this for you, and will fix broken indentation if you press tab. There are several common indentation conventions, but all obey the following rules.
  6. G: Use magic numbers only in constant declarations. A magic number is any number other than -1, 0, 1, and 2. This forces you to give meaningful names to mysterious quantities, which goes a long way toward making programs self-documenting.
  7. G: Use meaningful comments to clarify your program.
  8. G: Use public and private visibility modifiers.
  9. R: Group class members in a consistent and meaningful way. This usually means grouping by kind and in order of decreasing visibility. For example, put constants first, then instance variables, and finally methods. Within each of these groups, put public members before private members. Some programmers prefer to give priority to visibility, listing all public members, regardless of their kind, before private members. The following style is recommended: Group class members in the order class variables, instance variable, constructors, methods, and finally nested classes, with the exception that private fields used only by a single method are usually placed just before the method. Within each group, order by decreasing visibility, except for methods, which are grouped with related methods together, starting with the most important and visible.
  10. R: Put field modifiers in the standard order: visibility (such as public or private) first, then static (if present), and finally final (if present).
  11. G: Clarity is almost always the overriding style concern.
    1. Avoid unnecessarily complicated code. Rework the code if you see a better way (called refactoring, another precept of extreme programming).
    2. Clarity is more important than efficiency, unless the efficiency improvement is important to the application. (For most "improvements" programmers are tempted to make in the name of efficiency, it is impossible to detect an efficiency improvement, and sometimes they actually slow the program down by making the compiler's job more difficult.
  12. G: More guidelines for naming:
  13. Avoid dead code that can never be reached due to local program structure. This most commonly results from including an else clause after a series of else ifs that exhaust all possibilities. Instead, delete the last if (which would surely be true) and replace it with a comment indicating the condition.

    Wrong:

    if (x <= y) {
      doThis();
    } 
    else if (x > y) {
      doThat();
    } 
    else impossible();
    
    Ok:
    if (x <= y) {
      doThis();
    } 
    else { // x > y
      doThat(); 
    }
    
    It is still a good idea to detect and report errors that might result from errors in other parts of a program or invalid input resulting in data structures or other forms of program state that are inconsistent with the program design.Thus if local logic analysis does not prove that data is in the expected form in an else clause (or switch default clause), the clause should be replaced by an if statement that tests for the expected form and has an else clause that raises an exception.
  14. Avoid using else { if when else if is appropriate. If an else clause includes an if followed by one or more other statements, it is necessary to follow the else with a left brace; otherwise, do not do so. Also do not increase the level of indentation for an if expression immediately following else when extra braces are not used . Though the if expression technically introduces an extra level of syntactic structure, for indentation purposes it is treated as part of the same structure as the else it follows.

    Wrong:

    if (x == 1) one();
    else {
      if (x == 2) two();
      else moreThanTwo();
    }
    
    Also wrong:
    if (x == 1) one();
    else if (x == 2) two();
         else moreThanTwo();
    
    Ok:
    if (x == 1) one();
    else if (x == 2) two();
    else moreThanTwo();
    
  15. G: Avoid using Vector if its efficiency is of concern: it is amazingly inefficient. java.util.ArrayList is similar, but unsynchronized, so it is faster. Insterion and deletion are still linear time operations, though. 
  16. R: Use exceptions only for rare events: they are quite inefficient.
  17. R: Use assignment operators, such as =, ++, --, and +=, only as statements (so they are used only for the assignment, not for the value they return). Exception: = may be used to advantage in a while loop condition when the alternative is duplicating code or breaking out of the middle of the loop.
  18. G: Break up long declarations. If a method or class declaration is getting long (more than about 30 lines for a method and a page or two for a class), it is often a good idea to break it up.
  19. G: Use class extension only when an instance of the new class "is a" instance of the extended class. Never use extension simply to avoid using dots in names! In other instances in which you may be tempted to use extension, delegation or composition are generally appropriate.
  20. G: When declaring a collection, include a comment "of type " giving the type of the objects in the collection.