Semicolons in THT

A design note from my work on THT, a programming language for building websites.

There seems to be a trend in programming language design to remove semicolons as statement terminators.

So instead of this:

let greeting = ‘Hello World’;

You have this:

let greeting = ‘Hello World’

As a fan of simplicity, I can see the appeal: most programs have one statement per line, so semicolons are mostly redundant.

Newer languages like Swift make them optional. Ruby and Python seem to be successful without them.

So why are semicolons required in THT?

It mostly comes back to the main design principle for THT: to be familiar to both PHP and JavaScript programmers, because they are the vast majority of the millions of web developers.

PHP has semicolons. In JavaScript, they are optional, but most style guides require them because of potential bugs that come from relying on automatic semicolon insertion (ASI).

Still, I kept wondering if semicolons were a big enough problem that it was worth deviating from PHP and JS.

Eventually, I decided no. For a few reasons:

1)  They are easy to type. The semicolon key is on the home row of the keyboard, and does not require a Shift keystroke.

2)  They are visually lightweight (very little extra “ink” on the screen). At the same time, they are visible enough to notice when you’ve left one out.

3)  Forgetting semicolons might be a common mistake for novices, but after a while, they become muscle memory. In my experience, it’s not a common source of annoying typos. (I overlook commas in JS objects about 100x more often.)

4)  It’s possible that semicolons make line endings more consistent in a way that gives the reader another visual hint at the program structure. For example, most lines will end in “;” or delimiters like “{ … }”.

5)  My initial prototyping showed that removing semicolons would make the parser more complex. Newline characters have to be interpreted in multiple ways and there are no obvious rules for when a line should continue.

I know other languages like Python manage to solve this reasonably well, but I found it is not as straightforward as it first seemed.

6)  Typographically, it’s natural to have a visual terminator in human languages (i.e. a period at the end of sentences.)  I think it’s ok to have a little redundancy here, both for humans and compilers.

So I believe semicolons are right for THT, but I don’t have a strong opinion about it in other languages.

In fact, if I were designing a language completely for myself, I would probably omit them.

As with other programming language design topics, it would be great if there were more usability research available, especially based on eye-tracking.

For more fun on the topic, here’s an older exchange with Larry Wall, on why Perl has semicolons: