Update:Archive/2.0/Developers/Best Practices
Update: Home Page » Development »
Introduction
The purpose of this section is to define code style and methodology to ensure consistency throughout the project.
Formatting
Coding standards should be chosen, not argued about.
We have the responsibility to ensure consistency across the project, which is the primary goal behind establishing a set of guidelines for development.
Not many PHP projects have been undertaken under the Mozilla umbrella. There is no precedent for PHP, so it is in our best interests to choose a widely accepted standard for PHP programming.
By choosing a widely accepted PHP standard, we take a step in the right direction regarding readability, extensibility, ease of development, etc. Most people in the PHP community will be familiar with the syntax, making future developers feel 'at home' when they jump on board.
With the understanding that the Mozilla style guide (which is an interim guide and "not set in stone") recommends 2-space indentation (and items such as a modeline for EMACS), it is still in the best interests of the project to choose the PEAR Coding Standards as our primary style guide.
An additional constraint is the need for an 80 column limit on line length.
Coding Methods
Do
- Create functions for anything you will do more than once.
- Create comment blocks before functions in phpDoc format.
- Create meaningful to-the-point inline comments when necessary.
- Clean and validate all inputs to prevent SQL injection or malignant HTML.
- Use meaningful variable and function names.
- Use CSS for all styles.
- Create validated markup according to 4.01 Strict.
Don't
- Create functions that do not return anything and echo/print HTML.
- Use variable or function names that are ambiguous (like $var or validate()).
- Create unnecessary functions.
- Use <? ?> to designate a PHP block.
- Use $array[foo] or $array["foo"] unless it is appropriate (constant or a variable catted with a string).
- Loop MySQL queries.
- Use GET variables in MySQL queries without cleaning them.
- Pass SID via GET.
- Use inline styles. Ever.
- Duplicate DOM IDs.