Code

Fix regression in #4271
[gosa.git] / trunk / gosa-core / CODING
1 GOsa coding guidelines
2 ======================
4 * Scope of style guidelines
6 In order to keep the code consistent, please use the following conventions.
7 These conventions are no judgement call on your coding abilities, but more
8 of a style and look call.
11 * Indentation and line length
13 As a basic style rule, please use spaces instead of tabulators. This will
14 remove problems when using "diff" and avoid unneeded commits when using
15 "subversion".
17 For VI users, this can be achieved by the following settings:
19 8<----------------------------------------------------------------------------
20 set expandtab
21 set shiftwidth=4
22 set softtabstop=4
23 set tabstop=4
24 8<----------------------------------------------------------------------------
26 The line length should not exceed 80 characters. There is one exception for
27 i18n strings that must not be split for gettext.
30 *  Performance and Readability 
32 It is more important to be correct than to be fast. 
33 It is more important to be maintainable than to be fast. 
34 Fast code that is difficult to maintain is likely going to be looked down upon.
37 * Comments
39 Avoid perl style comments using "#". Always use "//" for single line comments
40 and /* */ blocks for multi line comments.
42 8<----------------------------------------------------------------------------
43 /*
44  * This is a long comment...
45  * ... which should look like this.
46  */
48 // Short comment
49 8<----------------------------------------------------------------------------
52 * Documentation
54 8<----------------------------------------------------------------------------
56 8<----------------------------------------------------------------------------
58 svn propset svn:keywords "Id" file
61 * File format
63 UTF-8, LF - not CR LF
64 svn propset svn:eol-style native file
67 * White spaces
69 Use a space before an opening parenthesis when calling functions, or indexing, like this:
71 8<----------------------------------------------------------------------------
72 # Methods
73 foo ($parameter);
75 # Arrays
76 $b = $value [0];
78 # Readability
79 if ($b + 5 > foo (bar () + 4)){
80 }
81 8<----------------------------------------------------------------------------
83 Don't layout your code like this, always minimize spaces:
85 8<----------------------------------------------------------------------------
86 var $most           = "something";
87 8<----------------------------------------------------------------------------
90 Always use spaces to seperate arguments after commata:
92 8<----------------------------------------------------------------------------
93 function foo ($param1, $param2)
94 8<----------------------------------------------------------------------------
96 Always use single spaces to split logical and mathematical operations:
98 8<----------------------------------------------------------------------------
99 if ( $a > 6 && $b == 17 && (foo ($b) < 1) ){
101 8<----------------------------------------------------------------------------
104 * Braces
106 If statements with or without else clauses are formatted like this:
108 8<----------------------------------------------------------------------------
109 if ($value) {
110     foo ();
111     bar ();
114 if ($value) {
115     foo ();
116 } else {
117     bar ();
119 8<----------------------------------------------------------------------------
121 Switches are formatted like this:
123 8<----------------------------------------------------------------------------
124 switch ($reason) {
125     case 'fine':
126         foo ();
127         break;
129     case 'well':
130         bar ();
131         break;
133 8<----------------------------------------------------------------------------
136 Always use use braces for single line blocks:
138 8<----------------------------------------------------------------------------
139 if ($value) {
140     foo ();
142 8<----------------------------------------------------------------------------
144 Function definitions, Classes and Methods have an opening brace on the next
145 line:
147 8<----------------------------------------------------------------------------
148 function bar ()
150     ...
152 8<----------------------------------------------------------------------------
155 * Casing
157 Always use camel casing with lowercase characters in the beginning for multi-
158 word identifiers:
160 8<----------------------------------------------------------------------------
161 function checkForValidity (){
162   $testSucceeded = false;
163   ...
165 8<----------------------------------------------------------------------------
168 * Naming
170 Non trivial variable names should speak for themselves from within the context.
172 8<----------------------------------------------------------------------------
173 // Use
174 $hour = 5;
175 // instead of
176 $g = 5;
177 8<----------------------------------------------------------------------------
179 Find short function names that describe what the function does - in order to
180 make the code read like a written sentence.
182 8<----------------------------------------------------------------------------
183 if ( configReadable ("/etc/foo.conf") ){
185 8<----------------------------------------------------------------------------
187 Use uppercase for constants/defines and _ if possible:
189 8<----------------------------------------------------------------------------
190 if ( $speedUp == TRUE ) {
191   $wait = SHORT_WAIT;
192 } else {
193   $wait = LONG_WAIT;
195 8<----------------------------------------------------------------------------
198 * PHP specific
200 Open and close tags:
202   <?php 
203   // Something here 
204   ?> 
207 HTML
209 Do not include HTML code inside of your PHP file. Use smarty templating if
210 possible.
213 Code inclusion
215 Use require_once instead of include.