Programming in Java consists of writing source code, but the conventions used vary widely. For the most part, Sun has done a better job of educating its developer community about the guts of Java and even which code style to use than most vendors have in the past. For example, Microsoft has always had a strong developer community because it has treated them well. Visual Basic created a buzz when it first came out. However, Microsoft wasn't clear about code style in those days and still could do better. Sun can't compete with Windows developer tool GUIs, but it is much better about supplying code convention instructions. The following example shows the two most common brace conventions. Most of the code in the SDK, Sun's documentation, and the Web site follow the second convention. I prefer the first one, so that is how you will find code in this book. 1] if (x < y) Good: Easy to match braces. { Bad: Dilutes lines of code measurement. x = y; Number of lines is a scarce y = 0; resource on the monitor. } 2] if (x < y){ Good: No wasted lines. x = y; Bad: Hard to match braces. y = 0; } StatementsStatements are the equivalent of sentences. It is hard to make a mistake with them, but I've seen it done. Be consistent with the style you choose. Simple StatementsThe following are examples of simple statements: /** * Constant for the maximum height of flight possible. */ public static final int MAXIMUM_FLIGHT_HEIGHT = 50000; char ch = seq.charAt(i); Compound StatementsStatements that combine more than one method call or more than one assignment or condition test are compound statements. The following is an example: int c = ASCII.toLower(seq.charAt(i+j)); return StatementsThe two most important ideas to keep in mind when you write your return statements are to avoid using more than one in each method and to avoid making assignments in the return statement. Some developers think you should minimize expressions and simply pass a variable that was assigned before the return statement. The following example shows a complex return statement: return ((ch-lower)(upper-ch)) < 0 && next.match(matcher, i+1, seq); if, if-else , and if else-if else Statements (Conditional)The if statement can vary from simple to very complex, with nested if and loop statements inside it. Sometimes the nesting is several levels deep, so using good indents, braces, and comments is important. You might want to comment at the closing brace if it ends especially complicated code. The following are a few snippets from java.util.regex.Pattern to demonstrate how Sun uses common Java statements and blocks including for , while , and try in the J2SE 1.4 SDK: if (not) { return new NotSingle(ch); }else { return new Single(ch); } if (!matchLimited matchList.size() < limit - 1) { String match = input.subSequence(index, m.start()).toString(); matchList.add(match); index = m.end(); } else if (matchList.size() == limit - 1) { // last one String match = input.subSequence(index, input.length()).toString(); matchList.add(match); index = m.end(); } if (matchRoot instanceof Slice) { root = BnM.optimize(matchRoot); if (root == matchRoot) { root = new Start(matchRoot); } } else if (matchRoot instanceof Begin matchRoot instanceof First) { root = matchRoot; } else { root = new Start(matchRoot); } for StatementsThe following two examples of the for statement are easy to read: NEXT: for (i = patternLength; i > 0; i--) { // j is the beginning index of suffix being considered for (j = patternLength - 1; j >= i; j--) { // Testing for good suffix if (src[j] == src[j-i]) { // src[j..len] is a good suffix optoSft[j-1] = i; } else { // No match. The array has already been // filled up with correct values before. continue NEXT; } } } for (int index=0; index<groupSize; index++) { char c1 = seq.charAt(i+index); char c2 = seq.charAt(j+index); if (c1 != c2) { c1 = Character.toUpperCase(c1); c2 = Character.toUpperCase(c2); if (c1 != c2) { c1 = Character.toLowerCase(c1); c2 = Character.toLowerCase(c2); if (c1 != c2) { return false; } } } } //another example of a quick for loop for (int j = 0; j < len; j++) { if (buf[j] != seq.charAt(i+j)) { return false; } } while StatementsThe following examples show easy-to-read while statements: while(read() != '}') { //do something } while(Character.getType(c) == Character.NON_SPACING_MARK) { i++; if (i >= patternLength) { break; } c = normalizedPattern.charAt(i); sequenceBuffer.append(c); } do-while StatementsThe following is an example of how you should format your do-while statements: do { Object current = iterator.next(); if (current.equals(target)) { break; } else { tests++; } } while (iterator.hasNext()); switch StatementsThe following two switch statements illustrate proper style: switch(ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': int newRefNum = (refNum * 10) + (ch - '0'); // Add another number if it doesn't make a group // that doesn't exist if (groupCount - 1 < newRefNum) { done = true; break; } refNum = newRefNum; read(); break; default: done = true; break; } int ch = peek(); for (;;) { switch (ch) { case 'i': flags &= ~CASE_INSENSITIVE; break; case 'm': flags &= ~MULTILINE; break; case 's': flags &= ~DOTALL; break; case 'd': flags &= ~UNIX_LINES; break; case 'u': flags &= ~UNICODE_CASE; break; case 'c': flags &= ~CANON_EQ; break; case 'x': flags &= ~COMMENTS; break; default: return; } ch = next(); } try-catch-finally StatementsThe following is a try-catch-finally construct that is easy to read: try { statements; } catch (ExceptionOneClass e) { statements; } catch (ExceptionTwoClass e) { statements; } finally { statements; } |