1 | Strangecode coding standards |
---|
2 | 29 Aug 2005 20:12:49 |
---|
3 | |
---|
4 | |
---|
5 | ====================================================================== |
---|
6 | Preamble |
---|
7 | ====================================================================== |
---|
8 | |
---|
9 | Our standards follow closely the PEAR coding standards. |
---|
10 | |
---|
11 | Essential reading: |
---|
12 | http://pear.php.net/manual/en/standards.php |
---|
13 | |
---|
14 | |
---|
15 | ===================================================================== |
---|
16 | General guidelines |
---|
17 | ===================================================================== |
---|
18 | |
---|
19 | - Respect these coding styles. |
---|
20 | - Don't add npm dependencies if you can avoid it. If a new dependency is unavoidable, be mindful of its size, freshness and added value. |
---|
21 | - Use Svelte for all UI needs. |
---|
22 | - Try to not shoehorn your code into existing functions or components. |
---|
23 | - Simple is better. OOP is not inevitable. Simple functions often work as well, if not better. |
---|
24 | - If you must use OOP, avoid inheritance as much as possible, no one likes digging several layers of abstraction. |
---|
25 | - Comment the code. What, why, how, just make your intent clear. |
---|
26 | |
---|
27 | |
---|
28 | ====================================================================== |
---|
29 | File Naming Conventions |
---|
30 | ====================================================================== |
---|
31 | |
---|
32 | script.php Public accessible scripts. |
---|
33 | |
---|
34 | Auth_SQL.inc.php One PHP Class to be included. The filename is the |
---|
35 | type of class, underscore, name. Or if in subdirs, |
---|
36 | this could be /Auth/SQL.inc.php while the class name |
---|
37 | remains "Auth_SQL" |
---|
38 | |
---|
39 | some_file.inc.html HTML file, which may or may not have some PHP, |
---|
40 | but will be included by some other PHP file. Never |
---|
41 | includes sensitive code as these files may be accessed |
---|
42 | directly in the web root. |
---|
43 | |
---|
44 | script.cli.php A command-line executable script, possibly executed |
---|
45 | with CRON. Outputs TEXT if any instead of HTML. |
---|
46 | |
---|
47 | script.inc.eml Text file to be sent by email. |
---|
48 | |
---|
49 | schema.mysql Database schema file that goes with the application. |
---|
50 | |
---|
51 | main.screen.css CSS file with media: screen/print/all |
---|
52 | main.print.css |
---|
53 | |
---|
54 | |
---|
55 | ====================================================================== |
---|
56 | Indenting and Wrap |
---|
57 | ====================================================================== |
---|
58 | |
---|
59 | Use an indent of 4 spaces, with no tabs. Code and especially comments should |
---|
60 | usually be wrapped <= 80 characters. Exceptions are made in the case where |
---|
61 | code readability is significantly improved with longer lines. |
---|
62 | |
---|
63 | |
---|
64 | ====================================================================== |
---|
65 | Control Structures |
---|
66 | ====================================================================== |
---|
67 | |
---|
68 | These include if, for, while, switch, etc. Here is an example if statement, |
---|
69 | since it is the most complicated of them: |
---|
70 | |
---|
71 | if ((condition1) || (condition2)) { |
---|
72 | action1; |
---|
73 | } else if ((condition3) && (condition4)) { |
---|
74 | action2; |
---|
75 | } else { |
---|
76 | defaultaction; |
---|
77 | } |
---|
78 | |
---|
79 | Control statements should have one space between the control keyword |
---|
80 | and opening parenthesis, to distinguish them from function calls. |
---|
81 | |
---|
82 | Even in the case of an if with no else clauses, and only a 1 line action, |
---|
83 | the curly braces should still be used: |
---|
84 | |
---|
85 | if (condition) { |
---|
86 | action; |
---|
87 | } |
---|
88 | |
---|
89 | This improves readability and allows easy extension of the clause. |
---|
90 | |
---|
91 | For switch statements: |
---|
92 | |
---|
93 | switch (condition) { |
---|
94 | case 1: |
---|
95 | action1; |
---|
96 | break; |
---|
97 | |
---|
98 | case 2: |
---|
99 | action2; |
---|
100 | break; |
---|
101 | |
---|
102 | default: |
---|
103 | defaultaction; |
---|
104 | break; |
---|
105 | |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | ====================================================================== |
---|
110 | Function Calls |
---|
111 | ====================================================================== |
---|
112 | |
---|
113 | Functions should be called with no spaces between the function name, |
---|
114 | the opening parenthesis, and the first parameter; spaces between commas |
---|
115 | and each parameter, and no space between the last parameter, the |
---|
116 | closing parenthesis, and the semicolon. Here's an example: |
---|
117 | |
---|
118 | $var = foo($bar, $baz, $quux); |
---|
119 | |
---|
120 | As displayed above, there should be one space on either side of an |
---|
121 | equals sign used to assign the return value of a function to a |
---|
122 | variable. In the case of a block of related assignments, more space |
---|
123 | may be inserted to promote readability: |
---|
124 | |
---|
125 | $short = foo($bar); |
---|
126 | $long_variable = foo($baz); |
---|
127 | |
---|
128 | |
---|
129 | ====================================================================== |
---|
130 | Function and Method Definitions |
---|
131 | ====================================================================== |
---|
132 | |
---|
133 | Function declaractions follow the "one true brace" convention: |
---|
134 | |
---|
135 | function fooFunction($arg1, $arg2 = '') |
---|
136 | { |
---|
137 | if (condition) { |
---|
138 | statement; |
---|
139 | } |
---|
140 | return $val; |
---|
141 | } |
---|
142 | |
---|
143 | Arguments with default values go at the end of the argument list. Always |
---|
144 | attempt to return a meaningful value from a function if one is appropriate. |
---|
145 | |
---|
146 | Use lowerCamelCase for function and method names to distinguish from php |
---|
147 | internal functions which standard on underscore_space_style_names(). |
---|
148 | |
---|
149 | |
---|
150 | ====================================================================== |
---|
151 | Return Values |
---|
152 | ====================================================================== |
---|
153 | |
---|
154 | When functions return boolean values, use 'return false;' or 'return true;' |
---|
155 | as opposed to 'return 0;' or 'return 1;' or 'return(-1);', except when |
---|
156 | boolean plurality is necessary. |
---|
157 | |
---|
158 | |
---|
159 | ====================================================================== |
---|
160 | String Concatenation |
---|
161 | ====================================================================== |
---|
162 | |
---|
163 | Always include a space before and after the concatenation operator '.' which |
---|
164 | improves readability and improves search-and-replace of adjacent elements. |
---|
165 | |
---|
166 | $something = $blah . funky() . ".\".=" . $blab; |
---|
167 | |
---|
168 | is better than: |
---|
169 | |
---|
170 | $something = $blah.funky().".\".=".$blab; |
---|
171 | |
---|
172 | |
---|
173 | ====================================================================== |
---|
174 | Quote Marks |
---|
175 | ====================================================================== |
---|
176 | |
---|
177 | Use the single quote marks ' to enclose simple strings whenever possible. |
---|
178 | Double quote marks " require extra parsing and thus slow things down, but |
---|
179 | are necessary if entities there must be swapped-out such as variables or |
---|
180 | control characters. |
---|
181 | |
---|
182 | $var['singlequote'] = 'singlequote'; |
---|
183 | $var["doublequote-$i"] = "$vars and \n funny \t %s things need doublequotes"; |
---|
184 | $var['doublequote-' . $i] = $var . 'you can do this' . "\t %s" . $var2 . |
---|
185 | 'but it isn\'t any better'; |
---|
186 | |
---|
187 | |
---|
188 | ====================================================================== |
---|
189 | Printing HTML |
---|
190 | ====================================================================== |
---|
191 | |
---|
192 | For large or complex blocks of HTML, using the following method is much faster |
---|
193 | and safer than echo because the php processor is bypassed and content goes |
---|
194 | directly to output: |
---|
195 | |
---|
196 | <?php |
---|
197 | if ($something) { |
---|
198 | // Here comes multi-line HTML... |
---|
199 | ?> |
---|
200 | <div class="sc-tiny"> |
---|
201 | <a href="<?php echo $app->ohref('/contact.php') ?>">Contact us</a> |
---|
202 | </div> |
---|
203 | <?php |
---|
204 | } else if ($somethingelse) { |
---|
205 | ?><h1>Just a single-line of html</h1><?php |
---|
206 | } |
---|
207 | ?> |
---|
208 | |
---|
209 | |
---|
210 | ====================================================================== |
---|
211 | Comments |
---|
212 | ====================================================================== |
---|
213 | |
---|
214 | Function comments should follow the Javadoc standard, with detailed |
---|
215 | function comments and one-line pointers along the way: |
---|
216 | http://java.sun.com/products/jdk/javadoc/writingdoccomments/index.html |
---|
217 | |
---|
218 | Here is an example of the codebase method Email::validEmail(): |
---|
219 | |
---|
220 | <?php |
---|
221 | /** |
---|
222 | * Validates an email address based on the recommendations in RFC 3696. |
---|
223 | * Is more loose than restrictive, to allow the many valid variants of |
---|
224 | * email addresses while catching the most common mistakes. Checks an array too. |
---|
225 | * http://www.faqs.org/rfcs/rfc822.html |
---|
226 | * http://www.faqs.org/rfcs/rfc2822.html |
---|
227 | * http://www.faqs.org/rfcs/rfc3696.html |
---|
228 | * http://www.faqs.org/rfcs/rfc1035.html |
---|
229 | * |
---|
230 | * @access public |
---|
231 | * @param mixed $email Address to check, string or array. |
---|
232 | * @return bool Validity of address. |
---|
233 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
234 | * @since 30 Nov 2005 22:00:50 |
---|
235 | */ |
---|
236 | function validEmail($email) |
---|
237 | { |
---|
238 | $app =& App::getInstance(); |
---|
239 | |
---|
240 | // If an array, check values recursively. |
---|
241 | if (is_array($email)) { |
---|
242 | foreach ($email as $e) { |
---|
243 | if (!$this->validEmail($e)) { |
---|
244 | return false; |
---|
245 | } |
---|
246 | } |
---|
247 | return true; |
---|
248 | } else { |
---|
249 | // To be valid email address must match regex and fit within the length constraints. |
---|
250 | if (preg_match($this->getParam('regex'), $email, $e_parts) && mb_strlen($e_parts[2]) < 64 && mb_strlen($e_parts[3]) < 255) { |
---|
251 | return true; |
---|
252 | } else { |
---|
253 | $app->logMsg(sprintf('Invalid email: %s', $email), LOG_INFO, __FILE__, __LINE__); |
---|
254 | return false; |
---|
255 | } |
---|
256 | } |
---|
257 | } |
---|
258 | ?> |
---|
259 | |
---|
260 | Single line comments should be of the double-slash variety, and be on the line above the |
---|
261 | code being commented upon: |
---|
262 | |
---|
263 | <?php |
---|
264 | // If the last word in $var is peanuts, set $need_peanuts to TRUE. |
---|
265 | $need_peanuts = preg_match('/peanuts$/i', $var); |
---|
266 | ?> |
---|
267 | |
---|
268 | |
---|
269 | ====================================================================== |
---|
270 | Including Code |
---|
271 | ====================================================================== |
---|
272 | |
---|
273 | If you are including a class, function library, or anything else which would |
---|
274 | cause a parse error if included twice, always use include_once. This will |
---|
275 | ensure that no matter how many factory methods we use or how much dynamic |
---|
276 | inclusion we do, the library will only be included once. |
---|
277 | |
---|
278 | If you are including a static filename, such as a conf file or a template |
---|
279 | that is _always_ used, use require. |
---|
280 | |
---|
281 | If you are dynamically including a filename, or want the code to only be |
---|
282 | used conditionally (an optional template), use include. |
---|
283 | |
---|
284 | |
---|
285 | ====================================================================== |
---|
286 | PHP Code Tags |
---|
287 | ====================================================================== |
---|
288 | |
---|
289 | Always use <?php ?> to delimit PHP code, not the <? ?> shorthand. Even |
---|
290 | use <?php echo $name; ?> instead of <?=$name?>. |
---|
291 | |
---|
292 | |
---|
293 | ====================================================================== |
---|
294 | php.ini Settings |
---|
295 | ====================================================================== |
---|
296 | |
---|
297 | All code should work with register_globals = Off. This means using |
---|
298 | $_REQUEST, $_GET, $_POST, $_COOKIE, $_SESSION, $_SERVER, and $_ENV |
---|
299 | to access all request, get, post, cookie, session, server, and |
---|
300 | environment data, respectively. |
---|
301 | |
---|
302 | All code should work with error_reporting = E_ALL. Failure to do so would |
---|
303 | result in ugly output, error logs getting filled with lots of warning messages, |
---|
304 | or even downright broken scripts. |
---|
305 | |
---|
306 | All code should work regardless of the setting of magic_quotes_gpc. |
---|
307 | Form data should be passed through stripslashes() if necessary. |
---|
308 | |
---|
309 | No code should assume that '.' is in the include path. Always |
---|
310 | specify './' in front of a filename when you are including a file in |
---|
311 | the same directory. Or better yet, for less abiguity, use: |
---|
312 | include dirname(__FILE__) . '/include_me.inc.php'; |
---|
313 | |
---|
314 | |
---|
315 | ====================================================================== |
---|
316 | XHTML 1.0 Compliance |
---|
317 | ====================================================================== |
---|
318 | |
---|
319 | All HTML should be valid XHTML 1.0 verified with the |
---|
320 | W3C Markup Validation Service: http://validator.w3.org/ |
---|
321 | |
---|
322 | All tag names and parameters must be lower case including javascript |
---|
323 | event handlers: |
---|
324 | |
---|
325 | <div class="mofogo">...</div> |
---|
326 | <a href="http://www.example.com/" onmouseover="status=''" onmouseout="status=''">...</a> |
---|
327 | |
---|
328 | All tag parameters must be of a valid parameter="value" form (numeric |
---|
329 | values must also be surrounded by quotes). For parameters that had no |
---|
330 | value in HTML, the parameter name is the value. For example: |
---|
331 | |
---|
332 | <input type="checkbox" checked="checked" /> |
---|
333 | <select name="example"> |
---|
334 | <option selected="selected" value="1">Example</option> |
---|
335 | </select> |
---|
336 | <td nowrap="nowrap">Example</td> |
---|
337 | |
---|
338 | All tags must be properly closed. Tags without a closing part must follow the |
---|
339 | XHTML convention and end with a space and a slash: |
---|
340 | |
---|
341 | <br /> |
---|
342 | <hr /> |
---|
343 | <img src="example.gif" alt="Example" /> |
---|
344 | <input type="submit" value="Example" /> |
---|
345 | |
---|
346 | All form definitions must be on their own line and either fully defined within |
---|
347 | a <td></td> pair or be outside table tags. Forms must also always have an action |
---|
348 | parameter: |
---|
349 | |
---|
350 | <form method="post" action="http://example.com/example.cgi"> |
---|
351 | <table> |
---|
352 | <tr><td>example</td></tr> |
---|
353 | </table> |
---|
354 | </from> |
---|
355 | |
---|
356 | <table> |
---|
357 | <tr><td> |
---|
358 | <form action="javascript:void(0)" onsubmit="return false;"> |
---|
359 | </form> |
---|
360 | </td></tr> |
---|
361 | </table> |
---|
362 | |
---|
363 | All JavaScript tags must have a valid language and type parameters: |
---|
364 | |
---|
365 | <script language="JavaScript" type="text/javascript"> |
---|
366 | <!-- |
---|
367 | ... |
---|
368 | // --> |
---|
369 | </script> |
---|
370 | |
---|
371 | Nothing may appear after </html>, therefore include any common footers after |
---|
372 | all other output. |
---|
373 | |
---|
374 | All images must have an alt parameter: |
---|
375 | |
---|
376 | <img src="example.gif" alt="<?php echo _("Example"); ?>" /> |
---|
377 | |
---|
378 | Input field of type "image" does not allow the border parameter, and may render |
---|
379 | with a border on some browsers. Use the following instead: |
---|
380 | |
---|
381 | <a href="" onclick="document.formname.submit(); return false;"><img src="example.gif" alt="<?php echo _("Example"); ?>" /></a> |
---|
382 | |
---|
383 | |
---|
384 | ====================================================================== |
---|
385 | Database Naming Conventions |
---|
386 | ====================================================================== |
---|
387 | |
---|
388 | All database tables need to make sure that their table and field names work in all |
---|
389 | databases. Many databases reserve words like 'uid', 'user', etc. for |
---|
390 | internal use, and forbid words that are SQL keywords (select, where, |
---|
391 | etc.). Adding '_tbl' to the end of the table name is a good idea for this reason |
---|
392 | and also improves searching for the table names in the code. |
---|
393 | |
---|
394 | A good way to do this for field names is to make the field name |
---|
395 | table_name_field_name. |
---|
396 | |
---|
397 | When building queries referencing two-or-more tables always preface columns |
---|
398 | with the table containing the column (admin_tbl.username, location_tbl.location_address). |
---|
399 | This method is much preferred over using aliases of table names (a.username, l.location_address). |
---|
400 | |
---|
401 | Table names should be singular (user_tbl instead of users_tbl). |
---|