We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

coding_standards.txt 3.49 KB
Newer Older
elleo's avatar
elleo committed
1 2
Libre.fm Coding Standards (Adapted from phpGroupWare standards)

clint's avatar
clint committed
3
Please comply with the following standard when working on Libre.fm if you
elleo's avatar
elleo committed
4 5 6 7 8 9 10 11
want your patches accepted and modules included in supported releases.

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 - we use 4 spaces.
	Spaces do not allow this.

clint's avatar
clint committed
12 13
3)	Use ' instead of " for strings, where substitutions aren't required.  This is a
	performance issue, and prevents	a lot of inconsistent coding styles.  When using
elleo's avatar
elleo committed
14 15 16
	substitutions, use curly braces around your variables - like so:
	$var = "my_var: {$my_var}";

clint's avatar
clint committed
17
4)	Comments go on the line ABOVE the code, NOT to the right of the code, unless it
elleo's avatar
elleo committed
18 19 20 21
	is very short.

5)	All functions and methods are to be documented using PhpDocumentor - http://phpdoc.org

22
6)	Use switch statements where many else if's are going to be used.  Switch/case is faster
elleo's avatar
elleo committed
23

24
7)	'If' statements need to use the following format:
elleo's avatar
elleo committed
25

26
	if ($var == 'example') {
elleo's avatar
elleo committed
27
		echo 'This is only an example';
28
	} else {
elleo's avatar
elleo committed
29 30 31 32 33 34 35 36 37 38 39 40
		echo 'This is not a test.  This is the real thing';
	}

	Do NOT make if statements like this:

	if ($var == 'example'){ echo 'An example'; }

	OR this

	if($var = 'example')
		echo "An {$var}";

41
8)	class/function format:
elleo's avatar
elleo committed
42 43 44 45

	/**
	 * This class is for testing
	 */
46
	class ModuleTesting {
elleo's avatar
elleo committed
47
		/**
48
		 * Output the value of $my_var the user
elleo's avatar
elleo committed
49
		 */
50
		public function printToScreen()	{
elleo's avatar
elleo committed
51
			$my_var = new Monkey();
52
			if ($my_var->name == 'example')	{
elleo's avatar
elleo committed
53
				echo 'This is only an example';
54
			} else {
elleo's avatar
elleo committed
55 56 57 58 59 60 61
				echo 'This is not a test.  This is the real thing';
			}
		}
	}

10)	Associative arrays must be written in the following manner:

62
	$array = array (
63 64
		'var'   => 'value',
		'var2'  => 'value2'
elleo's avatar
elleo committed
65 66
	);

67
	Note that spaces are preferred around the '=>'.
elleo's avatar
elleo committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

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

12)	a) Classes begin with a capital letter and use CamelCase for separating words (e.g. MyClass).
	b) Functions/Methods start with a lower case letter and use CamelCase for separating words (e.g. myFunction).
	c) Variables are all lower case and use _ for separating words (e.g. my_variable).

13)	Always use symbol based comparison operators (&&, ||) instead of text based
	operators (AND, OR) as they are evaluated in different orders and at different
	speeds.  This is will prevent any confusion or strange results.

14)	You code must run with E_ALL error reporting turned on, E_NOTICES are ERRORS!
	Where possible your code should run with E_STRICT error reporting.

15)	All variables, classes, methods, functions and comments must be in English.
	Bad english is easier to work with than having to babelfish code to work out
	how it works.

86 87
16)	Files should be in either ASCII or UTF-8 encoding with UNIX line endings.

clint's avatar
clint committed
88
17)	Files should not end with an ending php tag "?>".  Any whitespace after
89 90 91
	the closing tag is sent to the browser and cause errors, so don't include
	them.

92 93 94 95
18)	Translatable strings in templates should be surrounded by a {t} block,
	(e.g. {t}Translate me!{/t}). See the smarty gettext documentation for more
	advanced usage.

96 97 98 99 100 101 102
19)	When comparing strings in lower-case always use the DBMS lower() function
	where possible. strtolower() doesn't handle multi-byte characters and
	and mb_strtolower is typically much less efficient. Since this is typically
	used when comparing against a lower()'d database field it also ensures
	consistency in the comparison.

20)	If you see code which doesn't comply with the above, please fix it :)