egroupware/phpgwapi/doc/coding_standard.txt

131 lines
3.4 KiB
Plaintext
Raw Normal View History

2001-05-23 00:27:41 +02:00
1) Format your code so that we can read it, 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.
2002-05-12 00:10:48 +02:00
There is one exception (see #11 below).
2001-05-23 00:27:41 +02:00
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) For each section of code put a section divider with basic explanation of the following
code/functions. It should look like this:
2001-05-21 04:49:16 +02:00
/****************************************************************************\
* These functions are used to pick my nose *
\****************************************************************************/
2001-05-23 00:27:41 +02:00
6) Do not document every bit of code in comments. PHP is an interpreted language and it will be
nasty on performance.
2001-05-23 00:27:41 +02:00
7) Use switch statements where many elseif's are going to be used. Switch is faster and I like it
2001-05-21 04:49:16 +02:00
better!
2001-05-23 00:27:41 +02:00
8) 'If' statements need to use the following format:
2001-05-19 05:59:04 +02:00
if ($var == 'example')
{
echo 'This is only an example';
}
else
{
2001-05-23 00:27:41 +02:00
echo 'This is not a test. This is the real thing';
2001-05-19 05:59:04 +02:00
}
2001-05-23 00:27:41 +02:00
Do NOT make if statements like this:
2001-05-19 05:59:04 +02:00
if ($var == 'example'){ echo 'An example'; }
2001-05-23 00:27:41 +02:00
All other styles are not to be used. This is it. Use it or I will personally come and nag you to
death.
2001-05-21 04:49:16 +02:00
2001-05-23 00:27:41 +02:00
9) ALL 'if' statements MUST have matching { } (brackets). Do NOT create 'if' statements like this:
2001-05-23 00:27:41 +02:00
if ($a == b)
dosomething();
or:
if ($a == b) dosomething();
They make the code more difficult to read and follow.
10) class/function format:
2001-05-19 05:59:04 +02:00
class testing
{
function print_to_screen()
{
2003-12-05 14:26:55 +01:00
if($var == 'example')
2001-05-19 05:59:04 +02:00
{
echo 'This is only an example';
}
else
{
2001-05-23 00:27:41 +02:00
echo 'This is not a test. This is the real thing';
2001-05-19 05:59:04 +02:00
}
}
}
2001-05-23 00:27:41 +02:00
11) Associative arrays must be written in the following manner:
2003-12-05 14:26:55 +01:00
$array = array(
'var' => 'value',
'var2' => 'value2'
);
OR:
2002-11-18 23:09:01 +01:00
$array = array
(
2003-12-05 14:26:55 +01:00
'var' => 'value',
'var2' => 'value2'
2001-05-21 04:49:16 +02:00
);
2003-12-05 14:26:55 +01:00
Note that spaces are preferred around the '=>'. This is because only tabs
on the left side are guaranteed to line up correctly using different
tabstops.
2001-05-23 00:27:41 +02:00
12) Use the long format for <?php. Do NOT use <?.
2001-05-21 04:49:16 +02:00
2001-05-23 00:27:41 +02:00
13) All code should start with 1 tab. Example:
2001-05-21 04:49:16 +02:00
<?php
dosomething();
if ($a)
{
dosomemorestuff();
}
2001-05-23 00:27:41 +02:00
NOT:
2001-05-21 04:49:16 +02:00
<?php
dosomething();
if ($a)
{
dosomemorestuff();
}
2001-05-23 00:27:41 +02:00
14) Use lower case for variable and function names. No stubbly-case (mixed-case) code.
2001-05-21 04:49:16 +02:00
2003-12-05 14:26:55 +01:00
15) (int)$var is preferred vs. intval($var). Also, is_int()/is_string()/is_array()
should be used instead of gettype() where possible.
2003-12-05 15:15:25 +01:00
16) str_ functions should be used instead of ereg_ for simple text replacement, etc.
For example, ereg_replace(';','',$string) is much slower than str_replace(';','',$string.
Of course, for complicated regular expressions, you may still need ereg_.
17) Use the api function, copyobj($oldobject,$newobject), instead of
2003-12-05 14:26:55 +01:00
$newobject = $oldobject. This is for performance when using php5.
2003-12-05 15:15:25 +01:00
18) Try to avoid creating new objects when the api-created ones will work.
2003-12-05 14:26:55 +01:00
This is a performance issue. You might also be able to use copyobj() and then
call the constructor of the class if another version of the object exists
already.
2003-12-05 15:15:25 +01:00
19) Do not use, e.g., global $var; unless absolutely necessary. Please
2003-12-05 14:26:55 +01:00
consider developing with register_globals=off to understand why.
2003-12-05 15:15:25 +01:00
20) Thanks for following these rules :)