167 points · by MatthewPhillips · 14y ago · 111 comments
When you replace a thousand of them with commas, nothing has been gained!
When you start placing them only at the beginning of certain lines, subject to JS's parsing rules, you are thinking more, not less!
What does it gain you?
Anyway in this case I guess some automatic code reduction tool has decieded that four characters can be saved by changing it to commas. I doubt it was written that way to begin with.
I love Go's lack of semicolons but I don't extend that to some foolhardy attempt to write JavaScript without semicolons. In JavaScript, support for proper semicolon-less style is clearly lacking in practice if not in spec (if it weren't lacking this wouldn't be a oft-recurring story, but it is).
On the other side, this is why many of the comments, including the top comment, seem overly invested in the notion of semicolon insertion, as opposed to looking at the specific usage of replacing sequences of ExpressionStatements (even if separated by semicolons) with a single ExpressionStatement holding a very long compound Expression (separated by commas).
The actual bug appears to be not a bug so much as a limitation in the Opera parser with regards to how many comma delineated calls it can handle at a single time. Somewhere around comma 1019 things get messy.
In this case both the current title and the original title (if memory serves) give the impression that twitter is somehow at fault.
I think it's pretty sane to suggest that a single statement won't be 4MB in length.
The fact that you think otherwise is precisely the problem. If you don't want to write correct software (which, frankly, isn't any harder than writing broken parsers) you might want to reconsider your career choice.
For a very simpel reason -- you are not implementing a new language, but writing a parser/evaluator for an already existing one. And you shouldn't change the language (as much as all of us have a beef with Javascript).
> I believe it's Google Closure compiler putting commas in there.
Someone else linked to an explanation of why
> http://blog.vjeux.com/2011/javascript/javascript-comma-trick...
</fact><opinion>
So from the looks of it, the problem is in Opera's javascript implementation.
I'm not sure how much Google uses Closure Compiler internally but I wouldn't have thought they would use code they knew to cause issues in some browsers. I guess that ether means the Opera implementation is wrong and the code should work fine, or Google didn't test this as fully as they should have done
Regardless, the actual bug is in Opera as the resulting javascript is legit, even if its absolutely crazy to have 4MB of script be only a single javascript statement.
Conclusion - Using the comma trick to do { }-less indentation is far from viable. However this may still be useful for debugging and overall it is fun to try new coding styles!
if(true){dothis();dothat();}
if(true)dothis(),dothat();We already knew that we had a limitation that other browsers don't have in the parser, and it was already considered important to fix. Breaking Twitter of course raises the profile of this bug.
if (a) { b(); c() }
if (a) b(), c()Or you wouldn't, of course, if it weren't for some browsers being broken and unable to handle it.
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.
this is literally the only objective reason i have ever seen for using/not using semicolons
nnoremap A :call EndOfLine()<CR>a
fu! EndOfLine()
normal $
if getline(".")[col(".")-1] == ';'
normal h
endif
endfunctionAnyway 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.
I'd have to imagine that 355kb is the compressed size, but that character count (assuming one byte characters, not UTF8) works out to be 3.938 megabytes.
That's a lot of code. Even more when you consider it's been minified.
It's 1.34mb on disk uncompressed but still minified. The copy Twitter is hosting right now is 1409121 characters minified but uncompressed. I have to assume the 4mb mentioned is output from some middle step (closure compiler output?) I'm not arguing the merits of that filesize, just that it's definitely not 4mb.
What on earth are the people at twitter doing that warrants that much code? Maybe their single page app -> dedicated page transition was more about disabling some things and rewiring urls with the plan of taking advantage of the dedicated setup later?
C, Pascal, and ALGOL (1958) use semicolons as a statement delimiter. Why was the semicolon chosen instead of, say, the period or newline? BCPL (like JS and Go) allows semicolons to be omitted if a statement ends unambiguously on one line.
Was the semicolon a QWERTY home row key before or after ALGOL?
Before, well before. Take a look at http://www.etsy.com/listing/10230221/immaculate-1930s-reming... for a picture of an old typewriter. The ; is exactly where it is today.
As for why semicolons are used, see http://programmers.stackexchange.com/questions/139482/why-ar... for a bunch of speculation on it. The one that seems most reasonable to me is dan04's answer (which was not voted up much because he put it lately.
Start with ASCII. Now take out the characters which did not reliably appear in character formats, take out characters that have meaning to us in mathematical expressions, have obvious utilities as delimiters in other context, or which convey mood. You're left with ; and :. Of the two, ; is more visible and easier to type. So ; it is.
It makes sense when you think of an entire resolution (program) as a single goal (output/result), which involves many separate steps/clauses (statements) that need to be executed.
A period is a terminator; it says 'This thought ends here', while a semicolon is a delimiter (ie, the clauses it separates are independent grammatically, but not contextually).
[1]http://web.utk.edu/~modelun/resolutions.htm (Model UN rules, but you get the idea).
Whereas John Q. Doe is an upstanding citizen who has contributed to the city, and
Whereas aforementioned Mr. Doe is an awesome dude, and
Whereas some people who happened to contribute to my campaign are fans of Mr. Doe,
Therefore, I, Mayor Wile E. Coyote do hereby declare Octember 31st, 2012 as John Q. Doe Day in the city of Acme, CO.
In this context, the UN resolution is actually similar to the comma-style used here by Twitter. We don't want the statement/sentence to end, so we use semicolons/commas to make it continue, and end up breaking browsers/non-lawyer readers.A period is indistinguishable from a decimal point.
So if you have a = 4. is that the end of the thought or the end of the integer? It's obvious in context to a human, but a bad idea for a computer.
Pascal takes this literally: semicolons separate statements, and a full stop ends the unit contained in a file ("end.").
Although, if I'm not mistaken, the exclusion of the semicolon is technically correct. So, both parties are at fault here.