The semicolon; a short history
From a Venetian printer in 1496 to the end of every C statement, and how Go made it disappear: separators, terminators, automatic insertion, and why your brace has to stay on the same line.
The semicolon is a comma that sat on top of a full stop and decided to stay. It's had a strange career: born as a typographer's experiment in Renaissance Venice, slowly abandoned by writers, then rescued by programmers who put it at the end of almost every line they write. And then some of those programmers started trying to get rid of it again.
Venice, 1496
The semicolon as we know it first appeared in print in De Aetna, a short Latin dialogue by the young Pietro Bembo about climbing Mount Etna. It was printed by Aldus Manutius in Venice in February 1496 (1495 by the Venetian calendar, which is why you'll also see 1494–1495 quoted). The type was cut by Francesco Griffo, and it's the same face that was revived in the 1920s as Bembo.
Aldus wanted a mark for a pause that was longer than a comma but shorter than a colon, and he made one by literally stacking a comma under a dot. Medieval scribes had used similar-looking marks (the ; shape had long served as an abbreviation sign in Latin manuscripts), but this was the start of the semicolon as punctuation.
Aldine books were widely copied, and the mark spread across Europe. By Shakespeare's time English printers were using it freely; the 17th and 18th centuries were its golden age, with long periodic sentences held together by chains of semicolons.
How it's used in prose
The rules haven't really changed in five hundred years:
- Joining two complete clauses that are closely related: The build passed; the deploy did not.
- Before a conjunctive adverb: The tests are green; however, nobody has read them.
- Separating list items that already contain commas: Stockholm, Sweden; Oslo, Norway; and Helsinki, Finland.
It has always had enemies. Kurt Vonnegut's advice was not to use them at all, adding that "all they do is show you've been to college." In Swedish, semikolon is rare enough in everyday writing that many people never use it deliberately.
And a small trap: in Greek, the character ; is the question mark. Unicode even has a separate code point for it, U+037E GREEK QUESTION MARK, which normalizes to the ordinary semicolon U+003B and looks identical in most fonts. Replacing one semicolon in a colleague's C# file with U+037E is a well-known (and cruel) prank: the code looks fine and doesn't compile.
Enter the computer
In ASCII the semicolon is 0x3B, decimal 59, and it immediately found jobs that had nothing to do with pauses:
- Comments in most assemblers, in Lisp, and in INI files:
; this is a comment - Separators in data: in Sweden and much of Europe the comma is the decimal separator, so Excel "CSV" files are often semicolon-separated:
namn;pris/kaffe;32,50 - CSS declarations:
color: red; margin: 0; - Shell command lists:
cd /tmp; ls, and;;to close acasebranch - SQL: the end of a statement, which most interactive clients wait for before running anything
- MATLAB: a trailing
;suppresses printing the result
But its big role is in programming languages, where it marks where one statement stops and the next begins.
Separator or terminator?
This is the oldest argument about the semicolon in code, and it goes back to ALGOL 60. ALGOL used the semicolon as a separator: it goes between statements, the way it goes between clauses in English. Pascal inherited that. C, via B and BCPL, went the other way and made it a terminator: every simple statement ends with one.
The difference shows up right before an else. In Pascal the semicolon would separate the if from its own else, which is a syntax error:
if x > 0 then
writeln('positive') { no semicolon allowed here }
else
writeln('not positive');
In C it's the opposite, the statement before else must be terminated:
if (x > 0)
printf("positive\n"); /* semicolon required */
else
printf("not positive\n");
C also has the famous empty statement, which is how a stray semicolon turns a loop into a bug:
for (i = 0; i < n; i++); /* loops n times doing nothing */
total += a[i]; /* runs once, after the loop */
for (;;) { } /* the idiomatic infinite loop */
The whole C family — C++, Java, C#, PHP — kept the terminator. SQL, Erlang and a few others use it as a separator. Erlang is the most "English" of them: , separates expressions, ; separates alternative clauses, and . ends the function, almost exactly like a sentence:
fact(0) -> 1;
fact(N) -> N * fact(N - 1).
Line ending or not
Most languages people pick up today don't make you type semicolons at all, but they disagree on why.
Newline is the terminator. Fortran, BASIC, Python and Ruby treat the end of a line as the end of a statement. Semicolons are still allowed, but only to squeeze several statements onto one line, and style guides discourage it:
x = 1; y = 2 # legal, but PEP 8 frowns at it
Optional, inferred by the compiler. Kotlin, Swift and Scala treat a newline as a statement end when that makes sense, and let you use semicolons if you want. Haskell goes further: its layout rule translates indentation into explicit braces and semicolons before parsing, so you can write either form.
Optional, with "automatic insertion". JavaScript requires semicolons in its grammar, but the parser inserts them when it hits an error at a line break. The rules are subtle enough to have their own camps (semicolons always vs standard style, never), and they produce the classic trap:
function config() {
return // a semicolon is inserted here
{
debug: true
};
}
config(); // undefined
Meaningful. Rust is the interesting one. In Rust almost everything is an expression, and the semicolon turns an expression into a statement, throwing away its value. Leaving it off the last line of a block is how you return a value:
fn five() -> i32 {
5 // returns 5
}
fn broken() -> i32 {
5; // error: expected i32, found ()
}
One character, and the function's type changes.
Go: semicolons everywhere, and nowhere
Go is the language where the semicolon question was actually argued out in public.
When Go was open-sourced in November 2009 it used semicolons Pascal-style, as separators, with some special rules for when you could leave them out (after a closing brace, for example). In practice, Go code from that time had semicolons sprinkled in some places but not others, and you had to know the rules.
On 9 December 2009, a month after the release, Rob Pike posted a proposal to the golang-nuts mailing list on behalf of the Go team: get rid of them. The idea came from BCPL, the grandparent of C, which let you leave out a semicolon at the end of a line. Pike's note said that if they had thought of BCPL's approach at the time, they would almost certainly have followed its lead.
The proposal was simple. The grammar stays C-like, with semicolons as terminators. But the lexer inserts one automatically at the end of a line if the last token on it is:
- an identifier or a literal (number, string, rune),
- one of the keywords
break,continue,fallthrough,return, - one of
++,--,),],}.
The response on the list was mostly positive. Within a short time it was implemented, gofmt was used to rewrite the whole source tree, and semicolon-free Go has been the norm since. The Go FAQ sums up the position: semicolons "are for parsers, not for people."
The semicolons are still there, you just don't type them. You can write them out explicitly and the code compiles; gofmt removes them again. And you see them in the for clause, which is exactly the same C syntax:
for i := 0; i < 10; i++ {
fmt.Println(i)
}
if err := run(); err != nil {
return err
}
The price: where your brace goes
The rule is purely lexical, and the lexer doesn't know what you meant. So this:
if x > 0
{
fmt.Println("positive")
}
becomes if x > 0; followed by a free-standing block, and it's a syntax error. In Go the opening brace must go on the same line. Same thing with a multi-line function call: the last line needs a trailing comma, because a line ending in an identifier would get a semicolon:
result := compute(
alpha,
beta, // trailing comma required
)
That was where the debate continued. Over the years people on golang-nuts asked to be allowed to put braces on their own line (Allman style), called semicolon injection a kludge, and suggested alternatives like an explicit line-continuation character or a smarter parser. The Go team stayed with the lexer rule. The argument was that it's simple enough to fit in a few lines of spec, it never needs to look ahead or backtrack, and combined with gofmt it ends the brace-style arguments for good: there's exactly one place the brace can go, so nobody needs to argue about it.
That's a very Go answer. Instead of making the semicolon optional and letting everyone choose, like JavaScript does, Go made it invisible and took the choice away.
Five hundred years of pausing
Aldus Manutius invented the semicolon to mark a pause between two thoughts that belong together. Programmers turned it into the period at the end of every instruction, and then spent decades trying to make the computer put it there for them. It's still in every Go program, though, inserted by the lexer and never shown. The semicolon survives as the punctuation of a sentence that could have ended, but didn't.