[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 | |
---|
[767] | 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 | |
---|
[1] | 28 | ====================================================================== |
---|
[293] | 29 | File Naming Conventions |
---|
[1] | 30 | ====================================================================== |
---|
| 31 | |
---|
| 32 | script.php Public accessible scripts. |
---|
| 33 | |
---|
| 34 | Auth_SQL.inc.php One PHP Class to be included. The filename is the |
---|
[42] | 35 | type of class, underscore, name. Or if in subdirs, |
---|
[1] | 36 | this could be /Auth/SQL.inc.php while the class name |
---|
| 37 | remains "Auth_SQL" |
---|
| 38 | |
---|
[214] | 39 | some_file.inc.html HTML file, which may or may not have some PHP, |
---|
[1] | 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 | |
---|
[42] | 44 | script.cli.php A command-line executable script, possibly executed |
---|
[214] | 45 | with CRON. Outputs TEXT if any instead of HTML. |
---|
[1] | 46 | |
---|
[293] | 47 | script.inc.eml Text file to be sent by email. |
---|
| 48 | |
---|
[214] | 49 | schema.mysql Database schema file that goes with the application. |
---|
[1] | 50 | |
---|
| 51 | main.screen.css CSS file with media: screen/print/all |
---|
| 52 | main.print.css |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | ====================================================================== |
---|
[293] | 56 | Indenting and Wrap |
---|
[1] | 57 | ====================================================================== |
---|
| 58 | |
---|
| 59 | Use an indent of 4 spaces, with no tabs. Code and especially comments should |
---|
[767] | 60 | usually be wrapped <= 80 characters. Exceptions are made in the case where |
---|
[293] | 61 | code readability is significantly improved with longer lines. |
---|
[1] | 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; |
---|
[42] | 97 | |
---|
[1] | 98 | case 2: |
---|
| 99 | action2; |
---|
| 100 | break; |
---|
[42] | 101 | |
---|
[1] | 102 | default: |
---|
| 103 | defaultaction; |
---|
| 104 | break; |
---|
[42] | 105 | |
---|
[1] | 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 | ====================================================================== |
---|
[293] | 130 | Function and Method Definitions |
---|
[1] | 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 | |
---|
[293] | 146 | Use lowerCamelCase for function and method names to distinguish from php |
---|
| 147 | internal functions which standard on underscore_space_style_names(). |
---|
[1] | 148 | |
---|
| 149 | |
---|
| 150 | ====================================================================== |
---|
[293] | 151 | Return Values |
---|
[1] | 152 | ====================================================================== |
---|
| 153 | |
---|
[42] | 154 | When functions return boolean values, use 'return false;' or 'return true;' |
---|
[293] | 155 | as opposed to 'return 0;' or 'return 1;' or 'return(-1);', except when |
---|
| 156 | boolean plurality is necessary. |
---|
[1] | 157 | |
---|
| 158 | |
---|
| 159 | ====================================================================== |
---|
[293] | 160 | String Concatenation |
---|
[1] | 161 | ====================================================================== |
---|
| 162 | |
---|
[209] | 163 | Always include a space before and after the concatenation operator '.' which |
---|
[1] | 164 | improves readability and improves search-and-replace of adjacent elements. |
---|
| 165 | |
---|
| 166 | $something = $blah . funky() . ".\".=" . $blab; |
---|
[42] | 167 | |
---|
[1] | 168 | is better than: |
---|
[42] | 169 | |
---|
[1] | 170 | $something = $blah.funky().".\".=".$blab; |
---|
| 171 | |
---|
| 172 | |
---|
| 173 | ====================================================================== |
---|
[293] | 174 | Quote Marks |
---|
[1] | 175 | ====================================================================== |
---|
| 176 | |
---|
[42] | 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 |
---|
[1] | 180 | control characters. |
---|
| 181 | |
---|
[293] | 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'; |
---|
[1] | 186 | |
---|
| 187 | |
---|
| 188 | ====================================================================== |
---|
[293] | 189 | Printing HTML |
---|
[1] | 190 | ====================================================================== |
---|
| 191 | |
---|
| 192 | For large or complex blocks of HTML, using the following method is much faster |
---|
[209] | 193 | and safer than echo because the php processor is bypassed and content goes |
---|
[1] | 194 | directly to output: |
---|
| 195 | |
---|
| 196 | <?php |
---|
[293] | 197 | if ($something) { |
---|
| 198 | // Here comes multi-line HTML... |
---|
[1] | 199 | ?> |
---|
[214] | 200 | <div class="sc-tiny"> |
---|
| 201 | <a href="<?php echo $app->ohref('/contact.php') ?>">Contact us</a> |
---|
[1] | 202 | </div> |
---|
| 203 | <?php |
---|
[293] | 204 | } else if ($somethingelse) { |
---|
| 205 | ?><h1>Just a single-line of html</h1><?php |
---|
[1] | 206 | } |
---|
| 207 | ?> |
---|
| 208 | |
---|
| 209 | |
---|
| 210 | ====================================================================== |
---|
| 211 | Comments |
---|
| 212 | ====================================================================== |
---|
| 213 | |
---|
[42] | 214 | Function comments should follow the Javadoc standard, with detailed |
---|
[1] | 215 | function comments and one-line pointers along the way: |
---|
| 216 | http://java.sun.com/products/jdk/javadoc/writingdoccomments/index.html |
---|
[42] | 217 | |
---|
[214] | 218 | Here is an example of the codebase method Email::validEmail(): |
---|
| 219 | |
---|
[1] | 220 | <?php |
---|
| 221 | /** |
---|
[214] | 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 |
---|
[1] | 229 | * |
---|
| 230 | * @access public |
---|
[214] | 231 | * @param mixed $email Address to check, string or array. |
---|
| 232 | * @return bool Validity of address. |
---|
[1] | 233 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
[214] | 234 | * @since 30 Nov 2005 22:00:50 |
---|
[1] | 235 | */ |
---|
[214] | 236 | function validEmail($email) |
---|
[1] | 237 | { |
---|
[214] | 238 | $app =& App::getInstance(); |
---|
[767] | 239 | |
---|
[214] | 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; |
---|
[1] | 248 | } else { |
---|
[334] | 249 | // To be valid email address must match regex and fit within the length constraints. |
---|
[247] | 250 | if (preg_match($this->getParam('regex'), $email, $e_parts) && mb_strlen($e_parts[2]) < 64 && mb_strlen($e_parts[3]) < 255) { |
---|
[214] | 251 | return true; |
---|
| 252 | } else { |
---|
| 253 | $app->logMsg(sprintf('Invalid email: %s', $email), LOG_INFO, __FILE__, __LINE__); |
---|
| 254 | return false; |
---|
| 255 | } |
---|
[1] | 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 | |
---|
[293] | 289 | Always use <?php ?> to delimit PHP code, not the <? ?> shorthand. Even |
---|
| 290 | use <?php echo $name; ?> instead of <?=$name?>. |
---|
[1] | 291 | |
---|
| 292 | |
---|
| 293 | ====================================================================== |
---|
[293] | 294 | php.ini Settings |
---|
[1] | 295 | ====================================================================== |
---|
| 296 | |
---|
| 297 | All code should work with register_globals = Off. This means using |
---|
[293] | 298 | $_REQUEST, $_GET, $_POST, $_COOKIE, $_SESSION, $_SERVER, and $_ENV |
---|
[767] | 299 | to access all request, get, post, cookie, session, server, and |
---|
[293] | 300 | environment data, respectively. |
---|
[1] | 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. |
---|
[293] | 307 | Form data should be passed through stripslashes() if necessary. |
---|
[1] | 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 |
---|
[293] | 311 | the same directory. Or better yet, for less abiguity, use: |
---|
| 312 | include dirname(__FILE__) . '/include_me.inc.php'; |
---|
[1] | 313 | |
---|
| 314 | |
---|
| 315 | ====================================================================== |
---|
| 316 | XHTML 1.0 Compliance |
---|
| 317 | ====================================================================== |
---|
| 318 | |
---|
[293] | 319 | All HTML should be valid XHTML 1.0 verified with the |
---|
[1] | 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 | |
---|
[20] | 332 | <input type="checkbox" checked="checked" /> |
---|
[1] | 333 | <select name="example"> |
---|
| 334 | <option selected="selected" value="1">Example</option> |
---|
| 335 | </select> |
---|
| 336 | <td nowrap="nowrap">Example</td> |
---|
| 337 | |
---|
[42] | 338 | All tags must be properly closed. Tags without a closing part must follow the |
---|
[1] | 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 |
---|
[209] | 347 | a <td></td> pair or be outside table tags. Forms must also always have an action |
---|
[1] | 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). |
---|
[209] | 399 | This method is much preferred over using aliases of table names (a.username, l.location_address). |
---|
[1] | 400 | |
---|
| 401 | Table names should be singular (user_tbl instead of users_tbl). |
---|