Strangecode coding standards 29 Aug 2005 20:12:49 ====================================================================== Preamble ====================================================================== Our standards follow closely the PEAR coding standards. Essential reading: http://pear.php.net/manual/en/standards.php ===================================================================== General guidelines ===================================================================== - Respect these coding styles. - 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. - Use Svelte for all UI needs. - Try to not shoehorn your code into existing functions or components. - Simple is better. OOP is not inevitable. Simple functions often work as well, if not better. - If you must use OOP, avoid inheritance as much as possible, no one likes digging several layers of abstraction. - Comment the code. What, why, how, just make your intent clear. ====================================================================== File Naming Conventions ====================================================================== script.php Public accessible scripts. Auth_SQL.inc.php One PHP Class to be included. The filename is the type of class, underscore, name. Or if in subdirs, this could be /Auth/SQL.inc.php while the class name remains "Auth_SQL" some_file.inc.html HTML file, which may or may not have some PHP, but will be included by some other PHP file. Never includes sensitive code as these files may be accessed directly in the web root. script.cli.php A command-line executable script, possibly executed with CRON. Outputs TEXT if any instead of HTML. script.inc.eml Text file to be sent by email. schema.mysql Database schema file that goes with the application. main.screen.css CSS file with media: screen/print/all main.print.css ====================================================================== Indenting and Wrap ====================================================================== Use an indent of 4 spaces, with no tabs. Code and especially comments should usually be wrapped <= 80 characters. Exceptions are made in the case where code readability is significantly improved with longer lines. ====================================================================== Control Structures ====================================================================== These include if, for, while, switch, etc. Here is an example if statement, since it is the most complicated of them: if ((condition1) || (condition2)) { action1; } else if ((condition3) && (condition4)) { action2; } else { defaultaction; } Control statements should have one space between the control keyword and opening parenthesis, to distinguish them from function calls. Even in the case of an if with no else clauses, and only a 1 line action, the curly braces should still be used: if (condition) { action; } This improves readability and allows easy extension of the clause. For switch statements: switch (condition) { case 1: action1; break; case 2: action2; break; default: defaultaction; break; } ====================================================================== Function Calls ====================================================================== Functions should be called with no spaces between the function name, the opening parenthesis, and the first parameter; spaces between commas and each parameter, and no space between the last parameter, the closing parenthesis, and the semicolon. Here's an example: $var = foo($bar, $baz, $quux); As displayed above, there should be one space on either side of an equals sign used to assign the return value of a function to a variable. In the case of a block of related assignments, more space may be inserted to promote readability: $short = foo($bar); $long_variable = foo($baz); ====================================================================== Function and Method Definitions ====================================================================== Function declaractions follow the "one true brace" convention: function fooFunction($arg1, $arg2 = '') { if (condition) { statement; } return $val; } Arguments with default values go at the end of the argument list. Always attempt to return a meaningful value from a function if one is appropriate. Use lowerCamelCase for function and method names to distinguish from php internal functions which standard on underscore_space_style_names(). ====================================================================== Return Values ====================================================================== When functions return boolean values, use 'return false;' or 'return true;' as opposed to 'return 0;' or 'return 1;' or 'return(-1);', except when boolean plurality is necessary. ====================================================================== String Concatenation ====================================================================== Always include a space before and after the concatenation operator '.' which improves readability and improves search-and-replace of adjacent elements. $something = $blah . funky() . ".\".=" . $blab; is better than: $something = $blah.funky().".\".=".$blab; ====================================================================== Quote Marks ====================================================================== Use the single quote marks ' to enclose simple strings whenever possible. Double quote marks " require extra parsing and thus slow things down, but are necessary if entities there must be swapped-out such as variables or control characters. $var['singlequote'] = 'singlequote'; $var["doublequote-$i"] = "$vars and \n funny \t %s things need doublequotes"; $var['doublequote-' . $i] = $var . 'you can do this' . "\t %s" . $var2 . 'but it isn\'t any better'; ====================================================================== Printing HTML ====================================================================== For large or complex blocks of HTML, using the following method is much faster and safer than echo because the php processor is bypassed and content goes directly to output: