wiki:Dev_Guidelines

Version 14 (modified by thomasb, 5 years ago) (diff)

--

RoundCube Development Guidelines

Here are a few rules and explanations that should make it easier for new contributors to understand the structure and guidelines of the RoundCube code base.

Available Documentation

The code is not very well documented but using the PHPDoc comments we finally made it to create a class and functions overview. More details can be found at the RoundCube Documentation page. This also explains the directory structure and some common functions and classes.

Naming conventions

File names

In general all files should have the appropriate extension such as .php, .js, etc. For PHP include files that are not meant to be executed directly please use the extension .inc. This prevents them from being executed directly in case that the access restrictions do not work as intended.

All files should be named with lower case letters and underlines (for word separation).

Functions and variables

Function and variable names should only contain lower case letters and numbers and use underlines for word separation. In classes, private methods usually start with an underline.

Functions providing RoundCube specific functionality should start with either rcmail_ (webmail specific functions) or rcube_ (global/framework functions).

Code style

All code (PHP and JavaScript) obeys the same code style guidelines:

  • Opening and closing brackets for function and class definitions are on a new line
  • Never write control-flow code on a line
  • Code should not exceed a limit of 80 characters per line
  • Never omit brackets for blocks containing only one statement
  • Indentation consists of 4 spaces per level and no tabs!
  • Conventional operators should be surrounded by a space
  • Commas should be followed by a space
  • Semi-colons in for statements should be followed by a space
  • All names should be written in English
  • Logical units within a block should be separated by at least one blank line
  • Iterator variables should be called "i", "j", "k", etc.
  • Globals vars (PHP) should be named in upper case
  • Variables should be initialized where they are declared and they should be declared in the smallest scope possible
  • In general, we adhere to PEAR CS

Good code:

function foo_bar($aa, $bb)
{
    $out = '';

    if ($aa == 1 || $aa > 10) {
        $out .= "Case one\n";
        write_log("Foo: $aa");
    }
    else {
        alert('Bar');
    }

    for ($i=0; $i < $bb; $i++) {
        $out .= $i . 'a';
    }
    return $out;
}

The bracket indentation of the existing code differs from this example but I guess we should move towards this more common indentation style.

Bad code:

function fooBar($aa,$bb) {
  if ($aa==1 || $aa>10){
    $out.="Case one\n";
    write_log("Foo: $aa");
  } else {
    alert("Bar");
  }
  for ($i=0;$i<$bb;$i++)
    $out.=$i."a";

  return $out;
}

PHP Coding

All code should be functional in PHP 4.3.x and PHP 5.x. Especially for classes, you need to write constructors that work in both PHP versions. When working with files please also make sure that the code works with safe_mode enabled. Set error reporting to E_NOTICE to see if all variables are properly declared.

For security reasons NEVER access GET/POST variables directly but use get_input_value() instead. See PHP Commons for details.

Comments/Documentation?

Please add comments to logical code blocks giving a short description what this part is intended to do. For PHP classes, please add PHPDoc styled descriptions for public methods. Private method descriptions are optional.