JavaScript: Just use the damned semicolons already


In honor of resurrecting my blog (and while waiting for a long compile to finish), I'm going to repost some old, still relevant, Google+ posts that not a heck of a lot of people have read (given how Google+ isn't exactly the most popular social network out there), on here. I'm only going back in time about a year, though. I'm also only going to post long, "thoughtful" (at least I think so) stuff. Reposts of amusing pictures or links to articles by others will not be showing up here.

With that, my first post is on JavaScript and semicolons (what better way to start than with a holy war?).

My wife has decided to start learning JavaScript and, in order to help her out, I've also decided to learn it in my spare time. Of course, given that I'm already a professional programmer (albeit in other languages), learning JavaScript for me basically consists of learning its syntax, quirks, best practices, and maybe a commonly used library/API or two. It's more difficult for her as she doesn't have any programming experience at all (except for dabbling a bit in ActionScript years ago). However, she just wants to be able to use a bit of JavaScript in her web designs (she's a designer, not a programmer) and not be a full-time programmer.

Anyway, as part of my own reading up on JavaScript's best practices, I came upon the whole "do I or don't I use semicolons" debate. The answer for me is simple, "Use the damned semicolons!" and that's also the answer I've given my wife. Given that this point of view has been argued by Brendan Eich (the inventor of the language) and Douglas Crockford (a noted expert and member of its standards committee), among others, this is certainly the most rational side of the argument to take. Of course, I also have my own opinion that I formed based on what I've read from those two as well as my own understanding of the language standard.

Now, I've seen some in the "don't use semicolons" camp argue that "Well, Python/Ruby/etc. don't need semicolons, they do automatic semicolon insertion, so it's okay to use JavaScript's ASI on every statement too!" Gaa, some of the stupid in that statement just burns.

First of all, JavaScript is NOT Python or Ruby. The standards for those languages do not apply at all to JavaScript. If you're using their standards as a reason to do or not to do something in JavaScript, it makes me wonder just how much you understand programming languages and standards at all.

Second, by claiming that Python and Ruby do ASI (their exact words!), it shows that you don't know Python and (probably) Ruby that well either. Python does NOT do ASI. The Python standard explicitly defines the newline as the statement terminator. If you want to continue a statement across multiple lines, you either need to escape the newline with a '\' character or have the statement enclosed within parentheses, brackets, etc. Newlines are all that matter and the interpreter doesn't give a rat's booty about semicolons when it comes to statement termination. I don't know Ruby that well, but I think it's standard also states more or less the same thing (though I think you can use the semicolon to combine multiple statements on a single line – but that's the only case where semicolons between statements matter). Making this argument is akin to arguing that you should use line numbers in all your programs because that's how BASIC did it on your Commodore 64. FWIW, those old BASICs allowed you to combine multiple statements on a single line with a colon (':'), but nowhere in their documentation did they say anything about doing "automatic colon insertion" (that sounds kinda wrong, doesn't it, now that I think about it?) between different lines of code.

Third, the JavaScript standard explicitly states that the semicolon is the proper statement terminator. ASI is there not to be used regularly, but as a way to allow the JavaScript interpreter to better cope with malformed JavaScript, similar to how web browsers are able to muddle along and render something useful if the HTML is malformed. In other words, the standard states, "You should use semicolons to separate your statements, but in case you screw up for some reason, we'll insert them for you." Even Brendan Eich has stated that ASI was meant as an "error correction" feature and not the primary way of terminating statements in the language.

Fourth, you can't write completely semicolon-free JavaScript anyway. If you write semicolon-free JavaScript, every once in a while you're going to have to do something silly like put a semicolon at the beginning of a line in order to get the damned thing to work right.

Admittedly, there are some funky issues that ASI brings up even if you do use semicolons (such as inserting a semicolon on lines that contain solitary return statements before the newline), but the fact remains that the standard defines the semicolon as the proper statement terminator. I also admit that there may be an aesthetic reason to prefer writing semicolon-free (though I don't see what the big deal is – properly working code looks just as good to me with or without semicolons), but hey, the language says you should use them, so use them already. Personally, I've got no issue in general with a language using semicolons or newlines to separate statements, so long as programmers use the proper way as defined in the language's standard to separate their statements.

If I could go back in time and talk to Brendan Eich about the JavaScript design, I'd tell him "Look, ASI is a dumb idea. Either you make the semicolon the statement terminator or the newline the statement terminator. This half-assed 'we'll put semicolons in for you where needed if you forget them' notion is just going to lead to problems." I think even Brendan Eich admitted something similar in a Tweet where he stated, "if I could go back in time, I'd make newlines more important in the language." Presumably, I think that means that the newline would be the primary terminator and semicolons would only be used for multiple statements on a single line. That is actually quite reasonable, IMHO.

Maybe some future version standard will clean this up. I think the only way to do so would be to explicitly make the newline, and not the semicolon, the primary statement terminator and reserve the semicolon for multiple statements on a single line (semicolons at the end of a line would just parse as empty statements). I think this could be done without breaking existing code, but hey, I'm just a language user, not designer, so what do I know?

Current rating: 5


There are currently no comments

New Comment


required (not published)