Write Once, Pwn Everywhere…. oh and Jetpacking around Object Oriented Design is all fine and dandy, but how about thinking about the real world?
May 26

Icky? Using strings to add legacy features in languages

JavaScript, Tech Add comments

strings

A contingent in the JavaScript community is pretty much solely focused on securing the language. Security is always important, but even more-so when you are dealing with a language that runs in a user agent to run the Web.

Whenever you try to add a feature to a language you are instantly dealing with legacy and backwards compatibility.

Perl has a strict mode that is very popular in the community. I remember projects where if I checked in a module without use strict; and -w I would get beaten up.

Back to JavaScript, how can we apply stricter rules on what we do in the JavaScript runtime after the face? One solution that the ECMAScript Edition 5 folks came up with is borrowing use strict but since they couldn’t add it to the language itself, they have you put it in a string "use strict";. You can place it at the top of the file, or even as the first statement in a function.

I find it incredibly ugly, yet pragmatic. It works.

However, then I saw some new items:

"use strict,cajita";

And currently you can’t switch the order around? It looks for absolute keys? That seems a little crazy. Surely all of these should be possible:

"use cajita,strict";
"use strict,cajita";
"use strict";
"use cajita";

Then this little pragmatic hack starts to get real messy. I start to joking imagine a ludicrous world like this:

(function() {
  "use strict";
  "use let"; // let's get let in there!
  "use cramda"; // I love Alex's cramda, let me use it!
  "use e4x"; // XML inline. Screw templates
 
  // If I see a var with the same name, REALLY use let instead
  "let x;"
  var x;
 
  // Now I can use the cramda
  "x = #(a, b) { "\
  " ... logic here ... "\
  "};"
 
  // And the let... ironic that it is in a string now
  "let y = <xml></xml>";
 
  // Annotations come to JavaScript!
  "@Serializable"
  function someThing() {}
})();

I like to think of myself as pragmatic, but why do I feel so cranky about "use strict";? :)

6 Responses to “Icky? Using strings to add legacy features in languages”

  1. unscriptable Says:

    Totally agree. I’m hoping this is a short-term remedy. Let’s all hope the current generation of browsers is replaced a lot faster than the previous generation! (so it really can be a short-term remedy)

  2. Jeff Haynie Says:

    OMG that’s just not right…

  3. fearphage Says:

    I’ve read about “use strict” but not about any of the others. Where’d you see “use cajita” for instance?

    @unscriptable: All browsers are note being replaced with faster engines. Let me present to you exhibit IE8. It’s faster than IE7 but still slower than every (literally every single solitairy) browser out now.

  4. Leo Horie Says:

    I wonder what exactly is the point of going through all this trouble to avoid syntax errors, if programs won’t actually be backward-compatible anyways… (I mean, who in their right mind would code things twice like “let x;”;var x; ?)

  5. Simon Says:

    You know, what you need is something like an XML DOCTYPE for Javascript, that puts the engine into strict mode, but doesn’t open the door to the kind of abuse you’re talking about…

  6. Nathan Reynolds Says:

    You’re not adding extra capabilities to the language, you’re only asking the parser to be stricter with its rules. It’s a debugging aid.

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: What is the number before 3? (just put in the digit)

Loading...