Live data from Hacker News

Brendan Eich: The infernal semicolon

brendaneich.com

41–50 of 143 posts

Re: Brendan Eich: The infernal semicolon

#41
post #17

Earlier quoted context omitted.

Why not?

http://news.ycombinator.com/item?id=1893686 Mentioned in http://www.aminutewithbrendan.com/pages/20101122 . JS is becoming the compiler target "bytecode", without needing verifiers or new and complex standards to be adopted by multiple browser vendors. JS has gaps as a target language, for sure. We are working on filling them (e.g. 64-bit and other int types). These are easy bugs to fix compared to creating a new, po…

[deleted]

Re: Brendan Eich: The infernal semicolon

#42
Is it me, or did the author tack on a new topic regarding the use of && and ||?

The fact that they return the controlling operand is quite useful. Consider a situation where you want to check the property of an object, but don't know if that object is null:

  var foo = obj && obj.bar;
I find this much more readable than:

  var foo = null;
  if (obj) {
    foo = obj.bar;
  }
You can accomplish the same thing with a ternary operator, but it's longer and, to my eyes, less scannable:

  var foo = obj ? obj.bar : null;
Also useful when setting optional parameters:

  function myFunc(foo, bar) {
    foo = foo || {};
    bar = bar || 'default_value';
  }
(take care however: the empty string evaluates to false, as does [] and 0)

Re: Brendan Eich: The infernal semicolon

#43

Earlier quoted context omitted.

If javascript were any worse, it may have been sidelined entirely before it had a chance to improve, and we would be using proprietary application stacks like iOS even more heavily for client side development.

> If javascript were any worse, it may have been sidelined entirely before it had a chance to improve And we should keep using it in 2012, 17 years later?

JS has improved a lot in 17 years. It's still incumbent, and with ES6 and further Harmony work, it is still improving (much of ES6 are in V8 and SpiderMonkey, or coming very soon).

Good luck displacing JS. I mean that sincerely, especially if you work in the open. Dart had a rough start but even ignoring that, I don't think it will succeed. We'll see.

Re: Brendan Eich: The infernal semicolon

#44
post #42

Is it me, or did the author tack on a new topic regarding the use of && and ||? The fact that they return the controlling operand is quite useful. Consider a situation where you want to check the property of an object, but don't know if that object is null: var foo = obj && obj.bar; I find this much more readable than: var foo = null; if (obj) { foo = obj.bar; } You can accomplish the same thing with a ternary operat…

It's just you ;-). I love && and || being both short-circuiting (as in C) and value-preserving (as in LISPs, Perl, etc.). That's not the issue.

The issue is abusing && or || as the whole of an expression statement, instead of using if.

Re: Brendan Eich: The infernal semicolon

#45

Moar downvotes! Can we reconsider using this thing as the assembler for the web? Can we come up with a sane, cross-platform bytecode standard?

Lars Bak, who leads chrome's v8 team and was tech lead for java's hotspot vm, argues that javascript is a competitive 'bytecode' even compared to java or CLI bytecode [1]. Among other things, Bak claims that javascript source is more compact than traditional bytecode. [1] about 3/4 into this interview: http://channel9.msdn.com/Shows/Going+Deep/Expert-to-Expert-Erik-Meijer-and-Lars-Bak-Inside-V8-A-Javascript-Virtual-M…

I'm personally in favor of leaving JS mostly alone, especially at the syntax layer, and then let transcompiler solutions like CoffeeScript evolve to relieve the syntax burden and add semantic improvements.

CoffeeScript is far from perfect--it has syntax quirks of its own--but it's more pleasant for me to write in, being used to Python and Ruby. (And, yes, I understand JavaScript too; I just don't like the syntax.)

Eventually transcompiler languages will evolve to take advantage of different JS engine improvements. So far, this isn't a goal of CoffeeScript, but other abstraction layers might already be doing that.

Re: Brendan Eich: The infernal semicolon

#46

Earlier quoted context omitted.

Right. To avoid confusion, I updated the post so the sentence reads "ASI is (formally speaking) a syntactic error correction procedure."

