Standards/CodingStandards

Coding Standards

This document goes on to explain some common coding standards Plogger developers should adhere by. This keeps code consistent and easy to read as well as making it easier for other developers to make changes and understand logic.

Indentation

Use indentation logically to make blocks of code more readable. Consider:

if ($a = $b) {
return 1;
} 
else if ($b = $c) {
if ($c = $d) {
return 3;
}
else {
return 2;
}
}

versus:

if ($a = $b) {
    return 1;
} 
else if ($b = $c) {
    if ($c = $d) {
        return 3;
    }
    else {
        return 2;
    }
}

The use of hard (real) tabs is preferred over soft tabs because it has more support across clients.

Single / Double Quotes

If nothing is being evaluated, use single quotes... it saves time.

$config['key']

Line Length

Limit lines to 80 characters if possible so that there are no word-wrap issues. 80 is considered the standard line length on a UNIX terminal. Lines can be split on conditions or function parameters, etc.

if ($month == 'september' ||
    $month == 'april' ||
    $month == 'june' ||
    $month == 'november')
{
    return 30;
}

Whitespace

Whitespace should be used to logically break chunks of code as well as align to make more readable.

$john = 1;
$randy = 2;
$tom = 3;
$michael = 4;
$suzieq = 5;
$jennifer = 6;
$john    = 1;
$randy   = 2;
$tom     = 3;
$michael = 4;

$suzieq   = 5;
$jennifer = 6;

Whitespace should also be used around operators.

Use:

$foo = $bar;

Not:

$foo=$bar;

Brace Style

K&R style braces should be used for all code. Using the classic K&R style for function and class declarations makes it easier to spot them in the code.

For control structure:

if ($condition) {
    // statement
}

For function and class declarations:

function print_hello($name) {
    echo "Hello $name";
}

Naming Consistency

Variable names should show consistency among them. If a common word is shortened in a variable name, that same shortened version should be used elsewhere throughout the code.

Multiword Names

Use underscore to split words instead of caps (eg. CamelCase).

$really_cool_var = "Awesome";

Variable Names

Variables should be named all lowercase. Global variables and constants should be all caps. i, j, k, l, m, and n should be used for temporary variables.

$temp_var = 1;
$GLOBAL_VAR = 2;

Function Names

Function names should be all lowercase.

Class Names

  • The first letter of a class name should be capitalized
  • Underscores should be used to simulate nested namespaces
  • Multiword class names should be concatenated, and the first letter of each word should be capitalized (CamelCaps?).
class XML_RSS {}
class Text_PrettyPrinter {}

Method Names

Concatenate words and capitalize the first letter of every word after the first.

myMethodName ()

Variable / Schema Names

Variable names associated with database records should have matching names.

$query = "SELECT name,
                 filename,
                 id
          FROM pictures";
$results = mysql_query($query);
while(list($name, $filename, $id) = mysql_retch_row($results)) {
    // ...
}

SQL

  • Capitalize SQL parts like SELECT and FROM
  • Break lines on keywords
  • Use table aliases to keep code clean
$query = "SELECT name,
                 file_path
          FROM pictures p,
               user u
          WHERE p.user_id = u.user_id";

include_once / require_once

Use as appropriate. include_once produces a warning on failure, while require_once results in a fatal error. Fatal errors stop code execution. Also, if you use _once it will not be included or required again if it has already been called.

Long PHP Tags

To avoid parsing errors, make sure to always use long PHP tags.

<?php ?>

Not:

<? ?>