Java Coding Style Rules
- Align all curly braces vertically to allow easy visual matching.
- Use four (4) spaces for each indentation.
- Indent all statements contained within compound statements (if, for,
while, {}, etc).
- Use meaningful names for variables, parameters, subprograms, and classes.
(You may use simple names like i, c, s, or f for
general integers, characters, strings, or floats respectively.)
- Define and use names for all constants, e.g., PI instead of 3.14159,
and BUF_SIZE instead of 1024.
- Give an appropriate visibility modifier in front of every member
definition, either public, private, or protected. Only use package scope
when you know it is appropriate.
- Use the following conventions for letter-case of identifiers:
| Identifier | Convention | Examples |
| class names |
capitalize first word, capitalize all other words (no underscores) |
Circle, FilledCircle, ShadedFilledCircle |
| class member names |
lowercase first word, capitalize all other words (no underscores) |
draw(), drawLine(), centerX, centerY, radius |
| function names |
lowercase first word, capitalize all other words (no underscores) |
findIndex(), factorial(), toLowerCase(), isUpperCase() |
| named constants |
all caps, use underscores to separate words |
PI, MAX_BUFFER_SIZE |
- Use whitespace around every binary operator (e.g, x + y instead of x+y)
and around function arguments (e.g, put( x, y ) instead of put(x,y)).
- Keep subprograms simple. At most five simple statements or one
compound statement and two simple statements.
- Keep compound statements simple. At most three simple statements
per statement list.
- Avoid over parenthesization, e.g, return x + 1; instead of return (x + 1);
- Comment each class, each method, and each class variable
(describing it's purpose) using a form similar to the example
at the very end. (Note I only commented a few members to avoid
cluttering up the Class example.)
Examples of statments
simple if/else
if ( x < y )
y = 0;
else
x = 1;
cascaded if/else
if ( var1 < var2 )
{
var1 = ...;
var2 = ...;
...
}
else if ( var1 == var2 )
{
var1 = ...;
var2 = ...;
...
}
else
{
var1 = ...;
var2 = ...;
...
}
loop statements
for ( int i = 0; i < MAX_VAL; i++ )
{
...
...
}
while ( !cin.eof() )
{
cin.get( ch );
cout.put( ch );
}
do // read the first line (including the newline) only
{
cin.get( ch );
cout.put( ch );
} while ( ch != '\n' );
switch statements
switch ( factorial( i ) )
{
case 1:
case 3:
case 5:
cout << "It's odd!\n";
break;
case 2:
case 4:
case 6:
cout << "It's even!\n";
break;
default:
cout << "It's less than 1 or greater than 6!\n";
break;
}
Examples of Class Definitions
import java.awt.*;
/*
class Triangle defines a two dimmensional Triangle
*/
class Triangle
{
// the x,y coordinates of the origin of this Triangle
private int x, y;
// draws this Triangle on the display
public void draw()
{
// ...
}
// inverts this Triangle along a horizontal line
public void flip()
{
// ...
}
// rotates this Triangle by number of degrees specified by angle
public void rotate( int angle )
{
// ...
}
}
Examples of REALLY BAD commenting
All the following comments are poor. Either they state the obvious
about the programming language (typical from programmers just
learning the language), or they don't add any information about
the item they are commenting and the program would actually be more
readable if they were deleted. The final offense is pointing out that a
particular close brace matches some open brace. If you find yourself
needing this, either you aren't aligning your braces properly, or your
code is getting too complex.
/*
This is a class defintion for Shape.
*/
class Shape
{
public:
// These are abstract methods
// draws shape
abstract void draw();
// flips shape
abstract void flip();
// rotates shape
abstract void rotate( int angle );
} // end of Shape
// A Point class
class Point
{
// Two integers
private int x, y;
// a constructor
public Point( int newX, int newY )
{
// initialize x and y
x = newX;
y = newY;
} // end of Point
}
class Rectangle
extends Shape // Rectangle extends a Shape
{
// some private members
private Point tl;
private Point br;
private int angle;
// the public members
public Rectangle() // constructor
{
}
// ...
// some member functions
void draw()
{
// ...
}
void flip()
{
if ( x != y )
{
swap( x, y ); // swap the values of x and y
flip(); // flip again
} // end if
} // end flip()
void rotate( int angle )
{
}
float area()
{
}
} // end Rectangle