IceBB:Development/Coding style

From IceBB Wiki

Jump to: navigation, search

When contributing to IceBB, please use the following coding style. It helps keep IceBB's code clean and easy to modify.

Contents

File Formatting

  • Use tabs to indent. Do not use spaces.
  • Use standard Unix line termination (LF); do not the Windows or Mac style
  • All source files should be encoded in UTF-8

Naming Conventions

Please use the following naming conventions.

All names should be as descriptive as possible.

Functions, classes and variables

Names should be in lower case and new words separated by underscores (_). Exceptions are made for third-party classes that may be in use.

Constants

Should be in all upper case and new words separated by underscores (_).

Coding style

Comments

Comments should be on the same indentation level as the code that is being commented and should be in the C-style.

<?php
// I am a comment
if($food == 'potato')
{
	// the food is a potato
	echo "it's a potato";
}
?>

Use the phpdoc commenting style for all files in the includes/ folder. You may also use it elsewhere if you desire.

Control stuctures

These rules apply to "if", "else if", "else", "do", "while", "switch", "for" etc.

Please use curly brackets, even if not needed; this makes it easier to add additional code. However, lack of curly brackets is acceptable in some rare cases.

If, else if and else are written in the following way (note that it is "else if" and not "elseif"):

<?php
if($option == 1)
{
	echo "Option 1";
}
else if($option == 2)
{
	echo "Option 2";
}
else {
	echo "Invalid option";
}

The curly brackets (if on a new line), should be at the same indention level as the control structure.

An example of scenario where curly brackets are not needed could be:

<?php
echo "Counting...\n";
for($i=1; $i<=5; $i++)
{
	if($i == 2) continue;
	echo $i,"\n";
}

Ternary operator

You are allowed to use the ternary operator (?:). This often saves a bit of code, so please try to use it when possible. Example:

<?php
if(empty($bob))
{
	$bob = "this";
}
else {
	$bob = "that";
}

can be replaced with

<?php
$bob = empty($bob) ? "this" : "that";

Assignments

When assigning values to several variables in a row, you should use tabs to line up the assignment operators. Example:

<?php
// Single assignment
$var = 'value';

// Multiple assignments
$var       = 'value1';
$something = 'value2';

You may also line up the assignment operators even if the assignments are not together if you wish.

Arguments

When passing arguments to a function or defining them, spaces between the commas and equal signs are optional but strongly recommended. Do not put a space before the opening parenthesis.

Security

We maintain a high security standard for IceBB; it's in everyone's best interest that IceBB is secure. Please ensure that your code is secure to the best of your knowledge before committing.