Semicolons: mostly unnecessary?

Adam D. Ruppe destructionator at gmail.com
Wed Oct 21 20:20:26 PDT 2009


On Wed, Oct 21, 2009 at 09:25:34PM -0500, AJ wrote:
> That's not D source code. Why do you keep trying to use English text as an 
> example? 

The logic behind all the arguments you make, except for one, should apply
equally well to English as it does to D.

 Cons:

 1. Makes source code less comprehensible.

Based on what? Because you say so? I say punctuation makes English less
comprehensible, therefore periods should be removed from the language.

(See below for a more concrete code example.)


 2. Is redundant with the newline designator.

This one doesn't actually apply to English, but is obviously false, unless
you specifically require a line continuation character:

	a = b +
	c

If the newline alone could take the semi-colon's job, that would be two
statements: a = b+; c; and should raise a compiler error.

In the first language with optional colons that comes to mind, Javascript,
that isn't a compiler error. It does some magic (parses it and continues
if the statement is incomplete) to determine if the newline is indeed
a semicolon.


A newline and a semicolon are not redundant unless you specifically define
a statement as being one and only one line. (One language that actually does
this is assembly, and I'm perfectly fine with it there. There are no long
assembly instructions that you'd want to spread across lines anyway.)


Back to the magic of above though. What if you rewrote it:
	a = b
	   +c

Now what is it? Is it a = b+c;? Or a=b;+c;? a=b; is a perfectly reasonable
statement.

It isn't terribly difficult to read ahead and see if the next token makes
sense as the beginning of a statement, but here's the rub:

Suppose I actually meant to write a=b; ++c; but merely made a typo on the
second line!

Now, the magic rule made my stupid error go completely unnoticed. The
compiler accepts it and moves on.


That's not cool - hence, the semicolon's job as an anchor telling the
compiler what I really meant to catch things like that.


 3. Is more typing.

So are periods and a lot of vowels. But nobody (sane) argues that we should
all start using IM and txt-speak in all writing. The logic fails comparison
again.

Moreover, how long does it take to type a semicolon anyway? It is a trivial
key to hit (on EN-US anyway), and besides, actual typing probably isn't
the number one eater of time when coding: it's probably debugging, instead.

How much time would you spend tracking down that a=b; ++c; typo in something
like javascript? If that happens to you once, your entire career, you just
wasted more time than you would ever spend typing semicolons.


-- 
Adam D. Ruppe
http://arsdnet.net



More information about the Digitalmars-d mailing list