Thursday, September 8, 2011

A little syntax is nice

Ok, I was starting to comment on this blog post and it got ridiculously long:
What's wrong with Clojure syntax?

Clojure definitively has more syntax than Scheme and it is one of the reason I like it. A little syntax goes a long way. I guess we all have our own preference of how much syntax we like in a programming language.

In my case, Clojure is pretty near the sweet spot between too much or not enough. I love Ruby but too much syntax for me. Same goes for Scala. (I am not critiquing these languages. I am merely stating personal preferences.)

I tried to learn Common Lisp or Scheme, not enough syntax. Having a Lisp with almost no syntax is pretty hard (for me.) If there's no syntax, you have to remember arguments orders, keyword arguments, workflow if you're calling a macro. Was element the first arg or was it the collection in the "for" construct?

In contrast, "For each x in xs : do something" or its variant is pretty easy to remember or recognize in most popular non-Lisp languages.

On top of small visual clues to differentiate lists, vectors, sets and maps, Clojure has consistent destructuring conventions (which I would call syntax, but might not strictly be.)

Rich Hickey himself argues for using some syntax, and more clearly than I.
I assume quoting the article does not violate any copyrights.
"Since we were talking about syntax, let’s look at classic Lisp. It seems to be the simplest of syntax, everything is a parenthesized list of symbols, numbers, and a few other things. What could be simpler? But in reality, it is not the simplest, since to achieve that uniformity, there has to be substantial overloading of the meaning of lists. They might be function calls, grouping constructs, or data literals, etc. And determining which requires using context, increasing the cognitive load when scanning code to assess its meaning. Clojure adds a couple more composite data literals to lists, and uses them for syntax. In doing so, it means that lists are almost always call-like things, and vectors are used for grouping, and maps have their own literals. Moving from one data structure to three reduces the cognitive load substantially."
Taken from Rich Hickey Q&A, an interview by Michael Fogus, which appeared in Code Quarterly, the Hackademic Journal.