Code Formatting at Mediamatic and in anyMeta

Everyone uses different ways to format their code. And everyone has very good reasons to do it his/her own way. Of course, at Mediamatic we also use our own coding style. This article shows the coding standard we use.

In another article I will discuss the code commenting we use, including the documentation we write inside the code.

Most of the code in anyMeta is written in PHP. PHP does not have namespaces, and uses many constructs well known to C programmers. Because of this we started with reusing the coding standard that was (and I guess still is) in use at Tasking for their C programs.

Indentation

A simple guideline here.

  • indentation is four spaces,
  • tab length is also four positions.

Not eight because we don't want to end up at the right hand side of the screen, not less than four because we want to prevent too deeply nested constructs and unclear indentation within longer code blocks.

Curly braces

Always a hot topic. Where do you place the braces? An example of our use is the simplest:

function foo ()
{
    if (bar())
    {
        do_something();
    }
    else
    {
        do_something_else();
    }
}

So we always have the opening and closing braces on the same indentation, each on a separate line. This makes it very clear which brace belongs to which block. Just move up or down on a certain indentation level and you will find the accompaning curly brace.

Another rule: always use curly braces, even when the block contains a single statement. Why? In C you never know for sure if that one function in your block doesn't expand to multiple statements after preprocessing. The same might happen with search & replace actions in your editor. Always enclosing a block in curly braces makes sure that your else parts still belong to the correct if.

For example:

if ($a)
    if ($c) b();
    else c();

Now, with our pre-processor or text editor, we replace b() with two statements, b1();b2():

if ($a)
    if ($c) b1(); b2();
    else c();

which of course means:

if ($a)
{
    if ($c)
    {
        b1();  // the first statement of the if ($c)
    }
    b2();  // uh oh, this one should be in the if ($c)...
}
else
{
    c();   // and now this whole else has been moved!
}

This problem is easily prevented by the use of curly braces. Don't bother about more lines and bigger files. Readability is much more important than a bit less lines.

Switch statements

All cases in a switch statement should have a break. Only exception is when you really write a lot of comment where you explain what you are doing. In a very convincing way!

Also: always add a default clause, so that it is clear that in exceptional cases you are not doing anything!

Example:

switch ($method)
{
case 'show'
    show();
    break;
case 'uh':
case 'oh':
    amaze();
    break;
default:
    break;
}

Function definitions and calls

We use a small but significant difference in the spacing around the parentheses when we define a function or call a function.

function myfunc ( $a, $b )
{
    return otherfunc($a) + somefunc($b, 4);
}

See the spaces when defining a function, and not when we are calling a function? Simple reason here, when you search for the function name plus a space you will find its definition, when you search for a function name plus a parenthesis you will find where it is used! Simple and practical.

Besides this, always use a space after the commas in the parameter lists. Always use spaces around operators.

Complex logical expressions

When you have bigger nested conditions, with some interesting boolean logic, then we format it in the following way:

if (    $a
    &&  $b == 'view'
    &&  (   $c    == 'edit'
        ||  $cond == 'uhoh'))
{
    // ....
}

Basically:

  • boolean operator at the start of the sub-conditions
  • nesting with parentheses, boolean operators on same level
  • align the comparison operators

When it is a simple or or and condition:

if (!$type || $type == 4 || $type == 13)
{
    $is_pic = 0;
}

Multiple assignments

Next to the fact that we always have spaces around operators, we also try to make the structure of code clearer by forcing a vertical alignment of constructs.

$conf      = isset($args['conf'])      ? $args['conf']      : array();
$person_id = isset($args['person_id']) ? $args['person_id'] : '';
$role      = isset($args['role'])      ? $args['role']      : '';

This makes code a lot more readable than when you use minimal spacing.

see also

code

php download technical gpl anymeta opensource mediamatic oauth library mmcached

oauth-php - Google Code

A PHP library for OAuth consumers and servers. Complete with an extensible OAuth store, includi...

Securing your site against code injections

All Internet applications have to secure their inner workings against attacks from outside. We a...

OAuth Server And Consumer in PHP

Here is the full implementation of OAuth for anyMeta. For now we...

mmcached - a hierarchical extension to memcached

There is a new and better version of this memcached clone. Please check ou...

Verso Wiki: translate Wiki markup to HTML and HTML to Wiki markup

Verso Wiki is a Wiki to HTML and HTML to Wiki markup translator. We use this W...

Depcached - memcache(d) with dependencies

When using memcache we bumped into some problems. The major one was that we needed to invalidate...

The PHP Metadata Toolkit

The PHP JPEG metadata toolkit is a rather complete library to read the EXIF data from digital cam...

OAuth - Added Body Signing

I just published a new version of our OAuth server and consumer code. The major addition to thi...

LinkSleeve : SLV : Spam Link Verification

Spam Link Verification system: an XML-RPC implementation to filter user input with spam links. ...

opensource download anymeta technical gpl php oauth mediamatic library google

oauth-php - Google Code

A PHP library for OAuth consumers and servers. Complete with an extensible OAuth store, includi...

OAuth Server And Consumer in PHP

Here is the full implementation of OAuth for anyMeta. For now we...

Verso Wiki: translate Wiki markup to HTML and HTML to Wiki markup

Verso Wiki is a Wiki to HTML and HTML to Wiki markup translator. We use this W...

Depcached - memcache(d) with dependencies

When using memcache we bumped into some problems. The major one was that we needed to invalidate...

OAuth - Added Body Signing

I just published a new version of our OAuth server and consumer code. The major addition to thi...

download opensource anymeta oauth mediamatic gpl technical php protocol authentication

oauth-php - Google Code

A PHP library for OAuth consumers and servers. Complete with an extensible OAuth store, includi...

OAuth Server And Consumer in PHP

Here is the full implementation of OAuth for anyMeta. For now we...

Depcached - memcache(d) with dependencies

When using memcache we bumped into some problems. The major one was that we needed to invalidate...

OAuth - Added Body Signing

I just published a new version of our OAuth server and consumer code. The major addition to thi...

html php wiki translator xhtml technical library opensource anymeta download

Verso Wiki: translate Wiki markup to HTML and HTML to Wiki markup

Verso Wiki is a Wiki to HTML and HTML to Wiki markup translator. We use this W...

GeSHi - Generic Syntax Highlighter

GeSHi - Generic Syntax Highlighter for php. Highlight many languages, including PHP, CSS, HTML, S...

MarcWorrell.com/ created on 2006-11-15 22:49:02/ modified on 2007-08-31 16:49:27/ mail me at