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