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: 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()
.