What is expressiveness in a programming language? When I began thinking about this, I came up with a lot of different answers. I am not going to try to tell you what the right answer is, because I don't know. I suspect there isn't one, but the candidates are interesting.
- The number of programs in the language.
-
This definition is typically preferred by the argument that expressiveness is not a meaningful quality of programming languages, because all Turing-complete languages are capable of expressing exactly the same set of programs. However, we can bring a lot of nuance into the situation by noting that:
- Real computers aren't actually Turing machines
- Not all programming languages are Turing-complete
- The number of unique programs in the language.
-
This is the definition preferred by somebody who is really ambitious and wants to express lots of useful programs. So, if a language L has ten million ways to print "Hello, world!" but can't do anything else, such a person would not call it expressive. This starts to get at one of the problems of defining expressiveness: we need to know who is doing the expressing, what they might want to express, and how they go about choosing what to express from among the many possibilities. Language L might be a great productivity enhancer at MegaHelloWorldCorp.
One problem with this definition is that it might be equivalent to the previous definition, which we have already grown bored of. Whether or not it is equivalent depends on what you mean by "a language" and what you mean by "unique". What you probably mean by "unique" is not "the same string" but "the same in effect". So you are probably safe.
- The number of different ways to write the same program.
-
This definition says that programs are like poems. The proponent of this argument already has a particular program in mind. (The poet has an idea to convey.) The difficult thing is not finding the program you want to implement, but finding the most beautiful way to write it down.
This is still tricky. Most languages will let you repeat yourself in ways that don't accomplish anything. For example, if your poem in C is two syllables short on the second line, you could try augmenting it like this:
int main(int argc, char **argv) { printf("Answer: %d.\n", 40 + 1 + 1); }This is why poets don't like to have editors: there's always the risk of constant-folding.
- The compactness with which programs can be written.
-
This definition reveals a trend. We have been moving away from expressiveness as something that relates to what can be written and moving toward how it is written. Of course, the desire for compactness presents certain challenges. Should compactness be measured across all programs, or only useful ones? What is a useful program? (It's the program I'm writing right now.)
We might also want to ask why we are interested in writing less, because this will affect how it is measured. Perhaps we are slow readers, and want to reduce how long it takes to read someone else's code. Does whitespace count as making a program longer? What if that whitespace is significant to the meaning of the program? If whitespace doesn't count, does using the period as an operator make a more concise language than using the colon?
One oft-cited example of programming language redundancy is a line like this, from Java:
List<List<String>> foo = new ArrayList<List<String>>();
On the one hand, we can observe that there is a lot of redundancy here. On the other hand, we are gaining some information on both sides of the assignment, since the type we gave to foo is more abstract than its actual type. On the other other hand, if foo is a local variable that's only used for two lines, giving it an abstract type may not be doing enough good to justify all that typing. On the other other other hand, Eclipse would've done all the typing for us if we weren't writing a blog post into a <textarea>.
So at least we're getting somewhere.
Epilogue
I started on this tangent because of this article. I'm still not sure what I think of that blog as a whole, although I know it was interesting enough to read past my bedtime. Although I agree with the spirit of that post, I found his treatment of expressiveness to be a bit narrow.
Also, if you prefer the concrete "look at what I implemented" posts, don't worry. I've been so busy implementing things that I haven't posted in over a year, but one of these days I will.