3 ms·
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. R
by btilly 15y ago
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.
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 15y agoYou also gain a new possible bug--the leading comma.
- ynniv 15y 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 15y 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 15y agoIt is a real problem. Formatting can address it. That doesn't mean that formatting is the best solution.
- taligent 15y 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 15y 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
- mustpax 15y agoPrevention > Detection by Developer > Detection by Tester > Detection by Customer. Having mechanisms in place to catch programmer errors is no excuse for adopting practices that make programmer errors more likely.
- ynniv 15y agoTwo of the tools that I mentioned are in the "prevention" category: syntax checkers and minifiers. The full enumeration is just a sanity check. If coding style is the only thing keeping your product from being broken, you're doing something else quite wrong.
- ralesk 15y 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 15y 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 15y 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 15y agoTrailing commas is defined in ECMAScript 5. The issue is that IE8 and earlier choke on them.
- ralesk 15y 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 15y agoI think if you re-read my comment, you'll find I'm saying the same thing.
- bentlegen 15y agoWhat's harder, finding an extra trailing comma, or finding a missing semi-colon in a JavaScript file that contains no semi-colons?
- btilly 15y agoI was not comparing the possible pain of different bugs. I was merely stating that there is possible pain with commas.
- deleted 15y ago[deleted]
- ricardobeat 15y 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.