Page 795 - Introduction to Programming with Java: A Problem Solving Approach
P. 795

                Appendix 5 Java Coding-Style Conventions 761 public double getSum(float table[][], int rows, int cols)
{
}
// end getSum
double sum = 0.0;
for (int row=0; row<rows; row++)
{
for (int col=0; col<cols; col++)
{
sum += table[row][col];
} // end for col
} // end for row
return sum;
Variable Declarations
1. Normally, you should declare only one variable per line. For example:
float avgScore;
int numOfStudents;
// average score on the test
// number of students in the class
Exception:
If several variables are intimately related, it is acceptable to declare them together on one line. For example: Apago PDF Enhancer
int x, y, z; // coordinates for a point
2. Normally, you should include a comment for each variable declaration line.
Exception:
Don’t include a comment for names that are obvious (i.e., studentId) or standard (i.e., i for a for loop index variable, ch for a character variable).
Braces That Surround One Statement
1. For if, else, for, while, and do constructs that execute only one statement, it’s good practice to treat that one statement as though it were a compound statement and enclose it in braces, like this:
for (int i=0; i<scores.length; i++)
{
}
sumOfSquares += scores[i] * scores[i];
2. Exception:
If it would be illogical to add another statement to the construct at a later time, you may omit the curly braces when the omission improves readability. For example, this is acceptable for an experienced programmer:
for (; num>=2; num--)
factorial *= num;
































































   793   794   795   796   797