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.
Needs to be changed to:
This one is a bit of a holy war, but we're going to use a style that can be summed up in one [s]sentence[/s]paragraph:
Braces always go on their own line, with several major exceptions when writing JavaScript. When returning an object, you should obviously have braces on the same line, as due to JavaScript's implied semicolon insertion mechanism, the following code is invalid:
Code: Select all
function func() {
return
{
foo: "bar"
};
}
In that example, the function would return "undefined". For this reason, you should have the braces on the same line when returning objects. You should also have braces on the same line when defining variables as objects, as while it is still valid, it is difficult to read.
When defining anonymous functions, you should have braces on the same line, or the code is difficult to read. However, you should still have the braces on their own line when defining functions traditionally (
function funcName()
).
Seriously though - while I can understand that where to have braces in PHP is purely a matter of personal preference - depending on what you're used to, either look good - in a language such as JavaScript, it simply does not work to have braces on their own line. It can result in code like the following:
Code: Select all
/**
* Returns a random function.
*
* @constructor
*/
function MyFunc(a)
{
var i = 0;
if (a)
{
return function() {
return {
i: i += 1
}
}
}
else
{
return function() {
return {
i: i += 2
}
}
}
}
The code doesn't do much, but you can that it looks fairly inconsistent and stupid compared to how I would write it:
Code: Select all
/**
* Returns a random function.
*
* @constructor
*/
function MyFunc(a) {
var i = 0;
if (a) {
return function() {
return {
i: i += 1
}
}
} else {
return function() {
return {
i: i += 2
}
}
}
}
So I think that all JavaScript contained in the phpBB package should have the braces on the same line. However, as having JavaScript and PHP using different coding standards would be confusing and where the braces are in PHP are purely down to personal preference, I think that the entire phpBB project should switch to having braces on the same line.
In summary:
Advantages to having braces on same line:
- Doesn't look stupid, and is more consistent.
- It's usually more widely used, so developers will be more likely to be used to it.
Advantages to having braces on own line:
- No change, so nothing would need to be changed. However, as phpBB would be having a full rewrite, this doesn't seem like a major problem.
- Symfony has the braces on own line.
I would also suggest an amendment to the Method & Function Names section; functions, when used a constructor in a Javascript, should start with a capital letter.