Mail lists

Coding Guidelines

Diva tries to take a non-controversial approach to coding guidelines. We treat guidelines as guidelines, not as commandments. Below are the conventions that we have adopted, and which we like to see in code that is part of Diva. As usual, the best guide is the existing code. There are many other sources of guidelines, here are a couple of good ones:

Java Code Conventions
Doug Lea's guidelines


Here are the key conventions we have adopted in Diva:

  • Each file begins with a standard copyright disclaimer. See any source file.

  • Code indenting is set at four spaces.

  • The order of declarations in a class is:
    1. Instance variables
    2. Constructors
    3. Public methods, in alphabetical order
    4. Protected and private methods, in alphabetical order
    5. Inner classes

  • There are no protected variables. Use a private variable and public setter and getter methods instead.

  • Package-private and private variables have names beginning with an underscore.

  • Every public and package-private variable and method has a proper doc-comment, for extraction by javadoc.

  • If a file is long enough, use separator comments like this:
        //// Public methods


Here are a few reminders of accepted Java coding conventions, just in case you have forgotten them :-)

  • Methods that return an Iterator are named as the plural of the thing they return. For example,
        public Enumeration elements () {

  • Use "set" and "get" in names of methods that set and return a property-like value. For example,
        public FooBar getFooBar ();
        public void setFooBar (FooBar f);
    If the type is boolean, use "set" and "is":
        public boolean isVisible ();
        public void setVisible (boolean flag);

  • Static constants are uppercase. (Sun doesn't consistently follow this convention themselves.) For example,
        public static int LARGER_THAN_LIFE;


Here are some recommendations for things that we have found to make our lives easier:

  • Place a space before the left parenthesis in a method declaration. For example,
        public int foo (double bar) {
    This makes it easy to find method declarations (useful if you use an editor that doesn't do it for you).
Send feedback to cxh at eecs berkeley edu
©2002-2018 U.C. Regents