Next; tech; meltdown..? Mandatory; semicolons; in; JavaScript; mulled;

Punctuation-averse devs, you're coding it wrong

Javascript photo via Shutterstock

In what non-technical people might take as an attempt to outdo the absurdity of the tabs vs. spaces debate that continues to divide programmers, the TC39 technical group that advises the development of ECMAScript – the specification from which JavaScript is implemented – has proposed telling web developers to terminate statements and declarations with semicolons.

Semicolons help separate JavaScript statements from one another.

A great many JavaScript developers already pepper their code with semicolons, but they often don't have to, even though semicolons are required in most cases by the ECMAScript specification.

That's because there's an out. "For convenience, however, such semicolons may be omitted from the source text in certain situations," the ECMAScript spec explains.

ECMAScript allows programmers, in most cases, to flout its punctuation requirement through a mechanism called Automatic Semicolon Insertion (ASI), which as its name suggests, automatically adds semicolon tokens to mark the end of code constructs when the source file gets parsed by a JavaScript engine.

This is done because programmers, aware that every additional character offers another chance to make an error, appreciate not being held to an exacting standard. It's something akin to a just-in-time spell-checker.

What's more, this laissez-faire approach makes JavaScript fairly forgiving, which in turn makes it more appealing to inexperienced programmers.

Linus Torvalds in sweary rant about punctuation in kernel comments

READ MORE

But there are situations where omitting a semicolon will cause errors or unexpected results, such as when defining JavaScript class fields, because the ASI mechanism gets confused. So encouraging greater use of semicolons would reduce the need to recognize those special cases where they can't be omitted.

Debates about the merits of ASI have been going on for years, and like the perennially unsettled conflict about whether source code should be indented using tabs or spaces, it remains a matter of disagreement.

Brendan Eich, the creator of JavaScript, expressed his disapproval via Twitter, noting that there are other means of enforcing code correctness and that a better approach would be avoiding risky grammar additions.

Were the proposal to be adopted, it would be of little immediate consequence. JavaScript developers could still ignore the recommendation and omit semicolons. ASI would still catch missing marks and add them; other syntax-checking tools known as linters would do the same.

"To be clear, we have no intention to deprecate or remove ASI," said Daniel Ehrenberg, a systems software engineer for Spain-based consultancy Igalia and a former member of Google's Linux kernel team, in an email to The Register.

As one of the co-authors of the proposal, Ehrenberg sees the recommendation as a way to ward off future problems arising from the growing complexity of ECMAScript.

The proposed wording says, "As new syntactic features are added to ECMAScript, additional cases requiring explicit semicolons emerge over time. As such, consistently explicit semicolon use is recommended."

The intention here is to discourage reliance on ASI because there are situations where it may not be reliable and there are likely to be more such situations as ECMAScript evolves.

Nonetheless, the proposal, which has yet to be accepted as part of the ECMAScript standard, has detractors. In the Github comments on the proposal – a pull request – Berlin-based developer Yoshua Wuyts laments, "This makes me a little sad. JS is the only language I write, and I don't use semicolons. I know it's just a warning clause, but it sounds like the TC39 is saying we're bad for using the language in a particular way. And it, feels, well, a little hurtful." ®

Sponsored: Minds Mastering Machines - Call for papers now open


Biting the hand that feeds IT © 1998–2018