JS – Can I stop "use"ing "strict";?

It was during grade school that my analytical skills and thought process was developed. It came largely through the many hours I spent toiling through my Saxon Math and Algebra books, the names of which can strike fear into the heart of nearly any homeschooler. While mathematics was not my favorite subject in the world, I always did very well in it. This was accomplished, as aforementioned, by the many hours I spent not only trying to understand the poorly explained and demonstrated mathematical concepts but by always working to get the correct answer in the correct manner. There was always an answer; I just had to find it. Generally, math always has an answer to the equation. I would sometimes spend close to an hour solving and resolving one question, with 20-something more questions to do, because I did not fully understand how to solve it and was determined to get that coveted correct answer using the correct process.

Thus my deep analytical and logical thought process was thoroughly developed, becoming my greatest strength and biggest weakness. In anything I do, I seek out a good, proper, logical, and most efficient way to proceed, and research questions in a quest to find the answer. Lately a few people have interpreted this behavior pattern as perfectionism, something I truly am not anyway near or wish to be. When I am unable to find a suitable closure for the journey at hand, I often get very frustrated, for the answer I am looking for (and indeed there is an answer; most of my searches are ones that have at least a “best practice” or “generally accepted” ending) cannot be found, and I want to know it. Usually when my exploration hits a dead-end like this, I stop looking it up for a while and maybe the next day, a few days, or a week later, I resume my search, this time with a clear mind and better understanding of the issue at hand. It is rare I have an oft-lingering case without any answer, and if the channel dries up, I compose a new method to perform the same thing.

This post, however, explores one of those unanswerable questions I cannot find an answer to.

JavaScript (or more correctly, ECMAScript, whichever you fancy) contains an ECMAScript 5-introduced directive known as "use strict";, the use of which is highly encouraged by all JavaScript developers and code linting tools alike. According to MDN,

[Strict mode] is a way to opt in to a restricted variant of JavaScript. Strict mode isn’t just a subset: it intentionally has different semantics from normal code.

In short, strict mode, for the better, changes the way we write JavaScript. It corrects glaring errors and issues JavaScript has (after all, the initial version was only created in ten days, disables dangerous functions (good luck creating your own with() function), and reserves certain keywords that may be used in future ECMAScript revisions (e.g. class, which is used in the nearly complete ES6 standard), among other things. Strict mode comes in two different forms, global mode and function mode. As their names imply, the first enables strict mode for an entire script, while the second allows it to be used on a per-function basis:

"use strict";

function one() {
  console.log("I am running in strict mode!");
}

function two() {
  console.log("I am also running in strict mode!");
}
function three() {
  "use strict";
  console.log("I am running in strict mode!");
}

function four() {
  console.log("But I am NOT running in strict mode! Ha!");
}

Of course, like nearly everything in programming, there is a recommended form of "use strict";, namely the function mode. The reasoning behind this is sound: if I ever concatenated the two scripts above, the entire script would run in strict mode, breaking any non-strict code used in four().

For quite some time now, I have pondered and wondered if the day would come that I would not have to "use strict"; anymore. See, I’m tired of having to enable strict mode for every single one of my functions just so I can always use “the good parts”. It has gotten to the point I have started wrapping as much of my code as possible in a closure just so I use a faux global strict mode (with the added benefit of not polluting the global namespace with my variables and functions):

(function() {
  "use strict";
  // Code here
}());

My reasoning is if the parts strict mode limits and/or disables are truly that bad, why have they not been dropped from the language spec yet? Granted, browsers would still have to support the “dead code” for legacy reasons (with() is still defined in ES6, unchanged from previous standards), but maybe somewhere in the distant future even that reason would disappear and developers would be free to define their own with() function with pleasure. Eventually, we may even be able to stop using the strict directive, as all code would automatically be “strict”!

That was until I read Kyle Simpson’s excellent article about JavaScript’s underlying type coercion problems. In that article, Kyle suggested a new directive, in like manner of "use strict";, to force proper coercion behavior: "use proper";. Though Kyle himself said this idea “might, just might, have a tiny fraction of a % chance. Probably zero, but maybe it’s like 1e-9”, it brought me to a realization that I had not considered through all my previous inquires:

"use strict"; is not going way.

This realization was rather depressing, as I had hoped for a day "use strict"; would be a thing of the past. But now, a guest writer on a well-known programmer’s proposes (through his personal perspective) a new directive be added to the language. Judging by the general JavaScript developer reaction to coercion issues, if such a directive ever saw the light of day it would probably become an immediate standard, written directly above or below "use strict";, possibly with its own (discouraged) global and (accepted) function mode, adding more bytes to my scripts, furthering my current enclosure practice, and most importantly affirming that "use strict"; is here to stay.

Think about it: why would a sensible, smart, and experienced programmer like Kyle propose a directive if the directive it drew inspiration from was only a temporary part of the language? Fixing broken type coercion affects the fundamental behaviors of JavaScript. Such a directive would therefore become a permanent part of the language. If strict mode was indeed only temporary, unless the near-mandatory inclusion of a directive was not considered a major bother to include to the vast majority of programmers, such an idea would not be brought up by a man of his type.

Thus concludes my search for the answer of whether or not I can stop "use"ing the "strict"; directive in JavaScript that I write instead of it being implicitly declared strict. It is a rather sad answer, one I certainly was not wanting, but it is what it is. I guess this is what happens when you (and a lot of the developer world) adopt a rushed, sometimes broken, and slow-to-be-updated language as the new medium for client, server, and mobile projects. I will have no choice but to continuing living with strict mode and my enclosures, but it certainly will be a sadder way of life.

Have any thoughts on the permanence of strict mode and/or my reasoning behind this post? Leave me a comment below. I would love to hear your thoughts. 🙂

-le717

Advertisements

One thought on “JS – Can I stop "use"ing "strict";?

Triangular Reactions

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s