User Tools

Site Tools


organization:code_standards

This is an old revision of the document!


1) Format your code, please!

2) Use tabs for formatting, NOT SPACES. Tabs create smaller files and editors allow developers to view a tab as however many spaces as they prefer. Spaces do not allow this.

3) Use ' instead of “ for strings. This is a performance issue, and prevents a lot of inconsistent coding styles.

4) Comments go on the line ABOVE the code, NOT to the right of the code!

5) General comments style is PHPDocumentor

Commented the start of page

'' /** * Template processor class * * The template processor class allows keeping HTML code completely free of PHP code, but contain replacement fields. * The class provides you with functions which can fill in the replacement fields with arbitrary strings. * @package common */ ''
Commented each function as it is defined in PHPDocumentor

There are also single commenting styles // applied where it is needed.

And… Do not document every bit of code in comments.

7) Use switch statements where many elseif's are going to be used. Switch is faster!

8) 'If' statements need to use the following format:

      if ($var == 'example') {
	echo 'This is only an example';
} else {
	echo 'This is not a test.  This is the real thing';
}
Do NOT make if statements like this:
if ($var=='example'){ echo 'An example'; }
All other styles should not to be used.  This is it. Please use spaces before and after operators. 

9) ALL 'if' statements MUST have matching { } (brackets). Do NOT create 'if' statements like this:

''

if ($a == b)
	dosomething();

''

or:

''

if ($a == b) dosomething();

''

They make the code more difficult to read and follow.

10) class/function format: ''

class testing 
{
	function print_to_screen()
	{
		global phpgw, phpgw_info;
		if ($var == 'example') {
			echo 'This is only an example';
		} else {
			echo 'This is not a test.  This is the real thing';
		}
	}
}

'' 11) Associative arrays must be written in the following manner: ''

$array = array('var' => 'value',	'var2' => 'value2');

''

12) Use the long format for <?php. Do NOT use <?.

13) All code should start with no tab. Example:

<?php dosomething(); if ($a) {

dosomemorestuff();

}

?>

NOT:

<?php

dosomething();
if ($a) {
	dosomemorestuff();
}

?>

14) Use stubbly-case (mixed-case) code.

  Example: $currentUser is right, but $currentuser and $current_user are not.
Good function names are printLoginStatus(), getUserData()

15) Linefeeds: Ensure that your editor is saving files in the UNIX format. This means lines are terminated with a newline, not with a CR/LF combo as they are on Win32, or whatever the Mac uses.

16) SQL code layout: Here's a sample of how ideally SQL code should look. Note where the lines break, the capitalization, and the use of brackets.

 Examples:
	SELECT field1 AS something, field2, field3
	FROM (table a, table b)
	WHERE (this = that) AND (this2 = that2)
 This is valid too however:
	SELECT field1 AS something, field2, field3 FROM (table a, table b) WHERE (this = that) AND (this2 = that2)

ALWAYS enclose FROM tables into brackets – for LEFT JOIN compatability in MYSQL 5+.

SQL insert statements: SQL INSERT statements should be written LIKE UPDATE statemnets.

 Examples:
	# This is not what we want.
	INSERT INTO mytable
	VALUES ('something', 1, 'else')
	
	# This is correct.
	INSERT INTO mytable SET column1='something', column2=1, column3='else'
	

17) Don't use uninitialized variables. These errors can be avoided by using the built-in isset() function to check whether a variable has been set.

 Examples:
	// wrong 
	if ($forum) ...
	
	
	// correct
	if (isset($forum)) ...
	

18) Thanks for following these rules :) Please remember that those rules ARE STILL FLEXIBLE. If you work on existing code, you could vary your code styling to match existing code (if you think it makes sense).

Those rules are common for all non ZendFramework based projects. When the project is based on Zend Framework please follow Zend Framework code formatting!!!

комментарии в файлах

organization/code_standards.1400475207.txt.gz · Last modified: (external edit)