Changes between Initial Version and Version 1 of CodingStandard


Ignore:
Timestamp:
2011-05-27T00:05:58Z (13 years ago)
Author:
Pan Luo
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • CodingStandard

    v1 v1  
     1[[PageOutline]]
     2
     3= Coding Standard =
     4
     5Following [http://en.wikipedia.org/wiki/Coding_standards coding standards] is one of the easiest way for everybody to understand everybody's code.
     6
     7== Indenting and Whitespace ==
     8
     9* '''Never use tabulations''' in the code. Indentation is done by steps of '''2 spaces''':
     10
     11{{{
     12#!php
     13<?php
     14class sfFoo {
     15  public function bar() {
     16    sfCoffee::make();
     17  }
     18}
     19}}}
     20
     21* Lines should have no trailing whitespace at the end.
     22
     23* Files should be formatted with \n as the line ending (Unix line endings), not \r\n (Windows line endings) or mac endings.
     24
     25* All text files should end in a single newline (\n). This avoids the verbose "\ No newline at end of file" patch warning and makes patches easier to read since it's clearer what is being changed when lines are added to the end of a file.
     26
     27* Don't put spaces after an opening parenthesis and before a closing one.
     28{{{
     29#!php
     30<?php
     31if ($myVar == getRequestValue($name))    // correct
     32if ( $myVar == getRequestValue($name) )  // incorrect
     33}}}
     34
     35== Operators ==
     36
     37* All binary operators (operators that come between two values), such as +, -, =, !=, ==, >, etc. should have a space before and after the operator, for readability.
     38{{{
     39#!php
     40<?php
     41$foo = $bar; //correct
     42$foo=$bar;  //incorrect
     43}}}
     44
     45* Unary operators (operators that operate on only one value), such as ++, should not have a space between the operator and the variable or number they are operating on.
     46{{{
     47#!php
     48<?php
     49$i++;
     50}}}
     51
     52* When comparing a variable to a string, put the string first and use type testing when applicable:
     53{{{
     54#!php
     55<?php
     56if (1 === $variable)
     57}}}
     58
     59
     60== Control Structures ==
     61* Use braces for indicating control structure body regardless of number of statements it contains.
     62* Control statements should have one space between the control keyword and opening parenthesis, to distinguish them from function calls.
     63For if statements:
     64{{{
     65#!php
     66<?php
     67if ($condition1 || $condition2) {
     68  action1;
     69} elseif ($condition3 && $condition4) {
     70  action2;
     71} else {
     72  defaultaction;
     73}
     74}}}
     75
     76For switch statements:
     77{{{
     78#!php
     79<?php
     80switch ($condition) {
     81  case 1:
     82    action1;
     83    break;
     84
     85  case 2:
     86    action2;
     87    break;
     88
     89  default:
     90    defaultaction;
     91}
     92}}}
     93
     94For do-while statements:
     95{{{
     96#!php
     97<?php
     98do {
     99  actions;
     100} while ($condition);
     101}}}
     102
     103== Function Calls ==
     104
     105* 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:
     106{{{
     107#!php
     108<?php
     109$var = foo($bar, $baz, $quux);
     110}}}
     111
     112* Use `camelCase`, not underscores, for variable, function and method names:
     113   * Good: `function makeCoffee()`
     114   * Bad:  `function MakeCoffee()`
     115   * Bad:  `function make_coffee()`
     116 
     117
     118== Function Declarations ==
     119* In a function body, return statements should have a blank line prior to it to increase readability.
     120{{{
     121#!php
     122<?php
     123function makeCoffee() {
     124  if (false !== isSleeping() && false !== hasEnoughCaffeineForToday()) {
     125    canMakeCoffee();
     126
     127    return 1;
     128  } else {
     129    cantMakeCoffee();
     130  }
     131
     132  return null;
     133}
     134}}}
     135
     136* 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.
     137
     138Use underscores for option/argument/parameter names.
     139
     140== Class Constructor Calls ==
     141
     142* When calling class constructors with no arguments, always include parentheses:
     143{{{
     144#!php
     145<?php
     146$foo = new MyClassName();
     147}}}
     148
     149
     150== Arrays ==
     151
     152* Arrays should be formatted with a space separating each element (after the comma), and spaces around the => key association operator, if applicable:
     153{{{
     154#!php
     155<?php
     156$some_array = array('hello', 'world', 'foo' => 'bar');
     157}}}
     158
     159
     160* Note that if the line declaring an array spans longer than 80 characters (often the case with form and menu declarations), each element should be broken into its own line, and indented one level:
     161{{{
     162#!php
     163<?php
     164$form['title'] = array(
     165  '#type' => 'textfield',
     166  '#title' => t('Title'),
     167  '#size' => 60,
     168  '#maxlength' => 128,
     169  '#description' => t('The title of your node.'),
     170);
     171}}}
     172
     173* Note the comma at the end of the last array element; This is not a typo! It helps prevent parsing errors if another element is placed at the end of the list later.
     174
     175== String Concatenations ==
     176* Always use a space between the dot and the concatenated parts to improve readability.
     177* Avoid evaluating variables within strings, instead opt for concatenation:
     178{{{
     179#!php
     180<?php
     181$string = 'something';
     182$newString = "$string is awesome!";  // bad, not awesome
     183$newString = $string.' is awesome!'; // better
     184$newString = sprintf('%s is awesome', $string); // for exception messages and strings with a lot of substitutions
     185}}}
     186* When using the concatenating assignment operator ('.='), use a space on each side as with the assignment operator:
     187
     188== Comments ==
     189
     190All function and class methods should have their [http://www.phpdoc.org/about.php phpdoc] own block:
     191
     192 * All `@...` statements do not end with a dot.
     193 * `@param` lines state the type and the variable name. If the variable can have multiple types, then the `mixed` type must be used.
     194 * Ideally `@...` lines are vertically lined up (using spaces):
     195{{{
     196#!php
     197<?php
     198/**
     199 * Notifies all listeners of a given event.
     200 *
     201 * @param  Event  $event  A Event instance
     202 *
     203 * @return Event          The Event instance
     204 */
     205public function notify($event)
     206}}}
     207
     208All one line comments should be on their own lines and in this format:
     209{{{
     210#!php
     211<?php
     212// space first, with no full stop needed
     213}}}
     214
     215== PHP Code Tags ==
     216
     217* Don't end library files with the usual `?>` closing tag. This is because it is not really needed, and because it can create problems in the output if you ever have white space after this tag.
     218
     219* Always use <?php ?> to delimit PHP code, not the shorthand, <? ?>.
     220
     221== Constants ==
     222
     223Use lowercase PHP native typed constants: `false`, `true` and `null`. The same goes for `array()`. At the opposite, always use uppercase strings for user defined constants, like `define('MY_CONSTANT', 'foo/bar')`. Better, try to always use class constants:
     224{{{
     225#!php
     226<?php
     227class Coffee {
     228  const HAZ_SUGAR = true;
     229}
     230var_dump(Coffee::HAZ_SUGAR);
     231}}}
     232
     233
     234== Tools ==
     235
     236* Vim: [http://drupal.org/node/29325]
     237* Please add the coding standard plugin for eclipse here.
     238
     239This documentation is based on [http://trac.symfony-project.org/wiki/HowToContributeToSymfony#CodingStandards Symfony Coding Standard] and [http://drupal.org/coding-standards#indenting Drupal Coding Standard].