JS – Can I start globally "use"ing "strict";?

I was reading my previous post about the permanence of the "use strict"; directive in JavaScript/ECMAScript in an attempt to gain inspiration for a blog post, when suddenly another unsolved question came to mind, yet again involving JavaScript.

As I previously explained, ECMAScript 5 introduced a new Strict Mode with intentionally different schematics designed to improve rough edges in the language and disable the worst and most broken parts. As I also explained, the strict mode directive, "use strict";, can be used in one of two ways: global mode and function mode. To reuse my example:

"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!");

The first two functions use global mode, the third uses function mode, and the last uses non-strict mode, sometimes referred to sloppy mode. If I were to concatenate these two separate scripts, suddenly all the code would become strict, as the global mode is enabled if the directive is outside any function, possibly rendering any non-strict code inoperable or incorrect. The MDN article on strict mode states this issue very well:

[The global] syntax has a trap…: it isn’t possible to blindly concatenate non-conflicting scripts. Consider concatenating a strict mode script with a non-strict mode script: the entire concatenation looks strict! The inverse is also true: non-strict plus strict looks non-strict. Concatenation of strict mode scripts with each other is fine, and concatenation of non-strict mode scripts is fine. Only concatenating strict and non-strict scripts is problematic. It is thus recommended that you enable strict mode on a function-by-function basis (at least during the transition period).

This behavior is why function mode is the preferred way to enable strict mode, to the point I have increasingly begun writing my code in an IIFE (Immediately invoked function expression) to avoid adding the directive for every single function I write, making my code “automatically” strict. It is also why JavaScript linters flag global mode as a warning unless the message is suppressed.

I would like to draw attention to some of the repeated wording in the MDN article, namely “transition period”. This phrase indicates a time during which sloppy code and strict code are both used in production and browsers do not fully support strict mode. This is why the function mode has become the de-facto method of using strict mode.

I have long wondered why we in webdev continually perform this ritual, and my research into the cause has always come up blank. To this day, I have yet to find a definite answer. The only conclusion I can draw is this way of doing things has turned into a tradition. A tradition we blindly follow without remembering why we follow it, much like Tevye in Fiddler on the Roof.

It seems as if we have forgotten why we "use strict"; function mode without second-guessing ourselves, why linters throw the errors they do, and why global strict mode is so shunned, and because we have forgotten, we have resorted to alternate means to make our programming easier (IIFEs, though this is not necessarily a bad thing) when we could just move the directive to the very top of the script, not inside a function, and leave it be. Traditions are not inherently bad, but when we begin to keep them for the sake of “that is how we have always done it”, then it can become an issue.

It is not as if this will cause much, if any, issues. ECMAScipt 5 was finalized in December 2009. It has been over 5 years since strict mode was finalized. Every single modern browser (even IE 9) fully supports strict mode. Practically any JavaScript used in production in the last years has been developed in strict mode, and by this time legacy, sloppy mode code is no longer updated or has been rewritten and now uses strict mode. The transition period mentioned by MDN has very much ended. So we do we continue to perform this practice intended for only a short time in web history?

This is why I ask if I can start globally "use"ing "strict";. In the last few years, there has been a push in Web development to remove the old and traditional in favor of newer, better ways (old IE vs. Firefox, Flash vs HTML5 video), and the very soon-to-be-finalized ECMAScript 6 spec will continue that drive. Then why, do I ask, amidst all this progress do we continue to use strict function mode like a stubborn mule when we could very well be using global mode and saving many, many bytes in our scripts (and less keystrokes on our keyboards)? It looks to me the “transition period” is over. It is time to move on and let it go.



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