A response to “Erlang - overhyped or underestimated”

There is a blog post about Erlang which recently cropped up. It is well written and puts forth some critique of the Erlang/OTP language. Naturally, I have a bias. I write a lot of Erlang and I like the language - and anything less then a blog post myself would not be fair: There is much to discuss and a fleeting Twitter comment or a comment below the original post can’t convey the information needed.

Erlang is like an exotic beautiful woman with no dressing sense.


I Love this comment from the article. There is truth in it: underneath the clothes of Erlang, beauty is hiding. Yet, I feel that one might have misunderstood the dress code in the exotic world, which is why I sat down, C-x C-f’ed a new buffer and began writing.

1. Today’s mainstream developers who are used to C or Java like syntax wont find its Prolog-like syntax too friendly.


This point, the syntax point, comes up all the time. The claim is that Erlangs syntax is too far away from “mainstream” languages, whatever that means. I don’t think the critique is warranted, but since we have alternative languages like Efene and Reia there are a least some people who think Erlang has a syntax problem, so it warrants discussion.

Perhaps, one should be nuanced and hammer through the difference between syntax and semantics. Syntax is, roughly, the rules for forming valid sentences in the programming languages. Whereas semantics roughly is the meaning of the language, what it denotes. In other words, syntax are rules for transforming a valid input text into an internal parse tree — semantics the rules for executing that parse tree, running the program.

There is a tendency to focus more on Erlangs syntax than its semantics. I think this is partially wrong: the semantics shape the syntax and vice versa. I also have a hunch that people may claim a problem with the syntax of Erlang, where the point is really a misunderstanding of its semantics.

Is Erlangs Prolog-like syntax unfriendly? I don’t think so! Erlang has extremely few syntax rules and they are quite consistent. The grammar is less than 500 lines. Contrast with the Ocaml parser 3 times as large. The Go programming language has a parser which is well above in size as well - I cannot imagine the parser for Java nor C be any smaller.

The main objection is familiarity: “It doesn’t look like Java!” I think the point is somewhat moot. Python doesn’t look like Java. Neither does Ruby or Perl. Still programmers have no trouble picking up those languages. Before the C-style languages became dominant, programmers wrote Pascal, COBOL and Algol whose syntax is far from what we expect.

I expect far more people have trouble with the unfamiliarity of the semantics. If you come from an imperative setting, you need time to wrap your head around functional programming styles and idioms. Yet, unfamiliar semantics should pose no problem either: Python, Javascript, PHP and Java all execute very differently if you look at them modulo imperative execution.

2. While the core language itself is small and easy to learn, the libraries within the language are inconsistent, incomplete and poorly documented.


The Erlang/OTP libraries suffer from inconsistency having been built over time whenever there was a need for a new function. This is indeed unfortunate, but note this: apart from a few libraries which implement their functionality directly in the Erlang VM kernel, most libraries are written in pure Erlang and can be replaced easily. If you hate the list module, you can write your own lst.

Some libraries are de-facto deprecated as it is known they have certain limitations. The way Erlang tend to work however is that older modules known to have trouble are removed fairly slowly from the language - there may be a user out there, however poor that module is.

I cannot agree with the bad documentation. Erlang has excellent documentation. There a man-pages for every module, accessible with erl -man lists for instance (provided your distribution of Erlang is correctly built) and we have several online places where you can look up function definitions. In addition many modules have users guides which you can use to get started quickly. There even is an efficiency guide so you know how to write efficient code and there is a set of programming conventions with good advice on how to structure your programs.

Usually the documentation of functions are rather good I think. If you find something which you think is poorly documented, I’d advice you to make a patch against the documentation and discuss the change with others. Improving documentation is more important than ranting on its inadequacy.

3. Only a few people have written production level codes and you rarely get to hear from them.


I have written production level code in Erlang, namely a BitTorrent client good enough for everyday use. Our performance is currently as good as most other BitTorrent clients, CPU and memory-wise, despite we are writing the client in a “slow” language. I have also written programs professionally in Erlang - for the Web, but I am getting ahead of myself.

I try to write about my experiences - in part to tell stories, in part to educate and encourage others to pick up the language. No programming language is a silver-bullet. But when your task is to write highly concurrent programs Erlang shines. And if you look at the usual protocols for distributed computing on the web, http-servers, xmpp-servers, ad-serving frameworks, payment processing systems, BitTorrent-clients, video-servers, and so on — you find that many of them are highly concurrent.

