Simple
or Complicated ?
Martin
Odersky, 26 August 2010
Recently we have seen a heated debate on whether Scala
is too complicated for normal programmers or whether it’s in fact a rather
simple language to program in. Here are two representative blogposts
of the debate. The comments on the posts are also worth reading.
Reading the posts and the comments I have found that
often people were talking at cross-purpose. So I wanted to add some thoughts
that illustrate that things are often not as simple as they seem.
Which
one is simpler ?
Simpler to build ? No contest.
Simpler to use? Depends. If you have never used a
keyboard before, nor placed a telephone call, you might think it’s easier to
pick up Morse code. For the rest of us, no contest, the smartphone is simpler.
Compared to other languages, Scala is more like the
smartphone and less like the Morse machine. Its compiler and some of the core
libraries are extremely intricate pieces of software, yet the user experience
is liberating and friendly. There is evidence that Scala is indeed very simple
to use. For instance, have a look at the
scala-based Kojo
environment that helps kids do their first steps in programming a computer. Hundreds
of girls in an Indian high-school learn
programming with Scala, so that’s good evidence that neither a Ph.D. nor
super-human capabilities are required to pick it up.
You might argue that the comparison is not exactly
fair. Smartphones and Morse machines are fixed pieces of hardware, yet a
programming environment is open, more like a building kit than a consumer item.
OK, so let’s have a look at building kits.
Which
one is simpler ?
The Playmobil kit on the left has more different
pieces than the Lego on the right, so you could argue that the Lego is simpler.
On the other hand, the Playmobil kit gives you a fixed
scheme how to build something whereas the Lego gives you an almost unlimited
choice. Some people find choice scary (maybe not for them, but if their
co-workers have it). So for them, the Playmobil kit would be simpler than the
Lego one.
Scala is more like Lego than like Playmobil. By
objective measures it is a reasonably simple language with not too many
concepts. For instance look at this comparison
of the number of keywords in programming languages. I did myself a
comparison of context free grammar sizes, and again, Scala comes out as a reasonably
compact language, comparable to Haskell and OCaml, somewhat smaller than Java
5, much smaller than C# and C++.
(Scala sure is more complex as a
language than Turing machines or lambda calculus, but then these are the Morse
code equivalents of programming languages. Scala is also somewhat more complex
than Python or Scheme or Clojure, but then it has a very refined static type
system; this inevitably increases the footprint of a language).
On the other hand, because many of Scala’s concepts
are very general and orthogonal, they can be combined in a large number of
ways. So it is indeed true that almost anything can be achieved in different
ways in Scala. It takes practice to discover that often some ways are
preferable to others. And it takes a certain amount of tolerance to accept that
sometimes there’s more than one route to a good design.
You might still not be happy with the comparison,
because after all, almost every programming language is Turing complete, so on
that level everyone can do what all others can do. Yet it would be impossible
to construct the Sushi set above with the Playmobil kit.
So let’s compare Lego with Lego, shall we?
Which
one is simpler ?
The Lego Duplo kit on the left is clearly simpler than
the Lego Technik on the right, correct?
Yes, but what if you were asked to construct a given
nontrivial thing with either kit? Maybe you could get there with the Duplo, but
it would invariably be clunkier than with the Technik kit. More likely, you’d
need something else beyond your basic Duplo set – pieces of string or glue for
example.
You guessed it by now :-) Scala is more like the Technik
kit than the Duplo. It leads to much more concise solutions than with other
languages. It also can do things such as dependency
injection where other languages need help from external configuration
files,
Inevitably, this means that each line of Scala code
does more work, so you could argue it’s harder to understand than a more
verbose language. But there is empirical evidence that
this is not the case, that in the end, compact code can be assimilated more
quickly than verbose code.
I hope I have convinced you that the debate of simplicity
vs complexity is itself not so simple. There are many dimensions to simplicity
and complexity. Often, in a discussion each participant has a different notion
of what they mean.