Open Source DKP

EQdkp Core Developer Manual
EQdkp Coding Standards

This document describes the coding standards used in the core EQdkp code and is not necessarily applicable to plugins, but it would be preferrable.

Text Editors

Recommended for Windows: PHPEdit.

Use whatever editor you prefer.

Editor Settings

Tabs vs. Spaces: All spaces, no tabs. 4 spaces per indentation.

Naming Conventions

Variable Names: Variable names should be all lowercase, with words separated by an underscore. "$variable_name" is right, "$variablename" and "$variableName" are not.

Loop Indices: The only situation where a one-character variable name is allowed is when it's the index for a looping construct.

Function Names: Functions should be named descriptively while not being 50+ characters long.

Function Arguments: Subject to the same guidelines as variable names.

The basic idea here is to not sacrifice code clarity for the sake of laziness.

Code Layout

Standard CVS Header: This comment block is placed at the top of every file in EQdkp's core.

/******************************
* EQdkp
* Copyright 2002-2003
* Licensed under the GNU GPL.  See COPYING for full terms.
* ------------------
* filename.php
* Began: Thu March 27 2003
*
* $Id: Exp $
*
******************************/
?>
Braces: Always include the braces. Typing two extra characters greatly improves code clarity.

// The following are all wrong:

if (condition) do_stuff();
if (
condition)
    
do_stuff();
    
while (
condition)
    
do_stuff();
?>
Brace Position: This is another issue of personal preference, but in the case of EQdkp, please put each brace on its own line, it makes it much easier to tell where each block starts and stops when you can line up the start and end braces.

// Use:
if (condition)
{
    
do_stuff();
}              
else
{
    
do_stuff();
}

// Do NOT use:
if (condition) {
    
do_stuff();
} else {
    
do_stuff();
}
?>
Single/Double quotes: Unless you need to use double quotes, use single quotes.

// Wrong:
$var = "This is a string that incorrectly uses double quotes when single quotes would suffice.";
preg_replace("/is/", "was", $var);
?>
Use spaces and parenthesis to group conditions:

// Wrong:
if (condition)
elseif (
condition && condition)
while (
condition)
foreach(
condition)

// Right:
if ( condition )
elseif ( (
condition) && (condition) )
while (
condition )
foreach (
condition )

// Each pair shows the wrong way followed by the right way:
$i=0;
$i = 0;

if (
$i<7 ) ...
if (
$i < 7 ) ...
      
if ( (
$i < 7)&&($j > 8) ) ...
if ( (
$i < 7) && ($j > 8) ) ...
      
do_stuff( $i, "foo", $b );
do_stuff($i, "foo", $b);
      
for(
$i=0; $i<$size; $i++) ...
for (
$i = 0; $i < $size; $i++) ...
      
$i=($j < $size)?0:1;
$i = ($j < $size) ? 0 : 1;

function
function_name($a,$b,$c) ...
function
function_name($a, $b, $c) ...
?>
Notice that PHP keywords (if, for, foreach, while, else, elseif, etc.) have a space between the word and the parenthesis, whereas function calls do not.

SQL code layout: SQL keywords should be in uppercase ("SELECT field1 AS something FROM table WHERE (this = that)") and should be on their own line for large queries.

$sql = 'SELECT r.member_name
        FROM '
. RAID_ATTENDEES_TABLE . " r, " . MEMBERS_TABLE . " m
        WHERE m.member_name = r.member_name
        AND raid_id = '"
. $raid_id . "'
        ORDER BY member_name"
;
?>
Always include field names for INSERT statements. Plugins may want to add a field to an existing table instead of creating an entirely new table, and doing so would break SQL statements that don't specify the fields.

// Wrong:
$sql = 'INSERT INTO table VALUES ($field1, $field2, $field3)';

// If it's too much to type, you can use the SQL_DB::build_query method:
$query = $db->build_query('INSERT', array(
    
'field1' => $field1,
    
'field2' => $field2,
    
'field3' => $field3)
);
$sql = 'INSERT INTO table ' . $query;
?>
Commenting: Use helpful comments wherever possible - even if the logic is obvious to you but might confuse someone else, include comments. Do not comment just for the sake of commenting, however.

// BAD COMMENTING:

// if var1 is less than var2...
if ( $var1 < $var2 )
?>
For examples of the desired level of commenting, see admin/addraid.php and includes/eqdkp_plugins.php.

Ternary operator: The ternary operator is used extensively throughout EQdkp's core code. It should be formed in a similar way to an if-else block and should not go more than 2 levels deep:

// Wrong:
$var = expression ? 'if' : 'else';
$var = ( expression ) ? 'if' : (( elseif ) ? 'if' : 'else') : (( elseif ) ? 'if' : 'else') : 'else';

// Right:
$var = ( expression ) ? 'if' : 'else';
?>
Server Settings/Requirements

EQdkp should be able to run with register_globals set to off and error reporting set to E_ALL (notices, warnings, and errors). This means that variables must be initialized before being used/checked. Use isset() and empty() where necessary.

The minimum requirement for EQdkp is PHP 4.1.2 and should stay that way until more hosts are upgraded. Do not use a function that is only available in 4.2.x or 4.3.x - check the manual first.