Java Coding Style Guide

comments indenting curly braces
return points format width break and continue
files and file contents statemens per line

Comments

I want to see comments in your code, especially for class methods and variable declarations. This means all functions or methods should have the @param and @return listed explicitly. There should be comments on anything that is not obvious. This includes complex code, or things that are not obvious. Remember code is not self-documenting. The standard rule is: comment as if 6 months from now another programmer/student could understand it without you being there to explain it.
Here is an example function, with javadoc style block comments:

public class Power
{
    /**
    * @param  radix   the base for exponentiation
    * @param  expon   the exponent used
    * @return         the result of radix raised to the expon
    * this can be a reciprocal if the the expon is negative
    **/
    public static int power ( int radix, int expon )
    {
        if ( expon < 0 )
        {
            // the answer would be a float
            // we are going to return the reciprocal instead
            // so we can still return an int
            expon *= -1;
        }
        int rval = 1;
        while ( expon > 0 )
        {
            rval *= radix;
            --expon;
        }
        return rval;
    }
    /* main does not need block comments */
    public static final void main ( String args [] )
    {
        System.out.printf ( "%d ^ %d = %d\n", 2, 3, power(2,3) );
        System.out.printf ( "%d ^ %d = %d\n", 3, 2, power(2,3) );
    }
}

return to top

Indenting

Every time a new block of code starts it should be indented, and evenly indented. Indent can range from 4 to 8 spaces, I use 4 spaces. Spaces should always be used instead of tabs. Using tabs can cause the code not to display the same in any editor ( this is known as tab damage )
Here is an example of proper indenting ( but light on comments ) :

import java.io.*;
import java.util.*;

class ReadLine
{
    public static void main ( String[] args )
    {
        // System.in is the keyboard aka stdin
        InputStreamReader isr = new InputStreamReader( System.in );
        // input buffering
        BufferedReader in = new BufferedReader ( isr );
        String curLine = "";
        System.out.println( "input now !!" );
        try
        {
            while ( ( curLine = in.readLine() ) != null )
            {
                //System.out.println( "the line is '" + curLine + "'" );
                // break up the line into its words
                StringTokenizer st =
                    new StringTokenizer ( curLine,
                            " \t\n\f\r,.;:!@" );
                while ( st.hasMoreTokens() )
                {
                    String token = st.nextToken();
                    System.out.println( "the token is '" +
                            token + "'" );
                }
            }
        }
        catch ( IOException dummy_variable )
        {
            System.out.println( "this is very bad" );
        }
    }
}
Continuation of the same line of code should also be indented.
Here is an example ( from the above code ):

                StringTokenizer st =
                    new StringTokenizer ( curLine,
                            " \t\n\f\r,.;:!@" );

return to top

Curly Braces

I want everyone to use what are called East Coast Braces, not West Coast Braces.

Here is an example of west coast which is BAD.


    for( int i = 0; i < 10 ; i ++ ) {
        tmp += arr[i];
    }
Here is an example of east coast which is GOOD.

    for( int i = 0; i < 10 ; i ++ )
    {
        tmp += arr[i];
    }
East coast braces are easier to see if you forgot one, and helps make missing braces easier to spot.
return to top

Return Points

No function or method should have more than one(1) return point. The exception to this rule is if the function is recursive.

Here is what I DON'T want to see.


public static bool func( int n )
{
    if ( n > 10 )
    {
        return true;
    }
    else
    {
        return false;
    }
}
Here is the previous code rewritten to have only one return.

public static bool func( int n )
{
    bool rval = false;
    if ( n > 10 )
    {
        rval = true;
    }
    return rval;
}
This will help you avoid some error conditions in your code. Many programmers miss every condition that is possible and fail to always return a value from a function, this will prevent that error.
return to top

Format Width

All code should be formatted to less 80 characters in width. The standard terminal width also happens to be the standard printer width. If you want a big terminal make it taller, not wider.
return to top

Break and Continue

I frequently see people misuse both break and continue. Break is typically needed in switch statements, but in almost all other situations it is not. Typically a better conditional would have solved the problem. Any use of break in something other than a switch must be clearly comments as to why you are using it.

Most people tend to use continue when re-ordering the conditions correctly would have solved the problem. All uses of continue must be clearly comments as to why you are using it.
return to top


Files and File Contents

work in progress
return to top

Statements per line

I only want to see one statment per line.

Here is what I DON'T want to see.


foo = y; bar = z;
if ( foo == bar ) x = 5;
Here is the correct form of the above code

foo = y;
bar = z;
if ( foo == bar )
    x = 5;

return to top