/ Coding

Is 'Use Strict' Worth It?

A conversation about 'use strict' came up yesterday at work. I was on side of it being unnecessary, as tools like JSHint and such can handle most of what 'use strict' does. But a co-worker smartly pointed out that it's not just about syntax checking that counts, that it also has benefits at runtime.

Now I'm curious to know what type of benefits it really provides. In theory, performance should improve, although I'd really need to see some numbers on that to believe it has any sort of noticable difference.

The biggest benefit I can see is that 'use strict' will find runtime errors that static analysis tools don't. Immutable objects are a great example. In 'use strict' mode, an error will be thrown when changing a 'read only' property (versus failing silently). This can easily save an hour of debugging time wondering why a property isn't getting set.

One other minor benefit is that eval() is safer. eval() is known for introducing security holes, so anything to help patch that up is welcome.

So with those benefits, it's a no-brainer for use strict, right? Well, my biggest hesitation/concern with this is that you are now required to add boilerplate code to every file for at least the next five years.

If it were only one line, this wouldn't be such a big deal, but in reality, you need to now wrap your code in an IIFE to avoid issues with third-party scripts not being able to run in strict mode.

Inevitably you're going to forget to add 'use strict' to a file (or you'll just get lazy and stop adding it). Either way, your code now has established an inconsistent pattern, which makes it slightly more difficult for newcomers to understand what the correct pattern is. (Edit: Some folks have pointed out that a good linter can check for this, which makes this point moot if you have that set up, which you have set up, right? :silently_judges:)

One final very minor concern is what happens when ECMAScript wants a more strict mode? 'use more strict'? I admit it's a pretty petty argument, but I think it's still worth considering.

So is it worth it? I haven't decided. I'd love to hear what others have to say, especially if I'm missing out on any major benefits/drawbacks.

Comments aren't a thing on my blog right now, so if you do have a thought, reach out on twitter or write your own blog response.