News for hackers. Stuff that matters.
Re:Twitter crashes itself with commas by greghinch on Tuesday July 17, 2012 @10:58AM (#4255918)
Can someone please provide a valid, technical reason for not using semicolons other than "it doesn't say I hafta so I'm not gonna!" (that quoted bit should be read as if a 5 year old throwing a tantrum was saying it)
Re:Twitter crashes itself with commas by mdavidn on Tuesday July 17, 2012 @11:18AM (#4256032)
JavaScript minifiers use commas to avoid braces in control statements. The following two statements are functionally identical. Apparently the Closure Compiler goes overboard and just uses commas everywhere, even when the comma offers no length advantage over semicolons.

  if (a) { b(); c() }
  if (a) b(), c()
Re:Twitter crashes itself with commas by sjwright on Tuesday July 17, 2012 @01:07PM (#4256652)
Can someone please provide a valid, technical reason for not using braces other than "it reduces my file sizes by one percent prior to gzipping zomg!"
Re:Twitter crashes itself with commas by mistercow on Tuesday July 17, 2012 @01:12PM (#4256675)
But reducing file sizes by one percent, if done by an automatic tool, is a perfectly worthwhile reason. You don't need any other justification.

Or you wouldn't, of course, if it weren't for some browsers being broken and unable to handle it.

Re:Twitter crashes itself with commas by greghinch on Tuesday July 17, 2012 @01:34PM (#4256770)
Ok, but any front-end engineer worth their salt should be used to the fact that the browser will never be a perfect implementation environment and be willing to adapt around known issues to produce functional code that works for your users. They aren't going to notice that 1% file size. They are going to notice your page not working.
Re:Twitter crashes itself with commas by mistercow on Tuesday July 17, 2012 @01:59PM (#4256897)
Yes of course working in every browser should be the priority, but it is only obvious that this particular optimization would cause a problem in hindsight.

As for "they aren't going to notice that 1% file size", you could keep saying that about small optimization after small optimization until your compiler's output is 10% bigger than the competition's, at which point you will start losing significant user base.

Re:Twitter crashes itself with commas by shock3naw on Tuesday July 17, 2012 @05:37PM (#4258056)
A 1% reduction in output at Twitter's scale isn't as paltry as you might assume. Engineering decisions are made for both server-side and client-side reasons.

You're making a blanket statement about Twitter developing bad code because of a recent bug. It will be fixed.

Re:Twitter crashes itself with commas by GrumpySimon on Tuesday July 17, 2012 @06:38PM (#4258300)
Yes, saving 1% of a few million requests a day is not a trivial savings.
Re:Twitter crashes itself with commas by mistercow on Tuesday July 17, 2012 @07:58PM (#4258575)
I kind of wonder about that reasoning, though. I mean, no it's not trivial if you imagine having to foot the bill for it yourself, but proportional to the kind of money Twitter is dealing with, it's, well, still just 1% of a JS file.

But I don't think we need to think of it in terms of Twitter's scale at all. The point is that saving 1% for basically free, is saving 1% for basically free, and that's worthwhile no matter how big you are. On its own, it's a drop in the bucket, but the cumulative effect of many small low-opportunity-cost savings is a significant low-opportunity-cost saving.

Re:Twitter crashes itself with commas by jlarocco on Tuesday July 17, 2012 @02:54PM (#4257223)
Is there a valid technical reason to use braces?

IMO, the Javascript spec is kinda dumb for being so permissive, but it is covered in the spec, so it's legit to do it either way.

Whether that's a good thing or not.

Re:Twitter crashes itself with commas by hhhhhhhhh on Tuesday July 17, 2012 @01:36PM (#4256779)
in vim A (capital!) jumps to the end of the line in insert mode and semicolons get in the way

this is literally the only objective reason i have ever seen for using/not using semicolons

Re:Twitter crashes itself with commas by OriginalSyn on Tuesday July 17, 2012 @05:56PM (#4258141)
Hacked this together in a couple seconds, there is probably a better way:

    nnoremap A :call EndOfLine()<CR>a

    fu! EndOfLine()
        normal $
        if getline(".")[col(".")-1] == ';' 
            normal h
        endif
    endfunction
Re:Twitter crashes itself with commas by tomjen3 on Tuesday July 17, 2012 @11:14AM (#4256000)
Yes, but I am not going to waste my time with it when you are not willing to debate it in good faith.
Re:Twitter crashes itself with commas by pavel_lishin on Tuesday July 17, 2012 @11:16AM (#4256013)
I'm willing to, please don't withhold enlightenment to spite one guy.
Re:Twitter crashes itself with commas by tomjen3 on Tuesday July 17, 2012 @11:37AM (#4256150)
I write Javascript for a living, and the code standard requires semicolons.

Anyway variables in javascript have function scope, so two variables with the same name in the same function always refer to the same value, even if declared twice.

As a result I like to write all the variable statements at the top of each function, all in one var statement.

The problem then becomes that you write var a, b, c, d; and then later want to add variable e. If you aren't careful you might accidentally write var a, b, c, d; e; if you do that, the program may still work but e now has global scope. This results in nearly impossible to find bugs that may only happen if the function is recursive or it is called (perhaps indirectly) by some other function with a similarily named variable.

Considering that semicolons insertion mostly works, and the other can be a huge pain in the rear end, I can understand why people would want to skip the semicolons.

Re:Twitter crashes itself with commas by DrJokepu on Tuesday July 17, 2012 @12:35PM (#4256485)
I think it's generally a good idea to avoid declaring multiple variables in the same line (unless you have a very good reason). The space you "save" isn't worth the maintainability penalty. This is a habit I have from C where

  int* a, b
declares "a" as ( int* ) and b as ( int ). Instead, I just write

  int *a;
  int *b;
Re:Twitter crashes itself with commas by lmm on Wednesday July 18, 2012 @05:31AM (#4260041)
If you get in the habit of writing the * next to the variable then you can't make the mistake. int *a, b, c[]; perfectly clear.
Re:Twitter crashes itself with commas by tedunangst on Tuesday July 17, 2012 @11:50AM (#4256242)
If you aren't careful, you might accidentally write var a, b, c, d, r;
Re:Twitter crashes itself with commas by thedaniel on Tuesday July 17, 2012 @11:52AM (#4256253)
The JS interpreter shares your preference of writing all var declarations at the top - in fact, at interpretation time they will be "hoisted" to the top of the scope. In other words:

  foo()
  var bar = 1;
is actually run as

  var bar;
  foo();
  bar = 1;
Re:Twitter crashes itself with commas by tomjen3 on Tuesday July 17, 2012 @12:37PM (#4256489)
True, and that was part of what I was trying to articulate.
Re:Twitter crashes itself with commas by OriginalSyn on Wednesday July 18, 2012 @12:49PM (#4261597)
But this is a simple error caught by js(h/l)int. And it's especially easy to spot if you just declare each var one after another:

    var a,
        b,
        c,
        d;
        e;
         
Then your editor (which you've a integrated a linter... right?) will protested:

    yourJsFile.js |5 warning| 'e' was used before it was defined.
And if you have a global by the same name you'll see:

    yourJsFile.js |5 warning| Expected an assignment or function call and instead saw an expression.
Granted, if you assign it a default value then, yes you're going to have a bad time. But if you follow the white spacing rules of jsLint it will catch the obvious error.
Re:Twitter crashes itself with commas by bmuon on Tuesday July 17, 2012 @02:08PM (#4256942)
Strict mode should save you from a lot of those.

    (function () {
      'use strict';
      var a = 1; b = 2;
    }());
    // ReferenceError: b is not defined