20 | | '''Arguments/Parameters''' : There shall be a single space character between the method name (or logical controller i.e. "for", "if", "while"), and the opening parenthesis "(", and additional white space between each argument. There shall not be white-space between the argument and its successive separator ("," or ";" etc...) nor shall there be white space between the enclosing parenthesis and its arguments. For example: |
| 20 | '''Arguments/Parameters''' : |
| 21 | * If a method takes more arguments than can fit on the opening line, wrap the line starting at the first argument. |
| 22 | * If there are many arguments, consider putting each one on it's own line. |
35 | | Bad |
36 | | {{{ |
37 | | public void foo( String str , int k ) { |
38 | | for(int i=0;i<10;++i){ |
39 | | System.out.println(i); |
40 | | } |
41 | | int j = 10; |
42 | | do{ |
43 | | System.out.println(j--); |
44 | | }while(j>0); |
45 | | } |
46 | | }}} |
47 | | |
48 | | '''Scoping Method/Member Modifiers''' : All class members and methods should be fully scoped as applicable. That is, make use of "public"/"protected"/"private", "static", "final" etc... These should go in the order of (for example), {{{public static final String CONST_MEMBER = "Foo";}}}. |
49 | | |
50 | | == Coding for Efficiency == |
51 | | |
52 | | '''Pre- vs. Post- Increment/Decrement''' : When using the pre-/post- increment/decrement operators, one should favor the "pre" form for byte-code efficiency unless the return value is being utilized. Most modern compilers should perform this optimization for you, but why be lazy? Example: |
53 | | |
54 | | Good |
55 | | {{{ |
56 | | for (int i = 0; i < 10; ++i) { |
57 | | ... |
58 | | } |
59 | | int j = 0; |
60 | | while (j < 10) { |
61 | | int previous = j++; |
62 | | // ... Work with "previous" and "j" ... |
63 | | // Where j = previous + 1 |
64 | | } |
65 | | }}} |
66 | | |
67 | | Bad |
68 | | {{{ |
69 | | for (int i = 0; i < 10; i++) { |
70 | | ... |
71 | | } |
72 | | }}} |
73 | | |
74 | | '''Incremental Assignment''' : Use it. These are the "+=", "-=", "*=", "/=" operators. They can be used on any numerical primitive, and the "+=" can be used on the java.lang.String as well. These have been optimized for efficiency. Example: |
75 | | |
76 | | Good |
77 | | {{{ |
78 | | int i = 10; |
79 | | i += 20; // "i" is now 30. |
80 | | }}} |
81 | | |
82 | | Bad |
83 | | {{{ |
84 | | int i = 10; |
85 | | i = i + 20; // "i" is now 30. |
86 | | }}} |
| 28 | '''Java1.5 Enhancements''' : Use features introduced in Java 5 for more readable, less error-prone code. |
| 29 | * [http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html Generics] |
| 30 | * [http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html Enums] |
| 31 | * [http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html Enhanced For-Loop] |