programming in the
twenty-first century

It's not about technology for its own sake. It's about being able to implement your ideas.

A Short Quiz About Language Design

Suppose you're designing a programming language. What syntax would you use for a string constant? This isn't a trick; it's as simple as that. If you want to print Hello World then how do you specify a basic string like that in your language?

I'll give you a moment to think about it.

The obvious solution is to use quotes: "Hello World". After all, that's how it works in English, so it's easy to explain to new students of your language. But then someone is going to ask "What if I want to put a quotation mark inside a string? That's a legitimate question, because it's easy to imagine displaying a string like:

"April 2013" could not be found.

There are a couple of options to fix this. Some form of escape character is one, so an embedded quote is preceded by, say, a backslash. That works, but now you've got to explain a second concept in order to explain strings. Another option is to allow both single and double quotes. If your string contains single quotes, enclose it in double quotes, and vice-versa. A hand goes up, and someone asks about how to enter this string:

"April 2013" can't be found.

Ugh. Now you have two kinds of string delimiters, and you still need escapes. You need to explain these special cases up front, because they're so easy to hit.

What if instead falling back on the unwritten rule of using single and double quotes, strings were demarcated by something less traditional? Something that's not common in Latin-derived languages? I'll suggest a vertical bar:

|"April 2013" can't be found.|

That may be uncomfortable at first glance, but give it a moment. Sure, a vertical bar will end up in a string at some point--regular expressions with alternation come to mind--but the exceptional cases are no longer blatant and nagging, and you could get through a beginning class without even mentioning them.

(If you liked this, you might enjoy Explaining Functional Programming to Eight-Year-Olds.)

permalink May 11, 2013



twitter / mail

I'm James Hague, a recovering programmer who has been designing video games since the 1980s. Programming Without Being Obsessed With Programming and Organizational Skills Beat Algorithmic Wizardry are good starting points. For the older stuff, try the 2012 Retrospective.

Where are the comments?