Skip to content
ramaboo edited this page Jan 14, 2012 · 11 revisions

Coding Standards

Naming Conventions

Boo Prefix

Because of the lack of namespace support in PHP 5.2.x, class names, constants, and global variables are prefixed with Boo_, BOO_, and boo_, respectively.

Class Names

Classes are named using UpperCamelCase and start with 'Boo'. The underscore is to denote the path separator; the file Boo/classes/Html/Form.php must map to the class name Boo_Html_Form

/* CORRECT */
Boo_Html_Form // defined in Boo/classes/Html/Form.php
Boo_Error // defined in Boo/classes/Error.php

/* INCORRECT */
Boo_XML // not UpperCamelCase
BooMyClass // no underscore

Method Names

Methods are named using lowerCamelCase.

/* CORRECT */
getUsernameById()
open()
filterInput()

/* INCORRECT */
get_username_by_id()
Open()
FilterInput()

The use of getters and setters is strongly encouraged. Private and protected method names do not start with an underscore as is common in other frameworks.

Function Names

Functions are named using lower case letters separated by the underscore.

/* CORRECT */
function get_user_properties() // correct

/* INCORRECT */
userproperties()
userProperties()
getuserproperties()
getFileProperties()
get_user_properties_from_given_user() // to wordy

/* BETTER */
Boo_User::getProperties() // static method

Functions may only be defined within files located in the functions directory. In practice Boo uses very few functions. The use of static class methods is strongly preferred for all but the simplest tasks. If you can't envision the function being incorporated into PHP itself then you have done something wrong. Only SuperglobalFunctions may start with the underscore.

Constant Names

Constants are always upper-case and start with 'BOO'. Words are separated by the underscore. Constants should only be defined within files located in the config directory.

/* CORRECT */
BOO_SMTP_PORT

/* INCORRECT */
BOO_SMTPPORT //

You must check to ensure the end user has not defined a constant before defining it.

/* CORRECT */
if (!defined('BOO_SMTP_PORT')) { define('BOO_SMTP_PORT', 25); }

/* INCORRECT */
define('BOO_SMTP_PORT', 25);

This allows the end user to override the value of any constant while providing a reasonable default value.

define('BOO_SMTP_PORT', 465); // Gmail uses 465
require_once('PATH_TO/Boo.php');

Variables

Most variables in Boo are lower-case and separated by an underscore. The use of global variables is strongly discouraged. If absolutely necessary they must be prefixed with 'boo'.

/* CORRECT */
$first_name
$i // only for loops

/* INCORRECT */
$fn // to short
$first-name

Public Class Variables

Public class variables are named using lowerCamelCase if they represent an object.

/* CORRECT */
class Boo_Page {
     $msgError;

     public function __construct() {
          $this->msgError = new Boo_Dialog_MessageBox;
     }

}

/* INCORRECT */
class Boo_Page {
     $msg_error;
     $msgSuccess = 5; // not an object

     public function __construct() {
          $this->msgError = new Boo_Dialog_MessageBox;
     }

}

Code Documentation

Boo uses Doxygen to generate its API docs. A list of Doxygen commands is available here.

File Formating

Indentation

Tabs not spaces, always!

Whitespace

Boo was written in Eclipse with white-space characters enabled. Please keep the white-space clean of extra spaces/tabs at the end of lines.

Maximum Line Length

80 characters if you can please. No more than 120 (and you better have a good reason).

Line Termination

Line termination follows the Unix text file convention. Lines must end with a single linefeed character (0x0A). Mac and Windows users please don't screw up the files.

PHP Tags

Use full PHP tags.

/* CORRECT */
<?php echo $foo; ?>

/* INCORRECT */
<? echo $foo; ?>
<?=$foo?> 

If the file contains only PHP code the end tag is not permitted.

True, False, and Null

Within code use the lower-case versions only. Use upper-case in documentation.

/* CORRECT */
/**
 * @return bool Returns TRUE.
 */
function foo() { return true; }

Compatibility

Code must pass E_STRICT and work with PHP 5.2.x.

Data Types

Strings

The use of single quotes is strongly encouraged unless the string contains apostrophes or another variable.

/* CORRECT */
$name = 'john';
$sql = "SELECT * FROM users WHERE username='john'"; // ok

/* INCORRECT */
$name = "john"; // no reason for double quotes

Variable substitution is permitted in the following form only.

/* CORRECT */
$greeting = "Hello {$name}!";

/* INCORRECT */
$greeting = "Hello ${name}!";
$greeting = "Hello $name!";

Strings must be concatenated with the "." operator as follows.

/* CORRECT */
$name = $first . ' ' . $last;

/* INCORRECT */
$name = $first.' '.$last;

Multi line concatenations are as follows.

/* CORRECT */
$long_string = 'this is the'
     . 'start of a very,'
     . 'very, very, very,'
     . 'long string';

Arrays

Arrays should be comma delimiter with a trailing space. Associative arrays should contain spaces around the "=>" operator. Whenever possible arrays should have plural names.

/* CORRECT */
$names = array('john', 'david', 'paul');

/* INCORRECT */
$name = array('john','david','paul');

Multi line arrays should be indented as follows.

/* CORRECT */
$fruits = array(
     'a' => 'apple',
     'b' => 'banana',
     'c' => 'carrot');

Efficiently

In general Boo prefers elegant code to efficient code.

Here are a few common case that should be avoided.

For Loops
/* CORRECT */
$foo = foo();
for ($i = 0; $i < $foo; $i++) {
    // do something
}

/* INCORRECT */
for ($i = 0; $i < foo(); $i++) {
    // foo() is call unnecessary
}

Typecasting

/* CORRECT */
$number = (int) $number;

/* INCORRECT */
$number = intval($number);