Live data from Hacker News

A Strong Mode for JavaScript

docs.google.com

31–40 of 45 posts

Re: A Strong Mode for JavaScript

#31
post #30

Earlier quoted context omitted.

I fail to see how it breaks anything? It is opt-in. Aside from that "use optimize" is indeed a better naming.

It is supposed to be backwards compatible. "use strong" try { foo = bar[maybeMissing] } catch { // only runs in strong mode } The above will take different code paths depending on whether your JS engine supports "strong" mode.

That's a good spot.

I suppose it's a lot easier to declare one's intention to create a backwards-compatible subset than to actually create one.

Hope the V8 people are open to revisions on the details at least.

Re: A Strong Mode for JavaScript

#32

Earlier quoted context omitted.

Personally, I would say foo = ('x' in bar) ? bar.x : 3; instead. The problem with your code is that if the property bar.x exists, but is one of any number of values, like 0 or false, your code will still set foo to 3. Requiring properties to be explicitly created means that you're separating existence from value, which are two very different things in my book.

Which are some of the most insidious bugs possible in JS. I shudder every time I see the syntax in your post's parent. It's a red flag. Your solution is good. I also use a lot of typeof(bar.x) !== 'undefined' since it's very explicit.

Every time?

I mean, very many times (for example) you're pulling in some JSON from a server app, which will have types properly enforced at the database and/or application level. There are still gotchas around defaulting to true, whether or not an empty string is a valid value and so on, but there are many cases where foo || bar is safe enough.

Re: A Strong Mode for JavaScript

#33
post #31
post #30

Earlier quoted context omitted.

It is supposed to be backwards compatible. "use strong" try { foo = bar[maybeMissing] } catch { // only runs in strong mode } The above will take different code paths depending on whether your JS engine supports "strong" mode.

That's a good spot. I suppose it's a lot easier to declare one's intention to create a backwards-compatible subset than to actually create one. Hope the V8 people are open to revisions on the details at least.

This is true of strict mode as well, which shipped in ES5.

    "use strict";
    try {
      a = true;
    } catch(e) {
      // only runs in strict mode
    }
It's true of any mode switch that makes the language smaller. You shouldn't write non-strict (or non-strong) code if you opted into that mode, and catching those errors defeats the purpose of using it.

But it's not only true of mode switches...

    try {
      JSON.parse("{}");
    } catch(e) {
      // only runs in browsers that don't support JSON.parse
    }

    try {
      [ 1, 2, 3 ].forEach(function(x) { /* ... */ });
    } catch(e) {
      // only runs in browsers that don't support forEach
    }
Or even just:

    const x = 10;
    // only runs in browsers that support const
Any language change can cause differences between what executes in one browser vs. what executes in another. What strong mode guarantees is that if your code doesn't throw errors in strong mode, it won't throw errors in non-strong-mode (which is more than many changes guarantee!). Any other kinds of compatibility guarantees are impossible to make unless your changes are literally meaningless.

Re: A Strong Mode for JavaScript

#34

Still trying to sell Javascript? I guess it's getting harder to find cheap engineers willing to put up with it.

Yes, I see so many JS-only front-end job posts these days - nobody wants to touch that crap even with a stick: "let's hire a guy who will only do JS, close him in a separate room, and forget about this scripty language like the horror dream it is".

Re: A Strong Mode for JavaScript

#35

I think I like most of what this document proposes except for the following: In strong code, accessing objects (strong or not) throws on missing properties. New object properties have to be defined explicitly and cannot be removed from strong objects. To me, this seems to break a fundamental aspect of the language. I've found it very acceptable to be able to define an object literal property "on the fly." However, wi…

I agree and I come from a strongly typed background (.Net). It is extremely liberating and useful to just add properties to an object at any time, even ones I didn't create. Kind of like slapping a sticker on something for later reference, whereas a strong types object just explodes when apply you the sticker. I understand that there are performances issues to doing things this way, but it seems like you could have t…

I don't think performance is the primary reason behind this. The motivation section in the article states in the very first sentence:

Silent property failures and the resulting proliferation of 'undefined' are the most prominent mistake in JavaScript, and can be rather tedious to debug (very much like null pointer exceptions in other languages, but much more common).

Having different objects (that were created in the same way and represent the same thing) potentially have different properties at different parts of their lifetime can easily lead to a codebase where you can never be certain of anything about such objects and have to do loads of explicit checking every time you use them. Especially in a larger codebase developed by more than one person.

It's a nice feature for whipping up some quick prototypes though.

Re: A Strong Mode for JavaScript

#36
I don't get this approach, it's backwards. Why can't the JIT assume everything is perfect and spit out information like: Warning: Can't optimize function foo in bar.js because of foobar? Instead devs have to tell the JIT what to assume and wait for errors.

What I'm missing here?

Re: A Strong Mode for JavaScript

#37
post #31

Earlier quoted context omitted.

That's a good spot. I suppose it's a lot easier to declare one's intention to create a backwards-compatible subset than to actually create one. Hope the V8 people are open to revisions on the details at least.

This is true of strict mode as well, which shipped in ES5. "use strict"; try { a = true; } catch(e) { // only runs in strict mode } It's true of any mode switch that makes the language smaller. You shouldn't write non-strict (or non-strong) code if you opted into that mode, and catching those errors defeats the purpose of using it. But it's not only true of mode switches... try { JSON.parse("{}"); } catch(e) { // onl…

Indeed (regarding your first point), my bad. I hadn't read the complete proposal, just the SaneScript slides, where that point was not fully developed. From TFA, emphasis is mine:

However, a mode directive has the significant advantage that any program +not hitting any of the strong mode restriction+ should run unchanged in a VM not recognising the directive, and no translation step should be required.

Your next two points are different: the standard library additions can be polyfilled and the syntax change is intentionally backwards incompatible.

Re: A Strong Mode for JavaScript

#38
post #36

I don't get this approach, it's backwards. Why can't the JIT assume everything is perfect and spit out information like: Warning: Can't optimize function foo in bar.js because of foobar? Instead devs have to tell the JIT what to assume and wait for errors. What I'm missing here?

This would make more sense to me, if not only because it seems like a great way to avoid the most complained-about parts of JavaScript, like weird type coercion.

Re: A Strong Mode for JavaScript

#39
post #32

Earlier quoted context omitted.

Which are some of the most insidious bugs possible in JS. I shudder every time I see the syntax in your post's parent. It's a red flag. Your solution is good. I also use a lot of typeof(bar.x) !== 'undefined' since it's very explicit.

Every time? I mean, very many times (for example) you're pulling in some JSON from a server app, which will have types properly enforced at the database and/or application level. There are still gotchas around defaulting to true, whether or not an empty string is a valid value and so on, but there are many cases where foo || bar is safe enough.

I have to disagree with you there. It's simply not a good idea to have a works-sometimes syntax for "if property is unset". The brevity doesn't make up for the fact that you sometimes have to fallback on the explicit check anyway. It may look clever but it's an abusage, and you're eventually going to get a production error, unless you're testing for it, and if you've got tests for that, brevity has already lost.

Re: A Strong Mode for JavaScript

#40

Earlier quoted context omitted.

use `let ` or `const `. there's no need to use `var` in ES6+, ever.

But we're going to keep it around, aren't we? Because we can always add features but we can't really remove them. It feels like JS is getting cruftier. Would it be better if var already worked like let? Sure. Will JS be better with two slightly different ways of scoping a variable, one of which you really shouldn't use? Doesn't seem like it. One more thing to explain to newcomers.

Hopefully we can tell newcomers to "use strict", always use "let", and to not worry about reading code that doesn't "use strict" to start with?

People who are new to FORTRAN get told "if it doesn't say "implicit none" at the top then don't try to read it yet".

Post reply on HN