Translations
Info
All page names need to be in English.
en da  de  fr  it  ja  km  nl  ru  zh

TYPO3 Coding Guidelines - Notes

From TYPO3Wiki
Jump to: navigation, search
Draft Change the {{draft}} marker to {{review}} when you need a reviewer for text and TypoScript. info

Please use the discussion page for suggestions and changes


Contents

TYPO3 Coding Guidelines - Notes

General

  • do not bind CGL to specific versions of the core
  • rewrite CGL in a more "rule" style
  • check how documentation looks when generated
  • fix kickstarter!!!
  • fix extdeveval
  • no more function index required


spaces

  • before and after operators (>, .=, <, =, ==, *, +, ...) (doesn't apply for $i-- and $i++)
  • spaces after comma in function arguments ($argument1, $argument2, $argument3)


inline comments

  • keep them on a separate line, inline comments must precede the commented line and be indented by one tab.
  • comment your code if it is not immediately clear what it does, damn!

debug output

  • do not check in commented debug code
  • exception: comment it properly so that one can determine when it can be removed or activated again


IF/ELSE/ELSEIF

  • space before and after opening/closing "("
  • always use curly braces


PHP script:
<?php
 
    if ($a != 2) {
        $a = 2;
    } elseif ($a == 3) {
        $a = 4;
    } else {
        $a = 7;
    }
 
?>


Line endings:

  • unix
  • no empty lines / newline after or before php tags (<?php ?>)
  • no whitespace at the end of lines
  • keep lines shorter than 120 characters whenever possible
  • commented lines should stay below 80 chars as it makes them easier to read

php tags

  • always long opening tag
  • always closing tag


PHP script:
<?php
 
?>


switch () {}

  • there must be no more than one break per case
  • write some comment if you leave out the break
  • break is indented to the indentation of the code
  • default is the last "case"
  • there's no break in "default"


loops

  • use foreach instead of while (list())
  • if using foreach, there is no need for using reset()

The function of this two examples is identical:

PHP script:
<?php
 
    $arr = array('un', 'dos', 'tres');
    reset ($arr);
    while (list(, $value) = each ($arr)) {
       echo 'Value:  $value<br />\n';
    }
 
 
    foreach ($arr as $value) {
       echo 'Value:  $value<br />\n';
    }
 
?>


Same with this example:

PHP script:
<?php
 
    $arr = array('un', 'dos', 'tres');
    reset ($arr);
    while (list($key, $value) = each ($arr)) {
       echo 'Value: $key; Value: $value<br />\n';
    }
 
 
    foreach ($arr as $key => $value) {
       echo 'Value: $key; Value: $value<br />\n';
    }
 
?>

filenames:

  • as long as you wish
  • all lower case


classes, functions, and variables

  • add a comment about what the class does before the class
  • only one class per file
  • camelCase, no underscores
  • start with lower case
  • use speaking and descriptive names
  • don't shorten names for the sake of it
  • do not write the variable name in the parameter comment for methods


Strings

  • use single quotes (faster)


Concatenation of strings:

PHP script:
<?php
 
$project = 'TYPO3' . ' ' . 'Association';
 
?>


Multi line strings:

  • dot at the end


PHP script:
<?php
 
$longString = 'dkjfbs' .
'ajsdjka';
 
?>


PHP5 features:

  • use type hints (no must, but should)
  • use protected instead of private, when using private make a comment why
  • do not use the "var" keyword anymore


array:

  • define arrays before passing them to functions
  • declare array before filling them
  • check whether the array index is set in case you're not sure whether it is there
  • leave the check out in case you're sure that the index exists
  • arrays as class members should be declared as such during declaration / initialize them with declaration


PHP script:
<?php
 
    $a = array();
    $a = array(
        'asd',
        'ajsd',
        );
 
?>


Indentations:

  • tabs (only) at the beginning of the line, everything else with spaces


global variables

  • do not use the global keyword, use $GLOBALS[] instead


style

  • if the phpDoc comment says a method returns something, it must return somethign in any case
  • Use early returns only for guard clauses and only if they greatly improve readability.
  • only one return statement per function (exceptions allowed for legacy code)
  • mention the default values for optinal parameters in function comments
  • when using the terniary operator (short if/else), put it in "()"
  • no nesting with the terniary operator
  • uppercase booleans and null

Suggestions

  • convert code to utf8 at some point
  • line up arrows in associative arrays


RFCs

  • use true instead of 1 and false instead of 0

Tonda 12:52, 17 September 2008 (CEST)

  • use true for TRUE and false for FALSE because its faster (PHP knows this constants in lowercase too and interprets this in first run).

See http://www.phpbar.de/w/Code-Optimierungen (its german) Tonda 12:27, 17 September 2008 (CEST)

Go to: typo3.org    
Login, and you can edit.
Personal tools
Namespaces

Variants
Actions