Java Language Guidelines

I. A. General

1... Use consistent indentation of approximately 3-4 spaces.
2. Line up the closing bracket } with the corresponding open bracket {.
3. Use whitespace to improve the readability of your program.
Blank lines are to be encouraged. At a minimum, blank lines should separate declarations, input, different kinds of processing and output. (This is not to imply that programs should be double spaced.)

4. In arithmetic statements spaces should be used to separate variables and operators,
e.g. A = X + Y; not A=X+Y;

5. For arrays no space between array name and entry,
e.g. A[I] not A [I] or A [ I ]

6. Only one statement is permitted on a line.

B. Comments

1. The program heading should occur at the top of the program and should include:

		/*
		NAME:                                                                 DATE:
		PROGRAM SPECIFICATIONS
		NARRATIVE DESCRIPTION:
			:
		INTERFACE:
			INPUTS:
				:
			OUTPUTS:
				:
		CONSTANTS: ...
		*/

2. Each method heading should be followed by the following:

		/*
		PURPOSE:
		RECEIVES:
		RETURNS:
		INPUTS:
		OUTPUTS:
		*/

3. Comments should in general be on separate lines but very short comments are acceptable at the end of a line,e.g.
int GetValue(); // Return the current value

II. Variables and case.

1. Variable class, and method names should be meaningful.
Variable names should be in lower-case except for the first letters of all words after the first,
e.g. thisIsAVariableName
Class definitions should begin with a capital letter
e.g. ThisIsAnObject
Method names should not begin with a capital letter
e.g. thisIsAMethod

2. Non-trivial Constants must be declared and written captitalized,
e.g. final double SQRT2 = 1.414;

3. Use enumerated data types where appropriate.
e.g. enum okay {FALSE, TRUE};

III. Style for blocks, indentation and related comments:

A.

 	if (condition)
                   { 
				      statement(s)
                   }     /* endif */

B.	if (condition)
		{statements;
		}
		else
		{	if (condition2)
			{
			    statements2;
			}
   				:
 			else
			{
			    statement3;
			}
  		}// endif

C.	switch  (expression)
		{	
			case value1: 
	     		statements1;
     			break;
 			case value2:
     			statements2;
     			break;
				:
			default:
				statements;
     			break;
		} //endswitch

D.	while (expression)
		{
			program statements;
			:
 		}

E.	for (init_expression; loop_condition; loop_expression)
		{
			program statements;
			:
		}

F. 	do
		{
			program statements;
			:
		}
		while (expression);

G. public int thisIsAFunction (int par1, int par2)
	{
		:
	}

V. Style
A. All methods should be written to perform one and only one major task.
BACK TO Computer Science I Page