Code Style Guide

From CTRNet Wiki
Jump to: navigation, search

Code conventions are important to programmers for a number of reasons:

  • 80% of the lifetime cost of a piece of software goes to maintenance.
  • Hardly any software is maintained for its whole life by the original author.
  • Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.
  • If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create.

Contents

General Formating

Tab Size - 3 spaces Column Width - 100 characters

Function and Class declarations

Function definition

function foo($param = null, $additionalParams = array()) {
   // CODE HERE
}

Class definition

class Dispatcher extends Object {
   // CODE HERE
}

Control Structures

if (($a == 1) && ($b == 2)) {
  echo 'A was equal to 1';
  //easily add more code
} elseif (($a == 1) && ($b == 3)) {
  //do something
}

Commenting

/**
 *	short description of function
 *
 *	Optional more detailed description.
 *
 *	@param $paramName - type - brief purpose
 *	@param ...
 *	...
 *	@return type and description
 */

Strings

$associative_array['name'];
$var = 'My String';
$var2 = 'Very... long... string... ' . $var . ' ...more string... ';
$sql = "INSERT INTO mytable (field) VALUES ('$var')";

Constants and TRUE/FALSE

Constants should always be fully uppercase.

MY_CONSTANT
NEWLINE
SUPER_CLASS_VERSION

TRUE, FALSE keywords should always be fully uppercase as well.

while ($foo == TRUE) {
   $bar = FALSE;
}
Personal tools