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.
A simple guideline here.
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.
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.
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;
}
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.
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:
When it is a simple or or and condition:
if (!$type || $type == 4 || $type == 13)
{
$is_pic = 0;
}
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.
A PHP library for OAuth consumers and servers. Complete with an extensible OAuth store, includi...
All Internet applications have to secure their inner workings against attacks from outside. We a...
Here is the full implementation of OAuth for anyMeta. For now we...
There is a new and better version of this memcached clone. Please check ou...
Verso Wiki is a Wiki to HTML and HTML to Wiki markup translator. We use this W...
When using memcache we bumped into some problems. The major one was that we needed to invalidate...
The PHP JPEG metadata toolkit is a rather complete library to read the EXIF data from digital cam...
I just published a new version of our OAuth server and consumer code. The major addition to thi...
Spam Link Verification system: an XML-RPC implementation to filter user input with spam links. ...
A PHP library for OAuth consumers and servers. Complete with an extensible OAuth store, includi...
Here is the full implementation of OAuth for anyMeta. For now we...
Verso Wiki is a Wiki to HTML and HTML to Wiki markup translator. We use this W...
When using memcache we bumped into some problems. The major one was that we needed to invalidate...
I just published a new version of our OAuth server and consumer code. The major addition to thi...
A PHP library for OAuth consumers and servers. Complete with an extensible OAuth store, includi...
Here is the full implementation of OAuth for anyMeta. For now we...
When using memcache we bumped into some problems. The major one was that we needed to invalidate...
I just published a new version of our OAuth server and consumer code. The major addition to thi...
Verso Wiki is a Wiki to HTML and HTML to Wiki markup translator. We use this W...
GeSHi - Generic Syntax Highlighter for php. Highlight many languages, including PHP, CSS, HTML, S...