News for hackers. Stuff that matters.
Re:Best Practices Exist For A Reason by btilly on Wednesday May 02, 2012 @04:41PM (#3921078)
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... for a real life example.

With the comma first style, that bug would never have been introduced. Neat, huh?

Re:Best Practices Exist For A Reason by ynniv on Wednesday May 02, 2012 @05:00PM (#3921175)
With 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. ]

Re:Best Practices Exist For A Reason by mustpax on Wednesday May 02, 2012 @08:33PM (#3921856)
Prevention > 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.

Re:Best Practices Exist For A Reason by ynniv on Wednesday May 02, 2012 @11:22PM (#3922233)
Two 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.
Re:Best Practices Exist For A Reason by btilly on Wednesday May 02, 2012 @05:36PM (#3921339)
Indeed 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.
Re:Best Practices Exist For A Reason by ynniv on Wednesday May 02, 2012 @05:54PM (#3921403)
It is a real problem. Formatting can address it. That doesn't mean that formatting is the best solution.
Re:Best Practices Exist For A Reason by taligent on Wednesday May 02, 2012 @05:58PM (#3921420)
It 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.

Re:Best Practices Exist For A Reason by mattbriggs on Friday May 04, 2012 @12:27AM (#3927014)
if 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

Re:Best Practices Exist For A Reason by mark_story on Wednesday May 02, 2012 @09:27PM (#3921985)
You can also tell tools like JsHint to look for this common error, and not have to use _:0 everywhere.
Re:Best Practices Exist For A Reason by ynniv on Thursday May 03, 2012 @12:28AM (#3922382)
That's not what I meant... I must have used that example in a confusing manner. I do not recommend using syntax gimmicks.
Re:Best Practices Exist For A Reason by aec on Wednesday May 02, 2012 @04:59PM (#3921164)
You also gain a new possible bug--the leading comma.
Re:Best Practices Exist For A Reason by btilly on Wednesday May 02, 2012 @05:34PM (#3921327)
I have found that this is much less likely in practice.

Lists usually grow on the end, not the beginning.

(I use this kind of formatting for SQL, for a similar reason.)

Re:Best Practices Exist For A Reason by chris_wot on Wednesday May 02, 2012 @06:08PM (#3921464)
There's nothing like spending hours tracking down a bug that shouldn't occur in normal practice.
Re:Best Practices Exist For A Reason by ralesk on Wednesday May 02, 2012 @05:20PM (#3921253)
With 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.
Re:Best Practices Exist For A Reason by btilly on Wednesday May 02, 2012 @05:31PM (#3921312)
Actually 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.

Re:Best Practices Exist For A Reason by damncabbage on Wednesday May 02, 2012 @11:23PM (#3922235)
It'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.
Re:Best Practices Exist For A Reason by bentlegen on Wednesday May 02, 2012 @05:33PM (#3921325)
Trailing commas is defined in ECMAScript 5. The issue is that IE8 and earlier choke on them.
Re:Best Practices Exist For A Reason by ralesk on Wednesday May 02, 2012 @06:39PM (#3921552)
Isn'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.

Re:Best Practices Exist For A Reason by bentlegen on Wednesday May 02, 2012 @06:52PM (#3921599)
I think if you re-read my comment, you'll find I'm saying the same thing.
Re:Best Practices Exist For A Reason by bentlegen on Wednesday May 02, 2012 @05:21PM (#3921259)
What's harder, finding an extra trailing comma, or finding a missing semi-colon in a JavaScript file that contains no semi-colons?
Re:Best Practices Exist For A Reason by btilly on Wednesday May 02, 2012 @05:33PM (#3921323)
I was not comparing the possible pain of different bugs. I was merely stating that there is possible pain with commas.
Re:Best Practices Exist For A Reason by ricardobeat on Thursday May 03, 2012 @12:17AM (#3922357)
Visually? 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.