Summary: The basic philosophy here is to not hurt code clarity for the
sake of laziness. This has to be balanced by a little bit of common sense,
though; print_login_status_for_a_given_user()
goes too far, for example -- that function would be better named print_user_login_status() , or just print_login_status().
Standard header for new files: Here a template of the header that must be included at the start of all phpBB files:
/*************************************************************************** filename.php ------------------- begin : Sat June 17 2000 copyright : (C) 2000 The phpBB Group email : support@phpBB.com $Id: codingstandards.htm,v 1.3 2001/06/09 21:00:12 natec Exp $ ***************************************************************************/ /*************************************************************************** * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * ***************************************************************************/
Always include the braces: This is another case of being too lazy to
type 2 extra characters causing problems with code clarity. Even if the body of
some construct is only one line long, do not drop the braces. Just don't.
Examples:
/* These are all wrong. */
if (condition) do_stuff();
if (condition)
do_stuff();
while (condition)
do_stuff();
for ($i = 0; $i < size; $i++)
do_stuff($i);
/* These are right. */
if (condition)
{
do_stuff();
}
while (condition)
{
do_stuff();
}
for ($i = 0; $i < size; $i++)
{
do_stuff();
}
Where to put the braces: This one is a bit of a holy war, but we're
going to use a style that can be summed up in one sentence: Braces always go on
their own line. The closing brace should also always be at the same column as
the corresponding opening brace.
Examples:
if (condition)
{
while (condition2)
{
...
}
}
else
{
...
}
for ($i = 0; $i < $size; $i++)
{
...
}
while (condition)
{
...
}
function do_stuff()
{
...
}
Use spaces between tokens: This is another simple, easy step that
helps keep code readable without much effort. Whenever you write an assignment,
expression, etc.. Always leave one space between the tokens. Basically,
write code as if it was English. Put spaces between variable names and
operators. Don't put spaces just after an opening bracket or before a closing
bracket. Don't put spaces just before a comma or a semicolon. This is best shown
with a few examples.
Examples:
/* Each pair shows the wrong way followed by the right way. */ $i=0; $i = 0; if($i<7) ... if ($i < 7) ... if ( ($i < 7)&&($j > 8) ) ... if (($i < 7) && ($j > 8)) ... do_stuff( $i, "foo", $b ); do_stuff($i, "foo", $b); for($i=0; $i<$size; $i++) ... for($i = 0; $i < $size; $i++) ... $i=($j < $size)?0:1; $i = ($j < $size) ? 0 : 1;
Operator precedence: Do you know the exact precedence of all the
operators in PHP? Neither do I. Don't guess. Always make it obvious by using
brackets to force the precedence of an equation so you know what it does.
Examples:
/* what's the result? who knows. */ $bool = ($i < 7 && $j > 8 || $k == 4); /* now you can be certain what I'm doing here. */ $bool = (($i < 7) && (($j < 8) || ($k == 4)))
SQL code layout: Since we'll all be using different editor settings,
don't try to do anything complex like aligning columns in SQL code. Do, however,
break statements onto their own lines. Here's a sample of how SQL code should
look. Note where the lines break, the capitalization, and the use of brackets.
Examples:
SELECT field1 AS something, field2, field3 FROM table a, table b WHERE (this = that) AND (this2 = that2)
SQL insert statements: SQL INSERT statements can be written in two
different ways. Either you specify explicitly the columns being inserted, or
you rely on knowing the order of the columns in the database and do not
specify them. We want to use the former approach, where it is explicitly
stated whcih columns are being inserted. This means our application-level code
will not depend on the order of the fields in the database, and will not be broken
if we add additional fields (unless they're specified as NOT NULL, of course).
Examples:
# This is not what we want.
INSERT INTO mytable
VALUES ('something', 1, 'else')
# This is correct.
INSERT INTO mytable (column1, column2, column3)
VALUES ('something', 1, 'else')
Quoting strings: There are two different ways to quote strings in PHP
- either with single quotes or with double quotes. The main difference is that
the parser does variable interpolation in double-quoted strings, but not in
single quoted strings. Because of this, you should always use single
quotes unless you specifically need variable interpolation to be done on
that string. This way, we can save the parser the trouble of parsing a bunch of
strings where no interpolation needs to be done. Also, if you are using a string
variable as part of a function call, you do not need to enclose that variable in
quotes. Again, this will just make unnecessary work for the parser. Note,
however, that nearly all of the escape sequences that exist for double-quoted
strings will not work with single-quoted strings. Be careful, and feel free to
break this guideline if it's making your code harder to read.
Examples:
/* wrong */
$str = "This is a really long string with no variables for the parser to find.";
do_stuff("$str");
/* right */
$str = 'This is a really long string with no variables for the parser to find.';
do_stuff($str);
Associative array keys: In PHP, it's legal to use a literal string as
a key to an associative array without quoting that string. We don't want to do
this -- the string should always be quoted to avoid confusion. Note that this is
only when we're using a literal, not when we're using a variable.
Examples:
/* wrong */ $foo = $assoc_array[blah]; /* right */ $foo = $assoc_array['blah'];
Comments: Each function should be preceded by a comment that tells a
programmer everything they need to know to use that function. The meaning of
every parameter, the expected input, and the output are required as a minimal
comment. The function's behaviour in error conditions (and what those error
conditions are) should also be present. Nobody should have to look at the actual
source of a function in order to be able to call it with confidence in their own
code.
In addition, commenting any tricky, obscure, or otherwise
not-immediately-obvious code is clearly something we should be doing. Especially
important to document are any assumptions your code makes, or preconditions for
its proper operation. Any one of the developers should be able to look at any