Successful systems that just work will not catch headlines. A system that is shock-full of errors will. Many Erlang programs are running in companies with no open-source policy as well - don’t expect the programmers of those systems to even be able to talk about what they do. It is a competitive advantage to keep the mouth shut.

4. I can’t imagine how you can organize large code-bases in Erlang or even work as team, and this doesn’t feel right to any OO programmer.


Large Erlang programs are structured around the concepts of applications and releases. You write several isolated functional units, applications. Then you bundle these into a release which you can ship to the production environment. A typical application will provide an abstraction for something, be it running an HTTP server, talking to a database, controlling an external CPU-centered numerical program and so on.

The organization of large programs hinges on API-design. You want to design your program such that each application has a small API used by the other parts of the program. There may thousands of code lines behind the wall of the API, yet the interface to the rest of the world is small.

The trick of OO-languages is “abstraction is had by introducing another object”. If you take a look at the OO-design patterns, you will find that often a new object is introduced to mediate and solve an abstraction problem. This because the only way to abstract in those languages are to construct new objects (or classes, depending on lanuage).

In Erlang, the mantra is “abstraction is had by introducing another process”. In other words, you can usually solve abstraction-problems by introducing a middle-man mediating process, store state in a separate process, split a computation between several processes and so on. The OO-property of isolation, much important to structuring large programs, is present in the isolation of processes: you can’t go rummaging around inside the memory heap space of another process, you must ask it gently and nicely.
Naturally, this model has a design-pattern language as well and I know of several such. Remember this: “In OO-languages state is kept in objects; in Erlang, state is kept in processes”.

5. Most of the performance matrices are one-sided, and are performed by people who have an interest in Erlang. I would love to see some independent analysis.


In general, you should be wary of performance measurements where one does not fully understand the platforms they are working with. It is hard to make a program perform better but it is extremely easy to make a program perform worse. To improve a program you must understand the rules of the game you are playing. The rules used to speed up, e.g. node.js are much different from the rules used in Erlang. And that is different again from Ocaml, Scala, Java, Python, …

Also, workarounds for problems tend to be vastly different. A recent blog post of mine lays bare a curious property of node.js but the seasoned Node programmer understands how to work around it. He or she may deploy the multi-node for instance and it fixes a lot of the problems by using a single accept() queue for several workers. This is a nice way to break the rules of the game to avoid a problem.

Rather than thinking in terms of performance, I would argue you should think in terms of what your problem is. Erlang shines when a fully-preemptive, heavily concurrent process model is a good solution to your problem. It is powerful in that respect and it has the advantage it is a very mature system.

6. Its support for web-development is very primitive. With web frameworks like rails and grails, there is a lot of serious work for Erlang if it ever intends to go to that market.


I don’t think this is true. Web frameworks like Rails or Grails only talk about half of the web world. Clients in modern systems tend to be Javascript-heavy and only go to the server for their RPC Ajax-like requests. In this world, you need a lot less tooling at the side of the server. There are many web framworks popping up for Erlang currently, but let me plug the nitrogen project.

Yet, I think we will see much less need for web-frameworks as they were. We will need a new kind of framework which is much easier to work with server-side. And I think node.js shows the server doesn’t need a lot of stuff to be effective.

You should also think about the emerging alternatives to RDBMs data storage. There are systems such as couchdb and riak, which can cleverly bypass some of the usual Model-View-Controller pattern. I think we are in for a change in the way we do web development for the better and that Grails and Rails are a thing of the past if they don’t adapt to the new world (I am sure Rails will - but have to little experience with Grails to know if they stagnate or not).

7. Did I talk about Strings in Erlang? IO speed?


This single item is a blog post worthy in itself. First the strings.

A string in most languages is a sequence of bytes, pointed to by reference. In some languages the string is the central data you pass around and in some, it is the only kind of data you could pass around. Haskell and Erlang most notably defines a string to be a list of characters and integers respectively. There is much good and bad to be said about this choice - but it hurts people who don’t understand the difference.

Most web systems manipulate strings. The string is the ubiquitous data format: it stores integers, it stores floats, it stores XML documents, JSON, and such. The string is easy to transfer over a TCP/IP stream. It is no surprise that many languages center around string manipulation and are very effective at it. Perl is perhaps the ultimate string processor (apart from crm114, naturally).

The ubiquity of strings are also their Achilles-heel. The type information they carry is weaknonexistent to be precise. To manipulate a string in any statically typed language, Java, Ocaml, Haskell, etc., you need to transform it into an internal format. You process the string to an Object-hierarchy or an algebraic data type — and then you throw the string away! The new representation has all the advantages: it is typed, it can carry additional information in object state, and it can make illegal states unrepresentable.

