Coding style

From StatusNet
Jump to: navigation, search

Broadly, StatusNet code follows the PEAR project's coding style guidelines: http://pear.php.net/manual/en/standards.php

Contents

[edit] Quick sample

<?php

function common_timezone()
{
    if (common_logged_in()) {
        $user = common_current_user();
        if ($user->timezone) {
            return $user->timezone;
        }
    }

    return common_config('site', 'timezone');
}


[edit] PHP code tags

<?php

Always use the full-length <?php open tag; short tags <? may be unavailable depending on server configuration.

It's recommended to skip the closing ?> tag at the end of source files, due to the likelihood of trouble caused by extra whitespace at the end. Too many spaces or newlines following a closing tag can lead to hard-to-diagnose problems with missing HTTP headers and invalid XML output, which can be notoriously hard to track down.

[edit] Function definitions

function common_timezone()
{
    ...
}

Opening curly brace should be on the line following the function definition.

[edit] Naming

Standalone function names usually follow the internal PHP naming style, with lowercased_words separated_with_underscores.

Classes are InitiallyCased.

Member functions follow camelCase.

[edit] Parens

    if (common_logged_in()) {
        ...
    }
    return common_config('site', 'timezone');
  • Separate control structures (if, for, etc) from their parens with a space.
  • Do not leave any space between a function name and its argument list.
  • Do not leave extra whitespace inside parens.


[edit] Indentation

Use four spaces per indent... please don't mix tabs and spaces!


[edit] JavaScript

Client-side JS code for the user interface heavily uses the jQuery framework.

Style generally follows the above conventions with a couple exceptions:

  • Function definitions usually include the opening paren on the same line

[edit] Filesystem layout

Broadly:

  • / - entry points only (index.php, install.php)
  • /action - FooAction -> foo.php
  • /classes - Foo_member extends Memcache_Data_Object -> Foo_member.php
  • /lib - FooList -> foolist.php
  • /scripts - command-line scripts and daemons (queuedaemon.php, etc)
  • /js - core JavaScript files including libraries
  • /theme - CSS and images for theme subdirs
  • /plugins - core-maintained plugins
    • each either in FooPlugin directory or a single file
      • FooPlugin -> FooPlugin.php
  • /local - area for local plugins and themes, same subdir style
Personal tools
Namespaces
Variants
Actions
Navigation
Status.net
Toolbox