var variable, // trailing comma
anotherVariable,
thirdVariable;
var variable
, anotherVariable // preceding comma
, thirdVariable; var variable, // trailing comma
anotherVariable,
thirdVariable;
var variable
, anotherVariable // preceding comma
, thirdVariable;http://ajaxian.com/archives/is-there-something-to-the-crazy-...
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"
; if (true) {
console.log('winning);
}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.
return {
key: 'value',
anotherKey: 'another value'
}
This is fine and works as expected, now consider the following K&R/Allman style code... return
{
key: 'value',
anotherKey: 'another value'
}
In Javascript this is parsed as `return;` (notice the semi-colon), this is because Javascript does its dreaded automatic semicolon insertion magic.So, I think that's a pretty good practical reason to prefer 1TBS in Javascript.
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.Do Javascript parsers look at the next line before judging whether or not to treat the line-ending as an implicit semicolon?
(Do all parsers treat it in the same way? Is it part of the spec?)
http://blog.izs.me/post/2353458699/an-open-letter-to-javascr...
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: create table text_editors ( name varchar
, latest_version varchar
, list_of_stupid_flaws blob
);
I suspect that this approach would fix most of Damien Katz's complaints about Erlang's syntax in http://damienkatz.net/2008/03/what_sucks_abou.html: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:
blah(true) ->
foo(),
bar();
blah(false) ->
baz().
becomes blah(true)
-> foo()
, bar()
;
blah(false)
-> baz()
.
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: blah(false)
-> baz()
;
blah(true)
-> foo()
, bar()
.
His other transformation, of changing the order of foo() and bar(), still isn't trivial, but it's still visually obvious when you screw it up: blah(true)
, bar()
-> foo()
. if Cond1 -> Exp1
; Cond2 -> Exp2
; ... -> ...
; CondN -> ExpN
end
Other than that, meh. I think Erlang just needs to read/write code differently. My blog post on the issue: http://ferd.ca/on-erlang-s-syntax.html >>> x = ( "abc"
... "def" )
>>> x
'abcdef'
Erlang also works this way. Note it only works for string literals.But that's just FYI, it obviously doesn't affect your point.
It's actually slightly strange to me that for everything else Perl does that this doesn't work in Perl.
Well, doesn't JS allow you to wrap it in parens if you want?