You should never ever work directly with strings if performance matter. Even simple things like string comparisons may be fairly expensive (if the pointer comparison says different), whereas an atom comparison is not. The world of programming is more complex than just shoving every piece of data into a string.

Another weakness of the string is that the representation only answers to query by regular expression, recursive descent or LALR(1)-parsing. Some languages are very good at the former, regex query, but Erlang is not one of them since regular expressions are not built into the syntax and semantics.

So the first virtue of the Erlang programmer: Convert a string as fast as possible into an erlang-term() and then manipulate the term. Only work with crappy weakly-typed strings at the border of your application. An Erlang application should not constrain itself to work with only a single data type, namely strings!

The second virtue follow fast: If your string is large, use a binary() for effective storage and sharing. The binary representation, like the ByteString in Haskell, is as space efficient as C and it can be pattern matched if needed.

The third virtue is: Know thine iolists. When you construct strings in Erlang, you are not to build a sequence of characters! You should be building a tree of small string-like fragments, binaries, other trees, lists and so on. The output functions know how to effectively walk the tree and stream it to the output device.

The IO performance of Erlang is pretty good. I easily had some early tests in Etorrent moving 700 megabit on a single 1.2Ghz Pentium M CPU. Without any optimization at all.

Yet, it is important to notice that IO in Erlang is abstracted by default and this makes it a tad slower than what it can to be. The abstraction is rather nice and has to do with distribution. You can access a socket or file on another machine as if it is locally accessed. But this neat abstraction naturally has an overhead. Of course it is easy to build a primitive which throws away that abstraction if needed. And that will definitely run as fast as any other language.
9

