Programming Standards

Good programming is clear, unambiguous programming. Let's run through a few tips to help make your code more readable, easier to debug, and easier to make changes to later on.

Braces

For clarity each brace should be placed on its own line in the code:

if ($condition)
{
// code goes here
}

Don't be tempted to condense code like this:

if ($condition)
{ // code goes here }

Indenting

Indent code between braces:

if ($condition)
{
// code goes here
}

Code between braces within braces should have deeper indenting:

if ($condition)
{
if ($condition2)
{
// code goes here
}
// some more code goes here
}

Give Operators Space

All operators (except -- and ++) should have a space either side.

$a = $b + $c;

String Quoting

All strings should be quoted with single quotes when they don't contain variables or control characters. Otherwise always use double quotes:

$a = 'Hello, World!';
$b = "Hello,
World!";
$c = "$hello,
World!";

Return Values

Use only lower-case true and false for return values. Upper-case should be reserved for custom constants.

if ($condition)
{
return true;
}
else
{
return false;
}

AND and OR

Always use AND rather than and or &&, and OR rather than or or || in your code.

if ($num1 AND $num2 OR $num3)

AS

Similarly, AS in foreach statements should be capitalized.

foreach ($array AS $num => $var)
{
// code goes here
}

SQL Queries

Under all circumstances, make sure that you double quote all SQL queries.

$DB_site->query("SELECT field FROM " . TABLE_PREFIX . "table ORDER BY field");

It may be better to write long queries on more than one line.

Naming Conventions for Functions

Custom names for functions you add should all adhere to the following naming conventions:

Prefix

Description

build_

Save data back to the database.

cache_

Read data from the database and create a temporary PHP cache variables to reduce SQL database load.

can_

Return true or false based on permissions.

construct_

Return variables containing HTML.

convert_

Convert the data format of input variables.

delete_

Delete data from the database.

exec_

Perform an action.

fetch_

Return arrays, strings, integers, etc.

file_

Deal directly with the file system.

handle_

Called by the bbcode parser to deal with a specific bbcode type.

import_

Take an array of data and import it into the database.

is_, contains_

Return true or false based on conditions.

js_

JavaScript functions defined within the PHP code.

log_

Append to the vBulletin logs.

parse_

Initialize the bbcode parsing process.

print_

Print code out to browser or buffer.

process_

Prepare an array for later reference.

sanitize_

Check and clear data (such as removing illegal characters) for later processing.

sort_

Sort data.

strip_

Strip elements from strings.

undelete_

Functions that undo a soft deletion.

vb_, vb

Replace built-in vBulletin PHP functions with replacements designed to increase functionality or alter the behavior of the built-in function.

verify_

Check conditions and generate an error message if particular conditions are satisfied.

xml_

Read or output XML.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset