blog

Doug Crockford

Style Wars

by

I recently realized that semicolons might be optional in JavaScript. I guess this is something I kinda sorta always knew but didn’t want to think about. JavaScript is a mess of proprietary non-standards, right? Well, maybe ten years ago. It’s okay to care about the syntax now that it’s reliably deterministic.
So should I include them or omit them? Obviously the Spirit of the Art & Logic Style Guide would have the semicolons included. But I still need to ask myself the question for …reasons. I’m not going to be satisfied without understanding all the factors.
But wait, why are there factors? Why isn’t it common knowledge that everyone should stop typing all those “extra” semicolons, case closed? Well, it turns out that semicolons are only optional sometimes.
Apparently I was supposed to have this conversation in 2012 when there was a big disagreement. Sorry I’m late. Regardless, I notice a lot of slanted misinformation is still widely available on the Internet, as usual. So I’m hoping to help the situation some more by talking about only neutral, practical, real-world reasons for choosing a style.
(Disclaimer: My reasons are not really unbiased.)
The aforementioned disagreement isn’t worth reading because it’s full of that slanted misinformation. Instead, you should check out this article:
http://inimino.org/~inimino/blog/javascript_semicolons
It just explains the JavaScript specification (the only place where it’s called ECMAScript, strangely enough) neutrally and lets you come to your own conclusion. Really, it’s worth reading. (It says the pertinent section in the ECMAScript spec is 7.9, but as of this writing it’s 11.9.)
So the JavaScript specification says semicolons are required, but “for convenience” may be omitted at the end of a line, usually. The spec includes “Automatic Semicolon Insertion” rules covering all possible situations.
Understanding the rules eliminates superstitions. Some people say these rules are ambiguous, or that browsers are free to interpret them ambiguously. That’s obviously bunk, or else too many more web pages would be broken than are already. Moving on.
Real reasons against semicolons:

  • Less clutter.
  • Intimidates other programmers!

Real reasons for semicolons:

  • JS is based on C language where the semicolons are required. “Automatic Semicolon Insertion” was actually a compensation to allow sloppy code.
  • If you simultaneously program in other languages based on C besides JS, consistency in style would be helpful to you.
  • Exceptional cases requiring semicolons don’t need to be minded. The syntax is complicated compared to Lua or (legacy) T-SQL, which don’t have such exceptional cases. (And the best practice for exceptional cases is to prefix the line with a semicolon, which I personally despise.)
  • More explicit programmer intent. Of course, this matters the most when I’m fixing your broken code, but it could also matter when you’re fixing your own code.
  • Some minification tools are broken and won’t handle your source right without the semicolons.
  • Some minification tools insert semicolons to put the whole JS on one line, reversing your omissions.

I highly recommend you try the opposite of your usual practice on some real-world code just to see what happens. Then you won’t be just speculating, and you might learn something unpredictable. Incidentally, the minification tool I’m using at the moment is completely unaffected by this decision; the outputs are 100% identical.
You may notice I didn’t mention anything about the JavaScript syntax possibly becoming more strict in the future. That’s because that’s not a real concern; sloppy JS will always be supported by browsers just as sloppy HTML continues to be supported. That’s how we got into this “optional semicolons” mess in the first place!
I also didn’t mention the special rule affecting return statements. That’s because that’s a separate problem that isn’t directly solved by the use of semicolons.
What I learned from all of this is that there are several exceptional cases where semicolons need to be included even though they’re optional usually. For me, that’s the biggest giveaway. It means that a practice of omitting them requires extra vigilance against human error, which simply isn’t worth it. In addition, the exceptional cases help illustrate that the underlying intention of JavaScript is for the semicolons to be there, and deliberately omitting them is a rebellion you might have to take back someday. Once again, my advantage in perceiving this situation is due to experience in a variety of languages. Omitting semicolons in JavaScript is inconsistent and goofy compared to other languages where they really are optional.
Of course, another factor is whether or not other people are ever going to look at your code. For somebody out there at some time, the factors I listed above will weigh in favor of omitting semicolons, and that’s okay. As long as I really won’t have to fix your code someday.
Either way, you should actually learn the syntax and erase superstitions.

* * *

Incidentally, That JavaScript Guy won the big disagreement. Not that that should necessarily sway you.

+ more

Accurate Timing

Accurate Timing

In many tasks we need to do something at given intervals of time. The most obvious ways may not give you the best results. Time? Meh. The most basic tasks that don't have what you might call CPU-scale time requirements can be handled with the usual language and...

read more