Verzeichnisstruktur phpBB-2.0.0


Veröffentlicht
03.04.2002

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

codingstandards.htm

Zuletzt modifiziert: 09.10.2024, 12:51 - Dateigröße: 14.26 KiB


001  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
002  <!-- saved from url=(0044) -->
003  <HTML><HEAD><TITLE>phpBB Coding Standard Guidelines</TITLE>
004  <META content="text/html; charset=windows-1252" http-equiv=Content-Type>
005  <META content="MSHTML 5.00.2920.0" name=GENERATOR></HEAD>
006  <BODY aLink=#cccccc bgColor=#ffffff link=#0000ff text=#000000 
007  vLink=#0000ff><FONT face=verdana,arial,tahoma size=-1><A name=top></A>
008  <H2>phpBB Coding Standard Guidelines</H2>Comments or suggestions? email <A 
009  href="mailto:nate@phpbb.com">nate@phpbb.com</A><BR><BR><A 
010  href="#editor">Editor 
011  Settings</A><BR><A 
012  href="#naming">Naming 
013  Conventions</A><BR><A 
014  href="#layout">Code Layout</A><BR><A 
015  href="#general">General 
016  Guidelines</A><BR><BR><BR><A name=editor></A><A 
017  href="#top">top</A> 
018  <H3>Editor Settings</H3>
019  <P><B>Tabs vs Spaces:</B> In order to make this as simple as possible, we will 
020  be using tabs, not spaces. Feel free to set how many spaces your editor uses 
021  when it <B>displays</B> tabs, but make sure that when you <B>save</B> the file, 
022  it's saving tabs and not spaces. This way, we can each have the code be 
023  displayed the way we like it, without breaking the layout of the actual files. 
024  </P>
025  <P><B>Linefeeds:</B> Ensure that your editor is saving files in the UNIX format. 
026  This means lines are terminated with a newline, not with a CR/LF combo as they 
027  are on Win32, or whatever the Mac uses. Any decent Win32 editor should be able 
028  to do this, but it might not always be the default. Know your editor. If you 
029  want advice on Windows text editors, just ask one of the developers. Some of 
030  them do their editing on Win32. </P><BR><BR><A name=naming></A><A 
031  href="#top">top</A> 
032  <H3>Naming Conventions</H3>
033  <P>We will not be using any form of hungarian notation in our naming 
034  conventions. Many of us believe that hungarian naming is one of the primary code 
035  obfuscation techniques currently in use. </P>
036  <P><B>Variable Names:</B> Variable names should be in all lowercase, with words 
037  separated by an underscore. <BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;Example: <CODE><FONT 
038  size=+1>$current_user</FONT></CODE> is right, but <CODE><FONT 
039  size=+1>$currentuser</FONT></CODE> and <CODE><FONT 
040  size=+1>$currentUser</FONT></CODE> are not. <BR><BR>Names should be descriptive, 
041  but concise. We don't want huge sentences as our variable names, but typing an 
042  extra couple of characters is always better than wondering what exactly a 
043  certain variable is for. </P>
044  <P><B>Loop Indices:</B> The <I>only</I> situation where a one-character variable 
045  name is allowed is when it's the index for some looping construct. In this case, 
046  the index of the outer loop should always be $i. If there's a loop inside that 
047  loop, its index should be $j, followed by $k, and so on. If the loop is being 
048  indexed by some already-existing variable with a meaningful name, this guideline 
049  does not apply. <BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;Example: <PRE><FONT size=+1>
050          for ($i = 0; $i &lt; $outer_size; $i++) 
051          {
052             for ($j = 0; $j &lt; $inner_size; $j++) 
053             {
054                foo($i, $j);
055             }
056          } </FONT></PRE>
057  <P></P>
058  <P><B>Function Names:</B> Functions should also be named descriptively. We're 
059  not programming in C here, we don't want to write functions called things like 
060  "stristr()". Again, all lower-case names with words separated by a single 
061  underscore character. Function names should preferably have a verb in them 
062  somewhere. Good function names are <CODE><FONT 
063  size=+1>print_login_status()</FONT></CODE>, <CODE><FONT 
064  size=+1>get_user_data()</FONT></CODE>, etc.. </P>
065  <P><B>Function Arguments:</B> Arguments are subject to the same guidelines as 
066  variable names. We don't want a bunch of functions like: <CODE><FONT 
067  size=+1>do_stuff($a, $b, $c)</FONT></CODE>. In most cases, we'd like to be able 
068  to tell how to use a function by just looking at its declaration. </P>
069  <P><B>Summary:</B> The basic philosophy here is to not hurt code clarity for the 
070  sake of laziness. This has to be balanced by a little bit of common sense, 
071  though; <CODE><FONT size=+1>print_login_status_for_a_given_user()</FONT></CODE> 
072  goes too far, for example -- that function would be better named <CODE><FONT 
073  size=+1>print_user_login_status()</FONT></CODE> , or just <CODE><FONT 
074  size=+1>print_login_status()</FONT></CODE>. </P><BR><BR><A name=layout></A><A 
075  href="#top">top</A> 
076  <H3>Code Layout</H3>
077  <P><B>Standard header for new files:</B> Here a template of the header that must 
078  be included at the start of all phpBB files: <PRE><FONT size=+1>
079          /***************************************************************************
080                                          filename.php
081                                       -------------------
082              begin                : Sat June 17 2000
083              copyright            : (C) 2000 The phpBB Group
084              email                : support@phpBB.com
085          
086              $Id$
087          
088           ***************************************************************************/
089          
090          /***************************************************************************
091           *                                                                                         
092           *   This program is free software; you can redistribute it and/or modify      
093           *   it under the terms of the GNU General Public License as published by  
094           *   the Free Software Foundation; either version 2 of the License, or            
095           *   (at your option) any later version.
096           *
097           ***************************************************************************/
098      </FONT></PRE>
099  <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>
328