Number Per Line
1.
int
level;
// indentation level
2.
3.
int
size;
// size of table
1.
int
level, size;
1.
int
foo, fooarray[];
//WRONG
1.
int
level;
// indentation level
2.
3.
int
size;
// size of table
4.
5.
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.
01.
void
methodName() {
02.
03.
int
var1 =
0
;
//beginning of method block
04.
05.
if
(condition) {
06.
07.
int
var2 =
0
;
//beginning of “if” block
08.
09.
….
10.
11.
}
12.
13.
}
1.
for
(
int
i =
0
; i < maxLoops; i++) { … }
01.
int
count;
02.
03.
….
04.
05.
myMethod() {
06.
07.
if
(condition) {
08.
09.
int
count;
//AVOID
10.
11.
}
12.
13.
}
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 “{”
01.
class
Example
extends
Object {
02.
03.
int
var1;
04.
05.
int
var2;
06.
07.
Example(
int
i,
int
j){
08.
09.
var1 = i;
10.
11.
var2 = j;
12.
13.
}
14.
15.
16.
17.
int
emptyMethod() {}
18.
19.
….
20.
21.
}
0 comments:
Post a Comment