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.
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 }
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 }
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!";
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; }
Always use AND
rather than and
or &&
, and OR
rather than or
or ||
in your code.
if ($num1 AND $num2 OR $num3)
Similarly, AS
in foreach
statements should be capitalized.
foreach ($array AS $num => $var) { // code goes here }
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.
Custom names for functions you add should all adhere to the following naming conventions:
Prefix |
Description |
---|---|
|
Save data back to the database. |
|
Read data from the database and create a temporary PHP cache variables to reduce SQL database load. |
|
Return |
|
Return variables containing HTML. |
|
Convert the data format of input variables. |
|
Delete data from the database. |
|
Perform an action. |
|
Return arrays, strings, integers, etc. |
|
Deal directly with the file system. |
|
Called by the bbcode parser to deal with a specific bbcode type. |
|
Take an array of data and import it into the database. |
|
Return |
|
JavaScript functions defined within the PHP code. |
|
Append to the vBulletin logs. |
|
Initialize the bbcode parsing process. |
|
Print code out to browser or buffer. |
|
Prepare an array for later reference. |
|
Check and clear data (such as removing illegal characters) for later processing. |
|
Sort data. |
|
Strip elements from strings. |
|
Functions that undo a soft deletion. |
|
Replace built-in vBulletin PHP functions with replacements designed to increase functionality or alter the behavior of the built-in function. |
|
Check conditions and generate an error message if particular conditions are satisfied. |
|
Read or output XML. |