View comments

  1. Nice post. It is particularly odd to hear that you never get to hear about people who've written production-level code, or that you wouldn't be able to organize large code bases in Erlang. Usually, people praise the Erlang community because there are so many helpful people who have real experience writing commercial-quality code. And it's been a recurring criticism against OTP that it is more geared towards managing large code bases than catering to small startups.

    ReplyDelete
  2. On hype:

    I personally don't care whether a language should get more hype, less hype, or hype with extra pudding.

    On syntax:

    First of all, I like the process syntax, but hate everything else.

    I love Haskell/ML syntax; prolog-style syntax is similar but far more kludgy. Three different line endings make refactoring a chore. And I hate holding Erlang's hands by specifying arity. Let's just assume that if I want to export a function, I want to export every version of it.

    erl's quit function, q/0, is a well-kept secret. Nowhere does erl inform the user how to leave the interpreter. I tried Control+C, exit/0, quit/0, bye/0, and f*&^%off/0, to no avail. This is something a user should never have to Google.

    On stdlib:

    I hate the standard libraries. Many functions behave in unexpected ways. io_lib:format creates a deep list of strings that have to be flattened. re:replace sometimes returns a string, othertimes it returns a binary. The documentation mentions the return type (in passing).

    The documentation uses frames, which are awful. Hey, Ruby's docs also suck for this reason. Erlang's documentation format is so bad that there's a website, erldocs.com to counteract it.

    On large code bases:

    Again, refactoring is a bitch. The forced module/filename consistency is reasonable, though I should mention that Erlang v5.8 has a bug: module names can start with a number for escript but not erl.

    Programming in Haskell is like talking to a mathematician.

    Programming in Erlang is like talking to a drunk.

    ReplyDelete
  3. On Prolog Syntax -

    I've worked with prolog, successfully, but I am not one of those people who think the prolog syntax is especially useful. For some problems, prolog syntax is ideal -- and for other problems, it's an obstacle to overcome. This is part of the reason why I've bounced off of erlang -- I like reading about the capabilities of the language, but the prolog-like syntax keeps me away. Been there, done that, didn't find that world-view useful.

    On Syntax and Semantics -

    You hit that one just about right. The only problem is that you seem to dismiss syntax as something relatively unimportant... which misses the fact that it's syntax, not semantics, that helps the programmer to find a reasonable solution. Languages with too little syntax tend to encourage journeymen programmers to develop clear and obvious solutions that are pathologically bad.

    Simple syntax is not always better. Consider your favorite assembly language. The syntax is
    universally simple: "opcode [argument [,argument] [';' comment]" -- and universally rejected for higher-level languages. Syntax *helps* us humans to express ourselves. Rules aren't inherently evil.

    To be fair, languages with too much syntax tend to force pathologically opaque solutions upon those same programmers. Language design is *hard*, after all. Too much is bad, too little is bad, and sometimes the ranges of "too much" and "too little" overlap.

    On Abstraction -

    Abstraction is the key tool we use, as programmers, to solve problems. We decompose big problems into little problems by abstracting out parts of the solution recursively. Many, if not most, programmers do this poorly: they mix levels of abstraction in their code, they exploit knowledge of the implementation of an API, they expose data structures to code outside the module...

    Just as it is simplistic to assert that in an OO language, abstraction is handled by introducing a new class or object, I suspect it's also simplistic to say that in Erlang, abstraction is handled solely by introducing a new process. Large program organization also involves abstracting the abstractions, organizing them in some way, to reduce or mitigate the problems indicated above.

    Languages that do not support organizing abstractions aren't an indication that this does not need to be done, only that the solution is a matter of discipline on the programming team. Languages that do provide support (modularization, packaging, namespaces, etc.) still require discipline, but it becomes easier to identify breaches.

    On Performance Comparisons -

    This is always a tricky one. Someone who is hostile (or, perhaps, merely ignorant) of a language will not code the optimal solution; someone who is an advocate for a language will not code an optimal solution for *other* languages. Any technology that requires that you embrace it fully before you 'truly understand' should be treated as snake-oil, even if it is true. This applies doubly to languages.

    There are really only two solutions, neither entirely satisfactory -- engage disinterested third parties, who will not fully utilize the subtle advantages of the language (as they are disinterested, they will never be acknowledged as 'truly understanding' the language by the advocates); or to sample random code across the programming population using the language, which means that the work of mediocre programmers will be taken as 'typical'.

    Either way, it's a mess, and it's perhaps best to take all performance comparisons with a grain of salt, and to not take such things too seriously.

    ReplyDelete
  4. It is worth noting, that the article making the rounds criticizing erlang is from 2008. Things have changed a fair bit since then.

    I also think the erlang syntax is just fine. At first I rebelled, but I bought an erlang book, spent a month reading it and playing with the examples, and at the end, erlang became my favorite language.

    It really isn't that hard. In fact, it is easier for me to deal with than ruby.

    You just have to recognize that erlang offers features that seem to not exist in any other language, or not as well, and thus it is well worth spending a little time learning the syntax.

    Then again-- don't. Let erlang be the secret weapon for those of us who did!

    ReplyDelete
  5. Nice article and I'd tend to agree with most of the points you make.


    On the point of strings: of course one can and absolutely should avoid using them for internal data representation.

    How about situations where you don't have a choice however? When you need to write a program that needs to process a lot of string/text data e.g. a log file processor/utility?

    ReplyDelete
  6. Good post jlouis. I find that the Erlang mailing list is full of people who write/have written "real" code.

    ReplyDelete
  7. Hi JLouis, I liked a lot your post. Can i translate to portuguese and publish on my blog, with an link to your and credits? Is a little bit hard to find some useful articles about Erlang in portuguese, and your post is fantastic. My email is marcelo@mindaslab.com, if you agree please let me know.

    ReplyDelete
  8. Its probably worth emphasizing that when people say stuff like The Syntax Sux0rs, they really mean The Syntax is not at all like Java/C/Ruby/Python/....
    For good or for bad, most developers out there fall into the Java/Javascript/Ruby/PHP camp. On top of that, most people out there are actually doing "GUI" stuff (cue some comment about "HTML Programmers"), and this tends to be the one area in which erlang falls short.
    Mind you, that isn't necessarily a bad thing. If you're building something concurrent/distributed/cloud-y, you're probably writing erlang on the back, and Ruby/PHP/JS on the front.
    My point, however, is that most of those GUI types are exactly the ones that'll sit around and complain about Syntax, and how stuff just doesnt look like (or work like) Java/JS...

    ReplyDelete
  9. Nice article. Just to add th about syntax: some people have problem with it, so alternatives (reia, elixir, efene) were developped. I think it should be taken into account. One nice way to solve the problem is in vala/genie language: the compiler accepts 2 different syntaxes (vala has C-like syntax, genie has python-like syntax). Why not improve erlc to accept another syntax (punctuation, records,...) based on the same semantic ?

    ReplyDelete
Blog Archive
About Me
About Me
What this is about
What this is about
I am jlouis. Pro Erlang programmer. I hack Agda, Coq, Twelf, Erlang, Haskell, and (Oca/S)ML. I sometimes write blog posts. I enjoy beer and whisky. I have a rather kinky mind. I also frag people in Quake.
Loading