Given that these are all formal languages, though, isn't that equivalent to saying that ASI is, formally speaking, part of the language's syntax? It's all just parsers! Apart from the social conventions around them, of course.

No, there's an important distinction. ASI does not even kick in without a syntax error.

Yet the "expectation of ASI" or (I think more likely) "expectation of newline significance" makes people believe that they'll get a ; inserted by separating two things by one or more newlines.

Most languages do not specify error correction. HTML5 of course does; CSS too; among general programming languages it's much less common. The spec for ASI does not fit in the tried-and-true LR(1) formalism used by ECMA-262. Parsing is not all ad-hoc or equally well-formalized and proven.

In addition to ASI, ECMA-262 has to use lookahead restrictions and a bit of semantic checking to cope with what could be purely syntactic concerns (say, if it could use GLR instead of LR(1)).

Re: Brendan Eich: The infernal semicolon

#47
post #42

Is it me, or did the author tack on a new topic regarding the use of && and ||? The fact that they return the controlling operand is quite useful. Consider a situation where you want to check the property of an object, but don't know if that object is null: var foo = obj && obj.bar; I find this much more readable than: var foo = null; if (obj) { foo = obj.bar; } You can accomplish the same thing with a ternary operat…

I can't speak for Brendan, but I generally think && / || are great for assignment, but otherwise it's code-smell.

    var foo = obj && obj.bar; // great
    !isActive && $parent.toggleClass('open'); // smelly
    if (!isActive) $parent.toggleClass('open'); // better

Re: Brendan Eich: The infernal semicolon

#48

Earlier quoted context omitted.

Yes, he's acknowledged many times (including in this post) that it was a mistake. That was the intention, but it wasn't successful. It should have gone all in (no semicolons at all) or not have existed.

I think it was a reasonable decision at the time. The idea was that JavaScript was not going to be some language people wrote hundreds of thousands of lines of code in, and that the target audience would become frustrated if the language didn't "help" them. For example, if JavaScript truly were to always require semicolons at the end of lines then that means this would be a syntax error: ... > Error, missing semicolo…

> For example, if JavaScript truly were to always require semicolons at the end of lines then that means this would be a syntax error:

Depends whether the semicolon is defined as a terminator or as a separator.

This would be valid code with statement separator semicolons.

Re: Brendan Eich: The infernal semicolon

#49
post #42

Is it me, or did the author tack on a new topic regarding the use of && and ||? The fact that they return the controlling operand is quite useful. Consider a situation where you want to check the property of an object, but don't know if that object is null: var foo = obj && obj.bar; I find this much more readable than: var foo = null; if (obj) { foo = obj.bar; } You can accomplish the same thing with a ternary operat…

The problem here, I believe, is the abuse of && (or ||) to emulate conditional statements. Not expressions.

Re: Brendan Eich: The infernal semicolon

#50

Earlier quoted context omitted.

Given that these are all formal languages, though, isn't that equivalent to saying that ASI is, formally speaking, part of the language's syntax? It's all just parsers! Apart from the social conventions around them, of course.

No, there's an important distinction. ASI does not even kick in without a syntax error. Yet the "expectation of ASI" or (I think more likely) "expectation of newline significance" makes people believe that they'll get a ; inserted by separating two things by one or more newlines. Most languages do not specify error correction. HTML5 of course does; CSS too; among general programming languages it's much less common. T…

I suppose I'm thinking of that as implementation details; from the perspective of languages, it appears ECMA-262 does specify a well-defined language. Any input string is either rejected (not in the language at all), or is mapped unambiguously to an abstract syntax tree. So from that perspective, any sequence of characters that gets you an abstract syntax tree is a program in the language! How precisely it gets mapped is "innards of the parser" if you view languages as just string->AST mappings.

The error-correction view seems to want to add a third category, strings that are in some sense "errors", but nonetheless get unambiguously mapped to an AST. Which is strange from a classical formal-languages view, because if a string gets mapped to an AST, it's in the language, and the procedure that mapped it constitutes the parser! That category seems more like "warnings" to me, i.e. you probably shouldn't do this, but it will produce a program if you do.

Post reply on HN