I was afraid to read the style guide for fear of seeing the preceding comma pattern (I think it's popular in Node because ryah uses it). Thankfully it recommends the trailing comma :)
var variable, // trailing comma
anotherVariable,
thirdVariable;
var variable
, anotherVariable // preceding comma
, thirdVariable;
More specifically, in which is it easier for you to find the error?
// error in standard style
var a = "ape",
b = "bat",
c = "cat",
d = "dog"
e = "elf",
f = "fly",
g = "gnu",
h = "hat",
i = "ibu";
// error in comma-first style
var a = "ape"
, b = "bat"
, c = "cat"
, d = "dog"
e = "elf"
, f = "fly"
, g = "gnu"
, h = "hat"
, i = "ibu"
;
I'm not ignoring it but my code always runs through JSLint each time its saved and that picks up misplaced commas for me. Also we're not talking about client-side javascript here; fortunately Node.js uses the V8 Javascript engine, not the IE7 one. :)
I'd rather occasionally have comma bugs than make all my code horrendously ugly. Also, most people don't set their tabstop to 2 characters, which means you have to use space key twice in every variable declaration. That's got to be annoying.
If that's so, why do we not see this in other mainstream languages? While I see the slight advantage of seeing errors, that is the first thing node sees. I use either nodemon or cluster, which both monitor your program on saving your code. When you think you're ready to go, save and see if it has no compile errors.
Haskell's an odd case; experienced Haskell users are likely to be thinking of the comma as less of a "separator" than a "combinator" constructing a tuple, and preceding-combinator-on-newline is an idiomatic style. Which doesn't disprove your point, I'm just saying it's an edge case. This is after all the language community that interprets a semicolon (as used in most languages) as a combinator too.
I was afraid to read the style guide for fear of seeing K&R vs Allman indents/braces. As usual, it was hit on. However, to my amusement, the "Right" way of doing things actually has a syntax error:
I have been personally screamed at for suggesting One True Brace Style over K&R in response to a request for feedback on an architecture standards document. That was when I learned that corporate architecture teams don't actually want to get feedback when they ask for it.
I find it odd that I have a strong opinion on the topic but have never seen a convincing argument that one way is actually better than another, it's just that one feels right to me and the others don't and I can't articulate but neither can anyone else I've seen.
Other than style, is there an advantage to either of them over just declaring each one separately?
var variable;
var anotherVariable;
var thirdVariable;
One issue I see with multiple variable declarations is that a forgotten comma doesn't cause a syntax error, it just causes that variable and any after it to be global variables. Which leads to much nastier javascript bugs.
Y'know, I apparently haven't been plugged in enough to JS to realize that as a valid solution, because Python has spoiled me letting me terminate tuples with a comma (which I find elegant and painless) -- but the preceding comma pattern at least makes it easier to troubleshoot code and doesn't make me feel TOO much like I'm just trying to avoid pissing the interpreter off.
I use the comma-first style in everything (in languages that have this problem) now that I know about it. I was also interested to see it with semicolons in Simon PJ's chapter in Beautiful Code.
I just wish the character was something other than a comma! It looks so wrong in that position.
BTW, your second example should be
var variable
, anotherVariable // preceding comma
, thirdVariable
;
Perl and Python don't have the trailing-comma problem in most contexts, but they do have a similar problem with some other infix operators. For example, I'm still not sure whether I should use it for strings:
var myString = "This string is a bit too long to fit "
+ "comfortably on a single line, so I "
+ "have split it across three lines."
;
The problem with this is that the first line looks like a standalone JS statement. Python requires you to wrap the whole thing in parens, which solves that problem:
readMyString = ( "This string is a bit too long to fit "
+ "comfortably on a single line, so I "
+ "have split it across three lines."
);
The other language besides JavaScript where I find it useful is SQL:
Because Erlang's expression terminators vary by context, editing code is much harder than conventional languages. Refactoring -- cutting and pasting and moving code around -- is particularly hard to do without creating a bunch of syntax errors.
His example transformation of reordering the branches now works correctly simply by cutting and pasting lines, albeit in two chunks (just as it would be in JS), and moreover any error in the process is visually obvious:
The case of Erlang is more or less a matter of preference. I haven't seen any piece of Erlang code with the separator first, except in the case of the if expression:
if Cond1 -> Exp1
; Cond2 -> Exp2
; ... -> ...
; CondN -> ExpN
end
The problem with this is that the first line looks like a standalone JS statement. Python requires you to wrap the whole thing in parens, which solves that problem:
Well, doesn't JS allow you to wrap it in parens if you want?