/** * <code>ClassName</code> description ... * <p> * @author author * @version version, date */asdf
class ClassNameMake sure that there are no blank lines of other comments between the javadoc comment and the class code.
The comments in the heading should describe what the program does rather than how the code does it (you can read the code to see how it is done).
Any description of a method or comments about it should be in the Javadoc style and should be placed above the first line of the method.
Comments and blank lines that indicate the major sections of the subprogram are useful.
If (and only if) the meaning of a statement or group of statements is not clear from reading the code, brief comments may be included in the body of the program. In general these comments are not useful. If they merely state what is obvious from reading the code, they detract. If these comments are necessary, place them at the beginning of the block of code. This will reduce their disruption to the structure of the program. These embedded comments should generally use the double-slash (//) comment.
Variable and class names should be chosen to help describe their meaning. (a poorly chosen name that misleads the reader is worse than a nondescript name like x or a).
Variable names should be written in mixed case, but start with a lower-case letter. Even though Java regards '$' and '_' as letters, don't use them. For multiple word names begin the embedded words with an upper case letter, for example: taxRate, numberOfCars, etc.
Class names begin with an upper case letter. Method names begin with a lower case letter. We will not use prefixes to variables and methods to indicate data types as in the Visual Basic standard.
I have written a template which follows these guidelines: ClassTemplate.javaWithin a method the local variables should be listed above the executable statements.
All float and double constants must have a decimal point with a digit on each side: 1.0, not 1.; 0.3, not .3.
Use double (instead of float) and int for all calculations. The only exception would be if memory was severely limited.
When possible, avoid mixed mode in arithmetic expressions. When possible, avoid mixed mode assignments.
Logical expressions that have more than one logical operator should have parentheses to indicate the operator priority.
Each variable should be declared on a separate line.
Each statement should be on a separate line.
Unnecessary code should be removed. This is particularly true when the code is inside a loop that will be executed numerous times. It is not necessary to assign the value zero to a variable or array element before it is assigned a value by another statement. Not sure about this one.
Access modifiers should have the following order:
private|/*package*/| protected|public static abstract synchonized volatile final native
R2. The while and do while constructs are called indefinite repetition because the number of times the loop is executed depends on calculations within the loop. Definite repetition is where the number of times through the loop can be known before the loop begins execution. The for construct in Java is very general and can be used for indefinite repetition; however, the standard limits the use of the for construct to definite repetition. When possible, use a definite repetition (that is, a for) rather than an indefinite construct.
R3. In choosing between the while and do while constructs, if the loop must be executed at least once then the do while is the appropriate choice because its selection clearly shows that the loop will be executed at least once. The while loop is thus used exclusively in situations where the loop may not be executed at all depending on the value of the logical expression.
R4. In for loops, the scope of the indexing variable must be only the loop itself:
for (int i = 0; i < n; i++) {
// loop code here . . .
}
Notice that the for is definite iteration because we can see that
the loop will be executed n times.
R5. The bracing for loops should be as above, with the left brace on the same line as the for statement and the closing brace on its own line, vertically aligned with the f.
Braces are used to delimit class definitions, method definitions, and statement blocks in loops and if statements. While there are several good styles of bracing, for consistency we will adopt the following convention:
The opening brace of a block is on the same line as the defining command, separated by a space. The closing brace is on its own line, vertically aligned with the statement that began it.
I2. Braces shall be used in all loops and if constructs. Unlike Java, the standard does not allow the braces to be omitted if the code has only one statement.
public class Hello {
// Code for the class Hello
}
Method definition:
public int rating() {
// Code for the method rating()
}
For loop (while loop is similar):
for (int i = 0; i < n; i++) {
// code in the for loop
}
Do while loop:
do {
// code in the do while loop
} while (condition)
If conditional:
if (test) {
// code if test evaluates true
}
If-else conditional:
if (test) {
// code if test evaluates true
}
else {
// code if test evaluates false
}
Nested loops follow the same pattern, as in the following example:
while (condition) {
// code
if (test) {
// code
}
else {
// code
}
// code
}