Is it true that I should use K&R styling when writing javascript? [closed]
Asked Answered
M

2

13

I didn't realise it until recently, but I use the Allman style when writing javascript code.

According to http://encosia.com/in-javascript-curly-brace-placement-matters-an-example/ I should be using K&R style. This is because in some situations placement of the curly bracket on the next line can cause problems. The problem for me is that I've grown rather fond of the Allman style and would rather not change.

So, my question(s)

  • Is it true that the allman style can cause errors due to the placement of curly brackets in javascript? How about jquery?

  • Is there any trick that would enable me to use Allman style without problems due to curly brackets?

  • Are there any other problems associated with using these styles?

Merrick answered 2/9, 2012 at 6:54 Comment(6)
I guess it depends on how often you return object literalsMinuscule
I can't see any problems that may arise, except readability. However K&R seems to make more sense for me, as I often declare functions and/or objects within function calls (e.g. 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
I don't really think of return as a control statement anyway, so it seems strange to me that Allman style should call for the problematic code in the example in the first place. As a side-quesiton, does automatic semicolon insertion happen anywhere else than after a "return" where it could similarly disrupt intended behavior?Nuremberg
Yes it will cause a flame war on SO like it has hundreds of time before. Just this morning I actually read a comment that told the poster to use tabs or 4 spaces indent, which to me is plain silly and breaks posts at SO on iDevicesSoothfast
possible duplicate of JavaScript formatting: must braces be on the same line as the if/function/etc keyword?Crabbing
Technicality aside, I think egyptian brackets just look prettier.Nurture
R
9

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.

Reiter answered 2/9, 2012 at 7:24 Comment(7)
this behaviour is spec-compliant and becomes a problem not only with returnCrabbing
@o.v, in practice, continue and break take an optional identifier, not an object literal. do ... while would only pose problems if while evaluates an object literal, which arguably is quite a complicated way to implement an infinite loop. Your point on throw stands, though, even if throwing object literals is quite rare in my experience.Catherin
I was not really focusing on OP's specific scenario with brackets but rather on one's inability to break lines after those statements in general (i.e. no continue[label], duh), but one's ability to break lines as often as they wish after conditionals etc.Crabbing
Objects literals part is no longer relevant -- all modern browsers and node.js accept objects written in Allman style.Consult
@Piotr, I double-checked and I don't understand what you mean -- my node.js (0.10.31) still has the foo() function in my first snippet return undefined .Catherin
@Frédéric, I wrote about your second snippet, using of Allman style is fine when assigning objects. Allman style should be avoided only after return and maybe throw -- that part of your post is still valid.Consult
@Piotr, ah, I see. I never said Allman style was invalid when assigning, just less readable.Catherin
C
3

Keep in mind ECMAScript specification regarding automatic semicolon insertion:

Certain ECMAScript statements (empty statement, variable statement, expression statement, do-while statement, continue statement, break statement, return statement, and throw statement) must be terminated with semicolons. Such semicolons may always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations.

I laugh at the suggestion above that it is "convenient"...

While you technically could could continue using Allman formatting in cases not covered above, I would preach consistency. Personally I'm a fan of Google JS styleguide - sections relevant to your question are the code formatting and semicolons ones. The examples listed there are worth checking out.

Crabbing answered 2/9, 2012 at 8:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.