This page looks best with JavaScript enabled

Use Strict in Inherited Javascript Code

 ·   ·  ☕ 2 min read

You should use strict mode everywhere, but what about inherited code?

Typically, you designate strict mode at the beginning of the module -

1
"use strict";

But the module and its friends may have millions of lines of code that you did not write and do not have time to fix. If you introduce "use strict" globally (i.e., at the beginning of the module), you start seeing all those errors here and now.

Well, lucky for us - strict mode was designed to be used incrementally.

Strict mode can be included only at the beginning of the module or function.

1
2
3
// CoolModule.js

"use strict";
1
2
3
4
5
6
7
8
// SuperOldModule.js

// lot of code

function getSum(x, y) {
  "use strict";
  // more code
}

So, all you need to do is make two rules (and tinker with your ESLint to enforce those rules) -

  1. Any new module must have "use strict"; at the beginning of the module
  2. Any new function within existing module should have "use strict";. If it cannot be done, avoid creating new functions in the said module

Ensure that all developers understand and appreciate how errors are thrown with and without strict mode, so that they don’t tear themselves apart by all the inconsistent errors popping up during debugging and in production.

Stay in touch!
Share on

Prashanth Krishnamurthy
WRITTEN BY
Prashanth Krishnamurthy
Technologist | Creator of Things