Blog

Revised Code Formatting Rules for 2.0+

After discussion with @logikal16, here are the new rules we’ll be enforcing on the Pods codebase.

  • Shorthand

    Shorthand can get messy, so it’s permitted in simple cases. Here are the examples where Shorthand usage is permitted.

    if (0 < $variable || false !== $variable)
        $do = 'something';
    else
        $do = 'something';

    and

    $do = (0 < $variable || false !== $variable) ? 'something' : 'something else';

    Both cases above are permitted, but the second example should be limited to two conditionals, and no nested shorthand. If it does not meet that criteria, the first example should be used. Brackets should be used instead of shorthand conditionals if the conditional is complex with three or more conditions, or needs to contain more than one line of code within the statement.

    In no cases are shorthand PHP opening tag usage allowed, so the following example is a no-no.

    <? echo 'Shorthand opening PHP tags are not allowed'; ?>

  • Arrays

    Simple arrays can be on one line, while complex arrays with many variables should be placed on the next line (after the preceding comma) for each new variable for better readability.

    $simple_array = array('key' => 'value', 'key2' => 'value2');
    $complex_array = array('key' => 'value',
                        'long_key' => 'value2',
                        'key2' => $var,
                        'key3' => $var2);

    Arrays used within functions should be placed into a $variable beforehand in all cases for greater readability.

    $params = array('key' => 'var');
    do_something($params);

  • Function Definitions

    Function naming should have a space between the function name, and each variable.

    function my_function_name ($variable1 = null, $variable2 = 'name')

  • Conditionals

    When comparing a variable against a non-variable, the non-variable should appear first, and the variable second. In this case, the non-variable is a boolean value.

    if (false !== $variable)
        $do = 'something';

    In this next cases, and only in these cases, the values are compared to an integer, which should always follow smaller to larger.

    if (0 < $variable)
        $do = 'something';
    elseif ($variable < 1)
        $do = 'something else';

    In addition, a space should be place between the conditional name and the conditions, like in the cases above with “if” and “elseif” and the code within the parenthesis.

  • Spacing

    Spacing is important for readability and consistency. Every time there’s a . or comma in the code, we should separate with a space in the following ways.

    $variable = my_function_name('Awesome', 'slug');
    $variable = 'Totally' . my_function_name('Awesome', 'slug');

    Operators, especially within conditionals, should be spaced properly too, with one space on each side.

    if (0 != $var)
        $var = $other_var + 5;

    At the end of every PHP file, if PHP was not closed, it should not be closed to prevent whitespace from being sent on certain servers when people upload / download / edit from multiple systems with different linebreak settings.

    <?php
    $code = 'here';
    ..............etc..........
    echo $last_line;

  • Brackets

    Brackets should be spaced by one space between the parenthesis and the bracket. It should appear on the same line as the conditional or function name before it. The closing bracket should be tabbed so that it matches the depth of the start of the conditional, and on it’s own line. In no cases should the bracket share a line with other code, even if followed by an else statement.

    if (false !== $value) {
        $do = 'something';
    }

  • Commenting

    Implement PHPDoc above all functions, and comment complex sections of a function where it’s not inherently obvious what that section does.