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