mantisbt:coding_guidelines
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| mantisbt:coding_guidelines [2008/04/17 20:20] – We now use more OO after moving to PHP 5 vboctor | mantisbt:coding_guidelines [2025/01/11 10:16] (current) – [Coding Guidelines] fallback to PHP-FIG PER Coding Style dregad | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | There has been a discussion on new coding standards for PHP5 object orientation on the Mailing list: | ||
| + | http:// | ||
| + | |||
| + | ====== Coding Guidelines ====== | ||
| + | |||
| + | First, read the [[http:// | ||
| + | The rest of this page describes in which ways we differ, as well as other Mantis-specific guidelines. | ||
| + | |||
| + | If something is not defined in this document, then please apply the rules in PHP-FIG' | ||
| + | |||
| + | Please discuss any omissions or disagreements on our [[https:// | ||
| + | |||
| + | |||
| + | |||
| + | ===== General Formatting ===== | ||
| + | |||
| + | * Line length: should generally be kept below **80 chars**, although it is acceptable to have lines up to 120 chars when necessary. | ||
| + | * Indentations: | ||
| + | * PHP tags: always use ''<? | ||
| + | |||
| + | ===== Comments ===== | ||
| + | |||
| + | * Use the ''#'' | ||
| + | * Avoid ''/ | ||
| + | * Use [[http:// | ||
| + | * Use '' | ||
| + | |||
| + | |||
| + | ===== Code Blocks ===== | ||
| + | |||
| + | |||
| + | ==== Braces and Parentheses ==== | ||
| + | |||
| + | * Opening braces '' | ||
| + | * Closing braces '' | ||
| + | * At the end of a long condition or many nested conditions you should place end markers such as //# end for loop// or //# end outermost while// | ||
| + | |||
| + | * No space before the opening parentheses '' | ||
| + | * One space after the opening '' | ||
| + | * Arrays elements should be referenced without spaces, e.g. '' | ||
| + | |||
| + | Example:< | ||
| + | while( while_condition ) { | ||
| + | ... lots of code ... | ||
| + | if( blah_condition ) { | ||
| + | for( ... ) { | ||
| + | } | ||
| + | ... lots of code ... | ||
| + | } # end blah if | ||
| + | ... lots of code ... | ||
| + | |||
| + | if( cond1 ) { | ||
| + | something | ||
| + | } else if( cond2 ) { | ||
| + | something two | ||
| + | } else { | ||
| + | something else | ||
| + | } | ||
| + | } # end while true | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Conditions ==== | ||
| + | |||
| + | * For equality comparisons, | ||
| + | if( CONSTANT == $blah ) { | ||
| + | ...code... | ||
| + | } | ||
| + | </ | ||
| + | * The NOT operator '' | ||
| + | * Add parentheses as appropriate to make things improve readability (even if not syntactically required by operators precedence). | ||
| + | |||
| + | To facilitate reading of complex conditions, you can either: | ||
| + | |||
| + | * Use variables to store sub-conditions <code php> | ||
| + | $t_cond1 = CONSTANT > some_function( arg1, arg2, arg3 ); | ||
| + | $t_cond2 = 0 <= $var1 && 100 > $var1; | ||
| + | if( $t_cond1 || $t_cond2 ) { | ||
| + | ... | ||
| + | </ | ||
| + | * Break down the condition over multiple lines, aligning and grouping the conditions <code php> | ||
| + | if( CONSTANT > some_function( arg1, arg2, arg3 ) | ||
| + | || 0 <= $var1 | ||
| + | && 100 > $var1 | ||
| + | ) { | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Switch statements ==== | ||
| + | |||
| + | Every case //must// have a '' | ||
| + | |||
| + | <code php> | ||
| + | switch( condition ) { | ||
| + | case 1: | ||
| + | blah; | ||
| + | break; | ||
| + | case 2: | ||
| + | blah; | ||
| + | # Fall through must be commented | ||
| + | case 3: # No need for a break here | ||
| + | case 4: | ||
| + | blah; | ||
| + | return; | ||
| + | default: | ||
| + | blah; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Miscellaneous ===== | ||
| + | |||
| + | * Avoid print/echo of HTML unless it's short or in a function loop | ||
| + | * Do not use the EOF construct | ||
| + | * Severely limit use of ternary operator ''?:'' | ||
| + | * The '' | ||
| + | * Keep functions short. | ||
| + | * Do not use embedded assignments at all. One popular method is in a while loop for database row fetching. | ||
| + | * Mark unusual comment notes with a @@@ and one of the Gotcha keywords (KLUDGE, TRICKY etc.) You may also want to leave your username. | ||
| + | * Avoid magic numbers at all costs. | ||
| + | * Use single quotes around strings, unless double quotes are necessary (i.e. if you have '' | ||
| + | |||
| + | |||
| + | ===== File Names ===== | ||
| + | |||
| + | * Use all lower case. | ||
| + | * Use '' | ||
| + | * Use '' | ||
| + | * Filenames must be less than 32 characters in length. | ||
| + | * Includes should be suffixed by '' | ||
| + | |||
| + | Always include the standard header at the top of the file. | ||
| + | |||
| + | |||
| + | ===== Classes Names ===== | ||
| + | |||
| + | * Use CamelCase style, e.g. MantisFormattingPlugin | ||
| + | * Variables that are class objects should have the prefix coo_ | ||
| + | |||
| + | |||
| + | ===== Function Names ===== | ||
| + | |||
| + | * Use all lower case | ||
| + | * Separate words with underscore ' | ||
| + | * should be 5 words or less | ||
| + | * Should have the name of the API they are in as prefix, e.g. email_send_all(); | ||
| + | * < | ||
| + | * < | ||
| + | * A function that performs an action should should use a verb as the second token, e.g.: | ||
| + | * project_add_user() | ||
| + | * bug_delete_bugnote() | ||
| + | * A function that returns information should use ' | ||
| + | * bug_get_field() | ||
| + | * project_get_bug_count() | ||
| + | * " | ||
| + | * " | ||
| + | * [[http:// | ||
| + | |||
| + | |||
| + | ===== Variables Names ===== | ||
| + | |||
| + | * On principle, always use descriptive names, with the appropriate prefix (see below). | ||
| + | * Use all lower case keywords | ||
| + | * Separate words with underscore ' | ||
| + | |||
| + | ==== Prefixes ==== | ||
| + | |||
| + | * g_ for globals. | ||
| + | * p_ for function parameters. | ||
| + | * f_ for (uncleaned) form variables. | ||
| + | * v_, v2_ for extract variables. | ||
| + | * c_ for checked variables (e.g. parameters passed from forms that have been cleaned of any special SQL chars such as slashes) | ||
| + | * u_ for user variables. | ||
| + | * t_ for temporary variables. | ||
| + | |||
| + | Do not use q, o, i, or l for prefixes as they are visually confusing and prone to being mistaken for each other or existing prefixes. | ||
| + | |||
| + | ==== Counters and Loop variables ==== | ||
| + | |||
| + | * Good names for count variables are $t_bug_count, | ||
| + | * Good names for counters are $t_bug_counter, | ||
| + | * We use $i, $k, etc. for loop variables. | ||
| + | |||
| + | ==== Other Variables ==== | ||
| + | |||
| + | * $t_query is used commonly (previsouly, | ||
| + | * $t_result follows the same pattern as $t_query. | ||
| + | * $t_row also follows the same pattern as $t_query. | ||
| + | |||
| + | |||
| + | |||
| + | ===== SQL Formatting ===== | ||
| + | |||
| + | * UPPERCASE all SQL keywords (SELECT, FROM, etc.). | ||
| + | * Always create the query as a variable before sending it to the query function. | ||
| + | * Some consideration should be given to making things portable. | ||
| + | * Break up SQL queries over multiple lines to make them easier to read. <code php> | ||
| + | $query = " | ||
| + | FROM $t_bug_table | ||
| + | WHERE id=' | ||
| + | </ | ||
| + | |||
| + | ===== CSS ===== | ||
| + | |||
| + | Tables should be wrapped a '' | ||
| + | |||
| + | <code html> | ||
| + | <div class=" | ||
| + | </ | ||
