Number Per Line
int level; // indentation levelis prepared over
int size; // size of table
int level, size;Do not put different type on the same line. Example:
int foo, fooarray[]; //WRONGNote: The example above use one space between the type and the identifier. Another acceptable alternative is to use tabs, example:
int level; // indentation levelInitialization
int size; // size of table
Object currentEntry; // currently selected table entry
Try to initialize local variable where they are declared. The only reason to initialize a variable where it’s declared is if the initial value depends on some computation occurring first.
Placement
Put declaration only at beginning of blocks.(A block is any code surround by curly braces “{” and “}” ) Don’t wait to declare variables until their first use, it can confuse the unwary programmer and hamper code portability within the scope.
void methodName() {The one exception to the rule is indexes of for loops, which in Java can be declared in the for statement:
int var1 = 0; //beginning of method block
if (condition) {
int var2 = 0; //beginning of “if” block
….
}
}
for(int i = 0; i < maxLoops; i++) { … }Avoid local declarations that hides declarations at higher levels. For example, do not declare the same variable name at inner block.
int count;Class and Interface Declaration
….
myMethod() {
if (condition) {
int count; //AVOID
}
}
When coding Java classes and interfaces, the following formatting rules should be followed:
- No space between method name and parenthesis “(” starting its parameter list
- Open brace “{” appears at the end of same line as the declaration statement
- Closing brace “}” starts a line by itself indented to match its corresponding opening statement, except when it is null statement the “}” should appear immediately after the “{”
class Example extends Object {5.Method are separated by a blank line
int var1;
int var2;
Example(int i, int j){
var1 = i;
var2 = j;
}
int emptyMethod() {}
….
}
0 comments:
Post a Comment