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