Rev Author Line No. Line
130 kaklik 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2 <!-- saved from url=(0044) -->
3 <HTML><HEAD><TITLE>phpBB Coding Standard Guidelines</TITLE>
4 <META content="text/html; charset=windows-1252" http-equiv=Content-Type>
5 <META content="MSHTML 5.00.2920.0" name=GENERATOR></HEAD>
6 <BODY aLink=#cccccc bgColor=#ffffff link=#0000ff text=#000000
7 vLink=#0000ff><FONT face=verdana,arial,tahoma size=-1><A name=top></A>
8 <H2>phpBB Coding Standard Guidelines</H2>Comments or suggestions? email <A
9 href="mailto:nate@phpbb.com">nate@phpbb.com</A><BR><BR><A
10 href="#editor">Editor
11 Settings</A><BR><A
12 href="#naming">Naming
13 Conventions</A><BR><A
14 href="#layout">Code Layout</A><BR><A
15 href="#general">General
16 Guidelines</A><BR><BR><BR><A name=editor></A><A
17 href="#top">top</A>
18 <H3>Editor Settings</H3>
19 <P><B>Tabs vs Spaces:</B> In order to make this as simple as possible, we will
20 be using tabs, not spaces. Feel free to set how many spaces your editor uses
21 when it <B>displays</B> tabs, but make sure that when you <B>save</B> the file,
22 it's saving tabs and not spaces. This way, we can each have the code be
23 displayed the way we like it, without breaking the layout of the actual files.
24 </P>
25 <P><B>Linefeeds:</B> Ensure that your editor is saving files in the UNIX format.
26 This means lines are terminated with a newline, not with a CR/LF combo as they
27 are on Win32, or whatever the Mac uses. Any decent Win32 editor should be able
28 to do this, but it might not always be the default. Know your editor. If you
29 want advice on Windows text editors, just ask one of the developers. Some of
30 them do their editing on Win32. </P><BR><BR><A name=naming></A><A
31 href="#top">top</A>
32 <H3>Naming Conventions</H3>
33 <P>We will not be using any form of hungarian notation in our naming
34 conventions. Many of us believe that hungarian naming is one of the primary code
35 obfuscation techniques currently in use. </P>
36 <P><B>Variable Names:</B> Variable names should be in all lowercase, with words
37 separated by an underscore. <BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;Example: <CODE><FONT
38 size=+1>$current_user</FONT></CODE> is right, but <CODE><FONT
39 size=+1>$currentuser</FONT></CODE> and <CODE><FONT
40 size=+1>$currentUser</FONT></CODE> are not. <BR><BR>Names should be descriptive,
41 but concise. We don't want huge sentences as our variable names, but typing an
42 extra couple of characters is always better than wondering what exactly a
43 certain variable is for. </P>
44 <P><B>Loop Indices:</B> The <I>only</I> situation where a one-character variable
45 name is allowed is when it's the index for some looping construct. In this case,
46 the index of the outer loop should always be $i. If there's a loop inside that
47 loop, its index should be $j, followed by $k, and so on. If the loop is being
48 indexed by some already-existing variable with a meaningful name, this guideline
49 does not apply. <BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;Example: <PRE><FONT size=+1>
50 for ($i = 0; $i &lt; $outer_size; $i++)
51 {
52 for ($j = 0; $j &lt; $inner_size; $j++)
53 {
54 foo($i, $j);
55 }
56 } </FONT></PRE>
57 <P></P>
58 <P><B>Function Names:</B> Functions should also be named descriptively. We're
59 not programming in C here, we don't want to write functions called things like
60 "stristr()". Again, all lower-case names with words separated by a single
61 underscore character. Function names should preferably have a verb in them
62 somewhere. Good function names are <CODE><FONT
63 size=+1>print_login_status()</FONT></CODE>, <CODE><FONT
64 size=+1>get_user_data()</FONT></CODE>, etc.. </P>
65 <P><B>Function Arguments:</B> Arguments are subject to the same guidelines as
66 variable names. We don't want a bunch of functions like: <CODE><FONT
67 size=+1>do_stuff($a, $b, $c)</FONT></CODE>. In most cases, we'd like to be able
68 to tell how to use a function by just looking at its declaration. </P>
69 <P><B>Summary:</B> The basic philosophy here is to not hurt code clarity for the
70 sake of laziness. This has to be balanced by a little bit of common sense,
71 though; <CODE><FONT size=+1>print_login_status_for_a_given_user()</FONT></CODE>
72 goes too far, for example -- that function would be better named <CODE><FONT
73 size=+1>print_user_login_status()</FONT></CODE> , or just <CODE><FONT
74 size=+1>print_login_status()</FONT></CODE>. </P><BR><BR><A name=layout></A><A
75 href="#top">top</A>
76 <H3>Code Layout</H3>
77 <P><B>Standard header for new files:</B> Here a template of the header that must
78 be included at the start of all phpBB files: <PRE><FONT size=+1>
79 /***************************************************************************
80 filename.php
81 -------------------
82 begin : Sat June 17 2000
83 copyright : (C) 2000 The phpBB Group
84 email : support@phpBB.com
85  
86 $Id: codingstandards.htm,v 1.3 2001/06/09 21:00:12 natec Exp $
87  
88 ***************************************************************************/
89  
90 /***************************************************************************
91 *
92 * This program is free software; you can redistribute it and/or modify
93 * it under the terms of the GNU General Public License as published by
94 * the Free Software Foundation; either version 2 of the License, or
95 * (at your option) any later version.
96 *
97 ***************************************************************************/
98 </FONT></PRE>
99 <P></P>
100 <P><B>Always include the braces:</B> This is another case of being too lazy to
101 type 2 extra characters causing problems with code clarity. Even if the body of
102 some construct is only one line long, do <I>not</I> drop the braces. Just don't.
103 <BR><BR>&nbsp;&nbsp;&nbsp;Examples:<PRE><FONT size=+1>
104 /* These are all wrong. */
105 if (condition) do_stuff();
106 if (condition)
107 do_stuff();
108 while (condition)
109 do_stuff();
110 for ($i = 0; $i &lt; size; $i++)
111 do_stuff($i);
112  
113 /* These are right. */
114 if (condition)
115 {
116 do_stuff();
117 }
118 while (condition)
119 {
120 do_stuff();
121 }
122 for ($i = 0; $i &lt; size; $i++)
123 {
124 do_stuff();
125 }
126 </FONT></PRE>
127 <P></P>
128 <P><B>Where to put the braces:</B> This one is a bit of a holy war, but we're
129 going to use a style that can be summed up in one sentence: Braces always go on
130 their own line. The closing brace should also always be at the same column as
131 the corresponding opening brace. <BR><BR>&nbsp;&nbsp;&nbsp;Examples:<PRE><FONT size=+1>
132 if (condition)
133 {
134 while (condition2)
135 {
136 ...
137 }
138 }
139 else
140 {
141 ...
142 }
143  
144 for ($i = 0; $i &lt; $size; $i++)
145 {
146 ...
147 }
148  
149 while (condition)
150 {
151 ...
152 }
153  
154 function do_stuff()
155 {
156 ...
157 }
158 </FONT></PRE>
159 <P></P>
160 <P><B>Use spaces between tokens:</B> This is another simple, easy step that
161 helps keep code readable without much effort. Whenever you write an assignment,
162 expression, etc.. Always leave <I>one</I> space between the tokens. Basically,
163 write code as if it was English. Put spaces between variable names and
164 operators. Don't put spaces just after an opening bracket or before a closing
165 bracket. Don't put spaces just before a comma or a semicolon. This is best shown
166 with a few examples. <BR><BR>&nbsp;&nbsp;&nbsp;Examples:<PRE><FONT size=+1>
167 /* Each pair shows the wrong way followed by the right way. */
168  
169 $i=0;
170 $i = 0;
171  
172 if($i&lt;7) ...
173 if ($i &lt; 7) ...
174  
175 if ( ($i &lt; 7)&amp;&amp;($j &gt; 8) ) ...
176 if (($i &lt; 7) &amp;&amp; ($j &gt; 8)) ...
177  
178 do_stuff( $i, "foo", $b );
179 do_stuff($i, "foo", $b);
180  
181 for($i=0; $i&lt;$size; $i++) ...
182 for($i = 0; $i &lt; $size; $i++) ...
183  
184 $i=($j &lt; $size)?0:1;
185 $i = ($j &lt; $size) ? 0 : 1;
186 </FONT></PRE>
187 <P></P>
188 <P><B>Operator precedence:</B> Do you know the exact precedence of all the
189 operators in PHP? Neither do I. Don't guess. Always make it obvious by using
190 brackets to force the precedence of an equation so you know what it does.
191 <BR><BR>&nbsp;&nbsp;&nbsp;Examples:<PRE><FONT size=+1>
192 /* what's the result? who knows. */
193 $bool = ($i &lt; 7 &amp;&amp; $j &gt; 8 || $k == 4);
194  
195 /* now you can be certain what I'm doing here. */
196 $bool = (($i &lt; 7) &amp;&amp; (($j &lt; 8) || ($k == 4)))
197 </FONT></PRE>
198 <P></P>
199 <P><B>SQL code layout:</B> Since we'll all be using different editor settings,
200 don't try to do anything complex like aligning columns in SQL code. Do, however,
201 break statements onto their own lines. Here's a sample of how SQL code should
202 look. Note where the lines break, the capitalization, and the use of brackets.
203 <BR><BR>&nbsp;&nbsp;&nbsp;Examples:<PRE><FONT size=+1>
204 SELECT field1 AS something, field2, field3
205 FROM table a, table b
206 WHERE (this = that) AND (this2 = that2)
207 </FONT></PRE>
208 <P></P>
209 <P><B>SQL insert statements:</B> SQL INSERT statements can be written in two
210 different ways. Either you specify explicitly the columns being inserted, or
211 you rely on knowing the order of the columns in the database and do not
212 specify them. We want to use the former approach, where it is explicitly
213 stated whcih columns are being inserted. This means our application-level code
214 will not depend on the order of the fields in the database, and will not be broken
215 if we add additional fields (unless they're specified as NOT NULL, of course).
216 <BR><BR>&nbsp;&nbsp;&nbsp;Examples:<PRE><FONT size=+1>
217 # This is not what we want.
218 INSERT INTO mytable
219 VALUES ('something', 1, 'else')
220  
221 # This is correct.
222 INSERT INTO mytable (column1, column2, column3)
223 VALUES ('something', 1, 'else')
224 </FONT></PRE>
225 <P></P><BR><BR><A name=general></A><A
226 href="#top">top</A>
227 <H3>General Guidelines</H3>
228 <P><B>Quoting strings:</B> There are two different ways to quote strings in PHP
229 - either with single quotes or with double quotes. The main difference is that
230 the parser does variable interpolation in double-quoted strings, but not in
231 single quoted strings. Because of this, you should <I>always</I> use single
232 quotes <I>unless</I> you specifically need variable interpolation to be done on
233 that string. This way, we can save the parser the trouble of parsing a bunch of
234 strings where no interpolation needs to be done. Also, if you are using a string
235 variable as part of a function call, you do not need to enclose that variable in
236 quotes. Again, this will just make unnecessary work for the parser. Note,
237 however, that nearly all of the escape sequences that exist for double-quoted
238 strings will not work with single-quoted strings. Be careful, and feel free to
239 break this guideline if it's making your code harder to read.
240 <BR><BR>&nbsp;&nbsp;&nbsp;Examples:<PRE><FONT size=+1>
241 /* wrong */
242 $str = "This is a really long string with no variables for the parser to find.";
243 do_stuff("$str");
244  
245 /* right */
246 $str = 'This is a really long string with no variables for the parser to find.';
247 do_stuff($str);
248 </FONT></PRE>
249 <P></P>
250 <P><B>Associative array keys:</B> In PHP, it's legal to use a literal string as
251 a key to an associative array without quoting that string. We don't want to do
252 this -- the string should always be quoted to avoid confusion. Note that this is
253 only when we're using a literal, not when we're using a variable.
254 <BR><BR>&nbsp;&nbsp;&nbsp;Examples:<PRE><FONT size=+1>
255 /* wrong */
256 $foo = $assoc_array[blah];
257  
258 /* right */
259 $foo = $assoc_array['blah'];
260 </FONT></PRE>
261 <P></P>
262 <P><B>Comments:</B> Each function should be preceded by a comment that tells a
263 programmer everything they need to know to use that function. The meaning of
264 every parameter, the expected input, and the output are required as a minimal
265 comment. The function's behaviour in error conditions (and what those error
266 conditions are) should also be present. Nobody should have to look at the actual
267 source of a function in order to be able to call it with confidence in their own
268 code. <BR><BR>In addition, commenting any tricky, obscure, or otherwise
269 not-immediately-obvious code is clearly something we should be doing. Especially
270 important to document are any assumptions your code makes, or preconditions for
271 its proper operation. Any one of the developers should be able to look at any
272 part of the application and figure out what's going on in a reasonable amount of
273 time. </P>
274 <P><B>Magic numbers:</B> Don't use them. Use named constants for any literal
275 value other than obvious special cases. Basically, it's OK to check if an array
276 has 0 elements by using the literal 0. It's not OK to assign some special
277 meaning to a number and then use it everywhere as a literal. This hurts
278 readability AND maintainability. Included in this guideline is that we should be
279 using the constants TRUE and FALSE in place of the literals 1 and 0 -- even
280 though they have the same values, it's more obvious what the actual logic is
281 when you use the named constants. </P>
282 <P><B>Shortcut operators:</B> The only shortcut operators that cause readability
283 problems are the shortcut increment ($i++) and decrement ($j--) operators. These
284 operators should not be used as part of an expression. They can, however, be
285 used on their own line. Using them in expressions is just not worth the
286 headaches when debugging. <BR><BR>&nbsp;&nbsp;&nbsp;Examples:<PRE><FONT size=+1>
287 /* wrong */
288 $array[++$i] = $j;
289 $array[$i++] = $k;
290  
291  
292 /* right */
293 $i++;
294 $array[$i] = $j;
295  
296 $array[$i] = $k;
297 $i++;
298 </FONT></PRE>
299 <P></P>
300 <P><B>Inline conditionals:</B> Inline conditionals should only be used to do
301 very simple things. Preferably, they will only be used to do assignments, and
302 not for function calls or anything complex at all. They can be harmful to
303 readability if used incorrectly, so don't fall in love with saving typing by
304 using them. <BR><BR>&nbsp;&nbsp;&nbsp;Examples:<PRE><FONT size=+1>
305 /* Bad place to use them */
306 (($i &lt; $size) &amp;&amp; ($j &gt; $size)) ? do_stuff($foo) : do_stuff($bar);
307  
308  
309 /* OK place to use them */
310 $min = ($i &lt; $j) ? $i : $j;
311 </FONT></PRE>
312 <P></P>
313 <P><B>Don't use uninitialized variables.</B> for phpBB 2, we intend to use a
314 higher level of run-time error reporting. This will mean that the use of an
315 uninitialized variable will be reported as an error. This will come up most
316 often when checking which HTML form variables were passed. These errors can be
317 avoided by using the built-in isset() function to check whether a variable has
318 been set. <BR><BR>&nbsp;&nbsp;&nbsp;Examples:<PRE><FONT size=+1>
319 /* Old way */
320 if ($forum) ...
321  
322  
323 /* New way */
324 if (isset($forum)) ...
325 </FONT></PRE>
326 <P></P><BR><BR><A href="#top">Return
327 to top</A> </FONT></BODY></HTML>