coding_standards.txt 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. Libre.fm Coding Standards (Adapted from phpGroupWare standards)
  2. Please comply with the following standard when working on Libre.fm if you
  3. want your patches accepted and modules included in supported releases.
  4. 1) Format your code so that we can read it, please!
  5. 2) Use tabs for formatting, NOT SPACES. Tabs create smaller files and editors allow
  6. developers to view a tab as however many spaces as they prefer - we use 4 spaces.
  7. Spaces do not allow this.
  8. 3) Use ' instead of " for strings, where substitutions aren't required. This is a
  9. performance issue, and prevents a lot of inconsistent coding styles. When using
  10. substitutions, use curly braces around your variables - like so:
  11. $var = "my_var: {$my_var}";
  12. 4) Comments go on the line ABOVE the code, NOT to the right of the code, unless it
  13. is very short.
  14. 5) All functions and methods are to be documented using PhpDocumentor - http://phpdoc.org
  15. 6) Use switch statements where many else if's are going to be used. Switch/case is faster
  16. 7) 'If' statements need to use the following format:
  17. if ($var == 'example') {
  18. echo 'This is only an example';
  19. } else {
  20. echo 'This is not a test. This is the real thing';
  21. }
  22. Do NOT make if statements like this:
  23. if ($var == 'example'){ echo 'An example'; }
  24. OR this
  25. if($var = 'example')
  26. echo "An {$var}";
  27. 8) class/function format:
  28. /**
  29. * This class is for testing
  30. */
  31. class ModuleTesting {
  32. /**
  33. * Output the value of $my_var the user
  34. */
  35. public function printToScreen() {
  36. $my_var = new Monkey();
  37. if ($my_var->name == 'example') {
  38. echo 'This is only an example';
  39. } else {
  40. echo 'This is not a test. This is the real thing';
  41. }
  42. }
  43. }
  44. 10) Associative arrays must be written in the following manner:
  45. $array = array (
  46. 'var' => 'value',
  47. 'var2' => 'value2'
  48. );
  49. Note that spaces are preferred around the '=>'.
  50. 11) Use the long format for <?php. Do NOT use <?.
  51. 12) a) Classes begin with a capital letter and use CamelCase for separating words (e.g. MyClass).
  52. b) Functions/Methods start with a lower case letter and use CamelCase for separating words (e.g. myFunction).
  53. c) Variables are all lower case and use _ for separating words (e.g. my_variable).
  54. 13) Always use symbol based comparison operators (&&, ||) instead of text based
  55. operators (AND, OR) as they are evaluated in different orders and at different
  56. speeds. This is will prevent any confusion or strange results.
  57. 14) You code must run with E_ALL error reporting turned on, E_NOTICES are ERRORS!
  58. Where possible your code should run with E_STRICT error reporting.
  59. 15) All variables, classes, methods, functions and comments must be in English.
  60. Bad english is easier to work with than having to babelfish code to work out
  61. how it works.
  62. 16) Files should be in either ASCII or UTF-8 encoding with UNIX line endings.
  63. 17) Files should not end with an ending php tag "?>". Any whitespace after
  64. the closing tag is sent to the browser and cause errors, so don't include
  65. them.
  66. 18) Translatable strings in templates should be surrounded by a {t} block,
  67. (e.g. {t}Translate me!{/t}). See the smarty gettext documentation for more
  68. advanced usage.
  69. 19) When comparing strings in lower-case always use the DBMS lower() function
  70. where possible. strtolower() doesn't handle multi-byte characters and
  71. and mb_strtolower is typically much less efficient. Since this is typically
  72. used when comparing against a lower()'d database field it also ensures
  73. consistency in the comparison.
  74. 20) If you see code which doesn't comply with the above, please fix it :)