[prev][next]

6- Declarations

6.1 Number Per Line one declaration per line is recommended since it encourages commenting, in other words, int level; // indentation level int size; // size of table is preferred over int level, size; Do not put indifferent types on the same line. example: int foo, fooarray[]; //wrong! Note: The example above use one space between the type and the identifier. Another acceptable alternative is to use tabs, e.g: int level; // indentation level int size; // size of table object currentEntry; //currently selected table entry 6.2 Initialization Try to initialize local variables where theyre declared. the only reason not to initialize a variable where its declared if the initial value depends on some computation occuring first. 6.3 Placement Put declarations only at the beginning of blocks. (A block is any code surrounded by curly braces "{and"}",) dont wait to declare variables until their first use; it can confuse the unwary programmer and hamper code portability within the scope. void mymethod() { int int1 = 0; //beginning of method block if (condition) { int int2 = 0; //beginnning of "if" block ... } } The one exception to the rule is indexes of for loops, which in java can be declared in the for statement: for (int i = 0; i < maxloops; i++) {...} Avoid loval declarations that hide declarations at higher levels. For example, do not declare the same variable in an inner block: int count; ... mymethod() { if (condition) { int count; //avoid! ... } ... } 6.4 Class and Interface Declarations When coding java classes and interfaces, the following formatting rules should be followed:
  • No space between a method name and the parenthesis"("starting its parameter list
  • open brace "{" appears at the end of the same line as the declaration statement
  • closing brace "}" starts a line by itself indented to match its corresponding opening statement,except when it is a null statement the "}" shouldl appear immendiatelyn after the "{"
  • class sample extends object { int ivar1; int ivar2; sample (int i, int j) { ivar1=i; ivar2=j; } int emptymethod() {} ... }
    7- Statements 7.1 Simple Statements Each line should contain at most one statement, example: argv++; //correct argc++; //correct argv++; argc--; //avoid! 7.2 Compound Statements Compound statements are statements that contain list of statements enclosed in braces "{statements}". SEe the following sections for examples.
  • The enclosed statements should be indented one more level than the compound statement.
  • The opening brace should be at the end of the line that begins the compound statement. closing brace should begin a a line and be indented to the beginning of the compound statement.
  • Brace are used around all statements, even single statements, when they are part of a control structure, such as a if-else or for statement. this makes it easier to add statements without accidentally introducing bugs due to forgetting to add braces.
  • 7.3 Return Statements A return statement with a value should not use parenthesis unless they make the return value more obvious in some way. Example: return; return mydisk.size(); return (size ? size : defaultsize); 7.4 if,if-else,if else-if statements the if-else class of statements should have the following form: if (condition) { statements; } if (condition) { statements; } else { statements;; } if (condition) { statements; } else { statements; } if (condition) { statements; } else if (condition) { statements; } else { statements; } Note:if statements always use braces {}. AVoid the following error-prone form: if (condition) //avoid! this omits the braces {}! statements; 7.3 for statements A for statement should have the following form: for (initialization; condition; update){ statements; } an empty for statement (one in which all the work is done in the initialization, condition,a dn update clauses) should have the following form: for (initialization; condition; update); When using the comma operator in the initialization or update clause of a for statement, avoid the complexity of using more than three variables. if needed, use separate statements before the for loop (for initialization clause) or a t the end of the loop(for the update clause). 7.6 while statements A while statement should have the following form: while (condition) { statements; } an empty while statement should have the following form: while (condition); 7.7 do-while statements A do-while statements should have the following form: do { statements; }while(condition); 7.8 switch statements A switch statement should have the following form: switch (condition) { case ABC: /*falls through */ case DEF: statements; break; case XYZ: statements; break; default: statements; break; } every time a case falls through (doesnt include a break statement), add a comment where the break statement would normally be. this is shown in the preceding code example with the /*falls through*/ comment. every switch statements should includes a default case. the break in the default case is redundant, but it prevents a fall-through error if later another case is added. 7.9 try-catch statements A try-catch statement should have the following format: try { statements } catch (exceptionclass e) { statements; } A try-catch statement may also be followed by finally, which executes regardless of whether or not the tryblock completed successfully. try { statements; } catch (exceptionclass e) { statements; } finally { statements; }