CS101 CS101/CS102 - Style Guidelines

The following style guidelines will be strictly enforced!
(see also Appendix F of your textbook.)

Header Programs will begin with a comment block which includes the program name, description, author, date and an edit history.
Comments Whenever the function of a block of code is not immediately obvious, include a comment that explains it. In general, comments should be obtained from and therefore match the algorithm done during the design phase.
Blank Lines Blank lines should be used between methods and elsewhere to help clarify the structure of the program and to make it easier to read.
Spaces Use additional space characters to help make your code easier to read.

Put a space before and after operators, eg. =, +, -, *, /, %, ... See examples below.

Put  space after the opening parentheses of a method call & before the parameters, and put  space after the closing parentheses of a method call
    e.g.  x = doSomething( x, y, z );
Exception- if there are no parameters, do not put any spaces
 
e.g.  System.out.println();

Put a space after each semi-colon in the for loop

Meaningful names All variables, methods and classes will be meaningfully named!
Naming conventions Constants: are all uppercase (eg. SPEEDOFLIGHT ) You may use the underscore character to separate out words if you wish (eg. SPEED_OF_LIGHT)

Variables: lowercase but with the first letter of each embedded word except the first, capitalised (eg. sumSoFar)

Classes: lowercase, but first letter of every word capitalised (eg. BigNumbers )

Packages & Interfaces: follow the same rules as class names.

Indentation Programs (and algorithms) must be correctly laid out & indented as follows:
 
public class ClassName 
{
   // body of class
}
public int methodName( x, y ) 
{
   // body of method
}
if ( x <= y ) 
{
   // then part
}
if ( x <= y ) 
{
   // then part
}
else 
{
   // else part
}
if ( x < y ) 
{
   // first part
} 
else if ( x == y ) 
{
   // second part
}
else if ( x != y ) 
{
   // third part
} 
else 
{
   // last part
}
while ( x != y ) 
{
   // body of while loop
}
for ( int column = 0; column < MAXCOLUMNS; column++ ) 
{
   // body of for loop
}
do 
{
   // body of do-while loop
} while ( x != y );
Note: In each case, putting the opening brace "{" on the end of the preceding line is also acceptable. For example:
while ( x != y ) {
   // body of while loop
}

Whichever style you choose, be consistent!

Tip: Most modern integrated development environments, such as DrJava, JCreator, BlueJ, NetBeans and JBuilder, can arrange the indentation for you almost automatically as you type the code. Learning how to do this properly will save you a lot of time! Beginners frequently type the code first without any indentation, then indent it once it is debugged and working. This is a complete waste of time and defeats part of the reason for indentation in the first place.