4 ms·
I think using comma-first syntax as a counter example is disingenuous. Whether you're a novice programmer or an expert programmer, using comma-first or comma-la
by bentlegen 14y ago
I think using comma-first syntax as a counter example is disingenuous. Whether you're a novice programmer or an expert programmer, using comma-first or comma-last isn't going to bite you in a way that isn't immediately solvable.
Omitting semi-colons, however, can cause novice JavaScript programmers serious grief. Nevermind novice JavaScript programmers – they cause me grief, and I put semi-colons everywhere! That's because I'm often called in to debug our customers websites, and on several such occasions I've had to fix bugs that never would have occurred if that customer used semi-colons properly.
Sure, if our customers understood JavaScript's ASI and how to use semi-colons "properly", I wouldn't have this problem either. Except, as Tom points out, the reality is that almost none of these people will invest in doing so.
- deleted 14y ago[deleted]
- btilly 14y agoWhether you're a novice programmer or an expert programmer, using comma-first or comma-last isn't going to bite you in a way that isn't immediately solvable. Really? The comma first style makes it less likely that your list will end with a trailing comma, which can cause IE-specific bugs. If you're trying to track down the IE breakage a couple of weeks after you wrote down the code, it can be fun to track down the extra comma. If you don't know that this bug is possible (as I'm guessing you don't), multiply the hair-pulling by 10. See the cautionary story in http://www.enterprisedojo.com/2010/12/19/beware-the-trailing-comma-of-death/ http://www.enterprisedojo.com/2010/12/19/beware-the-trailing... for a real life example. With the comma first style, that bug would never have been introduced. Neat, huh?
- aec 14y agoYou also gain a new possible bug--the leading comma.
- ynniv 14y agoWith the comma first style, that bug would never have been introduced. Neat, huh? If you run any syntax checking or minification tools, this will be immediately obvious. If you test your site in IE at all, this will be immediately obvious. If you record JS errors on your site, this will soon be obvious. If you record any kind of usage statistic, this will eventually be obvious. Take it from someone who has experimented with cutesy formatting in the past[1]: you don't need to use cutesy formatting to solve this problem. [1 | You can end class declarations with _:0} instead of a bare } to avoid the trailing comma issue. ]
- btilly 14y agoIndeed the link I gave demonstrates how the right IDE also solves this problem. The fact that there are many ways of solving the problem does not mean that it isn't a real problem that formatting can help address.
- ynniv 14y agoIt is a real problem. Formatting can address it. That doesn't mean that formatting is the best solution.
- taligent 14y agoIt means that there are ways of solving the problem that don't involve rewriting the code in a way that initially will confuse 99.999% of other developers. And it is important in our industry to write MAINTAINABLE code.
- mattbriggs 14y agoif comma first confuses a dev more then half a second or so, they really aren't that good. First time I saw comma first was from a DBA about 10 years ago who swore by it, and it has been my go to technique for writing maintainable code in languages where you end up with huge comma separated lists. I freely remove, add, and move things to the bottom of my js classes without ever hitting trailing semis, or hitting a missing semi. I think that is more maintainable then the alternative
- ralesk 14y agoWith a decent language that allows trailing commas (eg. everything other than JS or SQL), it would never have been a problem in the first place.
- btilly 14y agoActually JS does allow trailing commas. Read the spec. It is an IE bug that causes it to break. One of the many ways they do not follow the spec.
- damncabbage 14y agoIt's the other way around. It was illegal in ECMAScript 3, which is what IE 8 and below (at the very least) implement: ObjectLiteral : { } { PropertyNameAndValueList } With ECMAScript 5 it is legal: ObjectLiteral : { } { PropertyNameAndValueList } { PropertyNameAndValueList , } So before v5, the other browsers were incorrectly tolerating something that goes against the spec. It's only semi-recently that it's been "allowed" as a valid practice.
- bentlegen 14y agoTrailing commas is defined in ECMAScript 5. The issue is that IE8 and earlier choke on them.
- ralesk 14y agoIsn't ES5 only supported since IE9? Not to mention, IE8 was released half a year before ES5 (according to a quick check on Wikipedia). So yes, pre-ES5 I'd say it's a normal thing to do not to allow trailing commas.
- bentlegen 14y agoI think if you re-read my comment, you'll find I'm saying the same thing.
- bentlegen 14y agoWhat's harder, finding an extra trailing comma, or finding a missing semi-colon in a JavaScript file that contains no semi-colons?
- btilly 14y agoI was not comparing the possible pain of different bugs. I was merely stating that there is possible pain with commas.
- deleted 14y ago[deleted]
- ricardobeat 14y agoVisually? Finding the missing semi-colon. It should be missing from at most one line out of a hundred, and at the beginning of it, very easy to spot - it's a visible wart, as opposed to looking for a needle in a haystack.
- mattbriggs 14y agoIt's not about omitting semi-colons, it is about treating special cases as special cases. I just wrote something about the goals of NPM style, and how it can help debug those nasty problems http://mattbriggs.net/blog/2012/05/03/npm-style-javascript-is-the-conservative-choice/ http://mattbriggs.net/blog/2012/05/03/npm-style-javascript-i...