Style » History » Version 4
Paula Gearon, 01/14/2008 04:08 AM
1 | 1 | Paula Gearon | |
---|---|---|---|
2 | 4 | Paula Gearon | h1. Style Guide |
3 | |||
4 | |||
5 | 1 | Paula Gearon | Coding style in Mulgara has always been intentionally liberal, in order to not stifle the creative process. |
6 | |||
7 | The original style document was developed when the code was being developed commercially, and is no longer available. This guide was built from a consensus of the developers of the day, and hence reflects the preferences of that particular group, who were all co-located together. Mulgara is now a scattered group, with few of the original developers. As such, it is appropriate to rewrite and update the coding standards. |
||
8 | |||
9 | Note that the only compulsory rules are the ones with MUST or MUST NOT. We strongly advise following rules of SHOULD and SHOULD NOT. |
||
10 | |||
11 | |||
12 | 4 | Paula Gearon | h1. General Principles |
13 | 1 | Paula Gearon | |
14 | |||
15 | 4 | Paula Gearon | * In order to facilitate rapid and enjoyable work, each developer should be free to work with a coding style comfortable to them. If an individual has an issue with this style guide, they should bring it to the attention of the group, so that a consensus may be achieved. |
16 | * With many individuals practicing their own styles, there are bound to be style conflicts in individual files. In this case the following rules should be followed: |
||
17 | ** If you are writing a new file, then you choose the style. |
||
18 | ** If you are modifying another's file, then stick to the style of the original file. The exceptions are: |
||
19 | *** You are modifying a significant portion of the file (> 10-20%). |
||
20 | *** The file is not being actively maintained, and it violates this style guide. |
||
21 | |||
22 | |||
23 | h1. Spacing |
||
24 | |||
25 | |||
26 | * All code MUST be written with an indenting of 2 spaces per indent. This allows significant nesting without excessive line wrapping. Look at some of our nested classes and you'll see why we want this rule! |
||
27 | * Each new code block (i.e. code which may be wrapped in {}, except array initializers) MUST have 1 indent. Other indenting (such as method parameters or line breaks) MUST be greater than 1 indent. |
||
28 | * Tab characters MUST NOT be used and MUST be replaced with 2 spaces. |
||
29 | * Spaces SHOULD NOT be consecutive, unless immediately following a newline. |
||
30 | * We are not writing COBOL. You SHOULD NOT align the = character in declarations (this is an explicit example of the rule of consecutive spaces). e.g. You SHOULD NOT write the following: |
||
31 | <pre> |
||
32 | <code class="java"> |
||
33 | 1 | Paula Gearon | int a = 1; |
34 | long b = 2L; |
||
35 | final String c = "hello"; |
||
36 | 4 | Paula Gearon | </code></pre> |
37 | 1 | Paula Gearon | Instead use: |
38 | 4 | Paula Gearon | <pre> |
39 | <code class="java"> |
||
40 | 1 | Paula Gearon | int a = 1; |
41 | long b = 2L; |
||
42 | final String c = "hello"; |
||
43 | 4 | Paula Gearon | </code></pre> |
44 | * All commas SHOULD be followed with a single space. |
||
45 | * Casts SHOULD NOT be followed with a space. |
||
46 | * Parentheses () and brackets [] SHOULD NOT have spaces inside of them, except after commas or after a newline. The following are acceptable: |
||
47 | <pre> |
||
48 | <code class="java"> |
||
49 | 1 | Paula Gearon | int[] a = new int[] {1, 2, 3, 4}; |
50 | String b = new String[] { |
||
51 | "Hello", |
||
52 | "there", |
||
53 | "world" |
||
54 | }; |
||
55 | 4 | Paula Gearon | </code></pre> |
56 | * Keywords SHOULD be followed by a space. |
||
57 | * Method calls MUST NOT be followed by a space. |
||
58 | * The . operator MUST NOT be preceded by or followed by spaces, unless splitting a line at that point. An example of acceptable line splitting is: |
||
59 | <pre> |
||
60 | <code class="java"> |
||
61 | 1 | Paula Gearon | int result = foo().bar().baz() |
62 | .fizz().fuzz(); |
||
63 | 4 | Paula Gearon | </code></pre> |
64 | * All other binary operators SHOULD have single spaces on either side. This includes: |
||
65 | <pre> |
||
66 | 1 | Paula Gearon | = == | || && & ^ + - * / % |
67 | 4 | Paula Gearon | </code></pre> |
68 | * The elements of the ternary operator should also have surrounding spaces. e.g.: |
||
69 | <pre> |
||
70 | <code class="java"> |
||
71 | 1 | Paula Gearon | return a ? b : c; |
72 | 4 | Paula Gearon | </code></pre> |
73 | 1 | Paula Gearon | |
74 | |||
75 | 4 | Paula Gearon | h1. Vertical Spacing |
76 | |||
77 | |||
78 | * There is no fixed point for line wrapping. There are no developers using 80 column terminals, therefor you SHOULD NOT wrap text at, or near, this mark. Conversely, excessive width can also be confusing, so use your judgment here. |
||
79 | * Braces have one accepted format: |
||
80 | 1 | Paula Gearon | Opening braces on the same line as the code commencing the block. Closing braces are either: |
81 | 4 | Paula Gearon | **** When the current structure has finished, the brace is on a line on its own. |
82 | **** When followed by more control structure elements, then those elements are on the same line as the closing brace (example elements are _else_ and _catch_). |
||
83 | 1 | Paula Gearon | In the past, there was another acceptable style. However, the current developers have now all agreed to the above style. The previously accepted style had opening and closing braces always on a line on their own. |
84 | 4 | Paula Gearon | * A element that continues the current structure MUST NOT be placed on a new line. The following is not allowed: |
85 | <pre> |
||
86 | <code class="java"> |
||
87 | 1 | Paula Gearon | try { |
88 | // some code |
||
89 | } |
||
90 | catch (Exception) { |
||
91 | } |
||
92 | 4 | Paula Gearon | </code></pre> |
93 | 1 | Paula Gearon | Instead use the following: |
94 | 4 | Paula Gearon | <pre> |
95 | <code class="java"> |
||
96 | 1 | Paula Gearon | try { |
97 | // some code |
||
98 | } catch (Exception) { |
||
99 | } |
||
100 | 4 | Paula Gearon | </code></pre> |
101 | 1 | Paula Gearon | Starting blocks in a continued control structure like this can make a code structure appear to have completed when it will actually continue. |
102 | |||
103 | 4 | Paula Gearon | _ *NOTE:* During 2005 someone reformatted many of the source files to violate this. If you are modifying a file that violates this formatting, please consider fixing the formatting as well._ |
104 | * Related blocks (if/else and try/catch) MUST NOT be separated by any more newlines than allowable in the chosen style. This means 0 newlines for style 1, and 1 newline for style 2. You MUST NOT do the following: |
||
105 | <pre> |
||
106 | <code class="java"> |
||
107 | 1 | Paula Gearon | if (test) { |
108 | // code |
||
109 | } |
||
110 | // a comment about the else block |
||
111 | else { |
||
112 | } |
||
113 | 4 | Paula Gearon | </code></pre> |
114 | 1 | Paula Gearon | This is an exacerbation of the control structure continuation problem, and is more likely to make a control structure appear to have completed. |
115 | 4 | Paula Gearon | * Method bodies MUST have at least one newline between them Method bodies SHOULD be separated by 2 newlines. |
116 | * You SHOULD separate logical blocks of code in a single method with a newline. |
||
117 | * Single line code blocks MUST be wrapped in braces if they are on a separate line to the start of the structure. e.g. The braces in the following MUST be used, and are compulsory: |
||
118 | <pre> |
||
119 | <code class="java"> |
||
120 | 1 | Paula Gearon | if (test) { |
121 | foo(); |
||
122 | } |
||
123 | 4 | Paula Gearon | </code></pre> |
124 | * Single line code blocks MAY appear on the same line as the start of the structure. e.g. The following is permissible: |
||
125 | <pre> |
||
126 | <code class="java"> |
||
127 | 1 | Paula Gearon | if (!test) throw new Exception(); |
128 | 4 | Paula Gearon | </code></pre> |
129 | *Note*: This was not allowed in the previous code style. |
||
130 | * No-op blocks MAY place the closing brace on the same line as the opening brace, violating the usual brace style. A comment SHOULD appear with this idiom. e.g.: |
||
131 | <pre> |
||
132 | <code class="java"> |
||
133 | 1 | Paula Gearon | try { |
134 | file.close(); |
||
135 | } catch (IOException e) { /* don't care */ } |
||
136 | 4 | Paula Gearon | </code></pre> |
137 | * Trivial accessor methods MAY violate the code style by placing all code and braces for the method on a single line, and avoiding line breaks between these methods. This SHOULD NOT occur unless the class is very small and contains almost entirely trivial methods like this. Line breaks MUST NOT be skipped exception between single-line methods. e.g.: |
||
138 | <pre> |
||
139 | <code class="java"> |
||
140 | 1 | Paula Gearon | int getX() { return x; } |
141 | void setX(int x) { this.x = x; } |
||
142 | 4 | Paula Gearon | </code></pre> |
143 | 1 | Paula Gearon | Note that the last four items were not allowable under the previous style guide. They've been included to permit some useful compression of syntactic salt. |
144 | |||
145 | |||
146 | 4 | Paula Gearon | h1. Comments |
147 | |||
148 | |||
149 | * Classes, methods and members MUST have Javadoc comments. This is our primary documentation for developers, so it is important, even for private methods and members. |
||
150 | * Trivial classes for holding internal values MAY avoid the use of Javadoc on the methods and members, but not on the class. e.g.: |
||
151 | <pre> |
||
152 | <code class="java"> |
||
153 | 1 | Paula Gearon | /** Represents a Cartesian coordinate. */ |
154 | private class Point { |
||
155 | private double x; |
||
156 | private double y; |
||
157 | |||
158 | } |
||
159 | 4 | Paula Gearon | </code></pre> |
160 | * Javadoc for methods MUST describe what the method does. The Javadoc SHOULD describe the usage and purpose of classes, members and methods. |
||
161 | * Good Javadoc on a method can eliminate the need for internal comments. However, anything that is not immediately clear to an experienced programmer SHOULD have an explanatory comment. |
||
162 | * All new code files MUST start with the following file header: |
||
163 | <pre> |
||
164 | <code class="java"> |
||
165 | 1 | Paula Gearon | /* |
166 | 4 | Paula Gearon | *** The contents of this file are subject to the Open Software License |
167 | *** Version 3.0 (the "License"); you may not use this file except in |
||
168 | *** compliance with the License. You may obtain a copy of the License at |
||
169 | *** http://www.opensource.org/licenses/osl-3.0.txt |
||
170 | 1 | Paula Gearon | * |
171 | 4 | Paula Gearon | *** Software distributed under the License is distributed on an "AS IS" |
172 | *** basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
||
173 | *** the License for the specific language governing rights and limitations |
||
174 | *** under the License. |
||
175 | 1 | Paula Gearon | */ |
176 | 4 | Paula Gearon | </code></pre> |
177 | * Javadoc for the main class in a file MUST include the following tags: |
||
178 | <pre> |
||
179 | <code class="java"> |
||
180 | 1 | Paula Gearon | @created |
181 | @author |
||
182 | @copyright |
||
183 | @licence |
||
184 | 4 | Paula Gearon | </code></pre> |
185 | 1 | Paula Gearon | An example is: |
186 | 4 | Paula Gearon | <pre> |
187 | <code class="java"> |
||
188 | 1 | Paula Gearon | /* |
189 | 4 | Paula Gearon | *** @created 2007-03-23 |
190 | *** @author Paul Gearon |
||
191 | *** @copyright © 2007 <a href="mailto:pgearon@users.sourceforge.net">Paul Gearon</a> |
||
192 | *** @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a> |
||
193 | 1 | Paula Gearon | */ |
194 | 4 | Paula Gearon | </code></pre> |
195 | The licence MUST be the "OSL":http://www.opensource.org/licenses/osl-3.0.php, unless it is based on source from elsewhere, where that source is compatible with the OSL. Compatibility is the developer's responsibility to check, and it MUST be checked carefully. Note that GPL and LGPL code is NOT compatible here! |
||
196 | 1 | Paula Gearon | |
197 | 4 | Paula Gearon | In the future, it may be desirable to take portions of any new code and use them in other projects. For this reason, dual licensing using the "Apache License":http://www.opensource.org/licenses/apache2.0.php is encouraged. |
198 | 1 | Paula Gearon | |
199 | |||
200 | 4 | Paula Gearon | h1. Code |
201 | |||
202 | |||
203 | * Imports SHOULD be listed for each required class. The exception to this is for packages in the Java library where a large number of classes is needed. e.g. If using URLs along with a large number of the collection types, then the following is appropriate: |
||
204 | <pre> |
||
205 | <code class="java"> |
||
206 | 1 | Paula Gearon | import java.net.URL; |
207 | import java.net.MalformedURLException; |
||
208 | import java.util.*; |
||
209 | 4 | Paula Gearon | </code></pre> |
210 | * Imports SHOULD be in 3 groups. Groups are separated by a blank line, and may also include a comment describing the group. The groups are: |
||
211 | ** java.* packages. |
||
212 | ** Mulgara packages. |
||
213 | ** Third party packages. |
||
214 | * Compiler warnings SHOULD be avoided. If this cannot be avoided in the code, then use annotations. |
||
215 | * Declarations of a narrowed generic type SHOULD NOT have any extra spaces around or in the type specification. So the following SHOULD NOT be used: |
||
216 | <pre> |
||
217 | <code class="java"> |
||
218 | Map <Integer, String> map = new [[HashMap]] <Integer, String> (); |
||
219 | </code></pre> |
||
220 | 1 | Paula Gearon | Instead, the following style SHOULD be followed: |
221 | 4 | Paula Gearon | <pre> |
222 | <code class="java"> |
||
223 | Map<Integer,String> map = new [[HashMap]]<Integer,String>(); |
||
224 | </code></pre> |
||
225 | 1 | Paula Gearon | Note that this is the only case where commas SHOULD NOT be followed by spaces. |