Is it true that the allman style can cause errors due to the placement
of curly brackets in javascript?
Yes, it is. As your link explains, return
statements followed by a newline will confuse the parser as it tries to insert a semicolon. This behavior is not limited to returning object literals, though. The following function:
function foo()
{
return
"bar";
}
Will also return undefined
.
How about jquery?
The situation remains exactly the same whether or not jQuery is included.
Is there any trick that would enable me to use Allman style without
problems due to curly brackets?
Yes, do not use Allman style with object literals. Consider you want to assign an object literal to a variable. Are you really going to write:
var foo =
{
bar: "quux"
};
Or will you go for:
var foo = {
bar: "quux"
};
IMHO the second snippet is more readable than the first. You can continue using Allman style with the braces inside for
, if
, while
, function
, etc., but make an exception for object literals.
Are there any other problems associated with using these styles?
return
is quite a special case, because it is valid with or without an argument. When confronted with a lone return
statement on one line, the parser cannot tell if it should add a semicolon to make the single-line statement valid, or continue to the next lines and hope for the best.
For this reason, I think this behavior only becomes a problem with return
and maybe throw
, not other statements.
setTimeout(1000, function() { complex_functions_content });
The content of the complex function would be in a new line and the closing brackets (both the curly and the round) are in the last line, on the same indentation level as the setTimeout function call. Writing such a code with Allman style would look weird IMHO. – Thorlie