Programs that check style:
Above all be consistent and clear.
disk_1 disk_2 disk_3
smallDisk mediumDisk largeDisk
void free ()
int assign ()
float multiply ()
void rescale ()
void printTitles ()
int rollTwo ()
void correctTable ()
Functions that return a boolean value often begin with is
boolean isEmpty ()
boolean isAllZero ()
void setField (int field) { this.field = field; }
int getField () { return field; }
interface Computable {
int compute (String x, float y);
}
Hungarian notation uses leading (or occasionally) trailing tags that identify the type of the object. "Avoid ``Hungarian''-style naming conventions which encode type information in variable names. They may be systematic, but they'll screw you if you ever need to change the type of a variable. If the variable has a small scope, the type will be visible in the declaration, so the annotation is useless clutter. If the variable has a large scope, the code should modular against a change in the variable's type. In general, I think any deterministic algorithm for producing variable names will have the same effect." Larson's C Coding Style (The term Hungarian refers to the nationality of Charles Simonyi, a programmer at Microsoft who wrote a doctoral thesis in the 1980's called "Program Identifier Naming Conventions").
I am not a big fan of camelCase (aka camelBack, sulkingCamelCase). Compare with BiCapitalization (aka InterCaps), sTuDLyCaPs, etc., at the article on Wikipedia. I'd prefer to separate words by underscores (wide case). It looks better to readers (the underscore is the space-which-is-not-a-space) and can be enforced syntactically. Compare [a-z][a-zA-Z]* with [a-z]+(_[A-Z][a-z]*)*. Capitalization can then be reserved for distinguishing larger syntactic namespaces. Also, underscores work better with occasional acronyms, like ICMB_missile. But that is not the true way in Java.
/*
This
comment
wastes a lot of vertical space.
*/
i = i + 1; // Add one to i
Answer the question why you are doing it.
i = i + 1; // Prepare for next record in list
// Loop comment
for (;;) {
}
Not
// Loop comment
for (;;) {
}
while (i<dataCount&&data[i]<target) {
with
while (i<dataCount && data[i]<target) {
private boolean hasExcess () { return dataCount>MAXIMUM; }
private boolean hasShortage () { return dataCount<MINIMUM; }
with
private boolean hasExcess () { return dataCount>MAXIMUM; }
private boolean hasShortage () { return dataCount<MINIMUM; }
while (k<=j) {
statements;
}
if (...) {
statements;
} else if (...) {
statements;
} else {
statements;
}
for statement should have the following form:
for (initialization; condition; update) {
statements;
}
for statement with an empty body should be written:
for (initialization; condition; update) /* DO NOTHING! */ ;
try {
statements;
} catch (ExceptionOneClass e) {
statements;
} catch (ExceptionTwoClass e) {
statements;
} finally {
statements;
}
switch statement should have the following form:
switch (condition) {
case ABC:
statements;
/* FALLS THROUGH! */
case DEF:
statements;
break;
case XYZ:
statements;
break;
default:
statements;
break;
}
for (int space=0; space<indent; space++) {
System.out.print(" ");
}
Get in the habit of doing this and you will have less trouble modifying
your own code.
Experienced programers may be forgiven for occassionally
breaking this rule in situtations where the entire
construct fits comfortably on one line.
for (int space=0; space<indent; space++) System.out.print(" ");
In mathematical text always break mathematical formulas before a binary operator when the formula is displayed. (When the formula is in the text of the paragraph, then break after the operator.) Thus spoke Knuth.
for (int i=0; i<a.length; i++) {
statements
}
int x, y[]; // Avoid; use two declarations
// counter-clockwise is a left turn
private final boolean leftTurn (Point p) {
assert stack.size()>1; // must be at least two points on the stack
return Triangle.isCCW ((Point)stack.get(1), (Point)stack.get(0), p);
}
// No place for file, so create new disk final Disc d = new Disc (discs.size()+1); assert (length<=discCapacity); // assume any 1 file can always fit on empty disc d.add (length,name); discs.add (d);