Erlang & Single Assignment: You’re Doing It Wrong

Posted: July 28th, 2008 | Author: kevin | Filed under: Erlang | View Comments

Patrick Logan’s latest missive highlighting the complaints some people have about Erlang’s single-assignment semantics reminds me of something I’ve been wanting to say for a while:

If you are chronically bumping into the limitations of single-assignment then you’re doing it wrong.

Off the top of my head, here are the two reasons I can think of why people don’t like single-assignment semantics (SAS for short):

  1. Looping over a collection of values – If you’re running afoul of SAS then you’re looping iteratively not functionally. You should investigate list comprehensions, recursive functions combined with pattern matching, or map, fold, zip, or zipwith in the lists module. If you really need plain old for-loop style iteration, lists:foreach/2 should do nicely but in a functional style.
  2. Reusing variables in the same function – Quit it. Really. Erlang is trying to tell you your function is either too long or doing too many things.

This isn’t to say that you’ll never bump into SAS. I run into it mostly when I’m writing code which makes heavy use of records and qlc although I daresay with a bit of thought and factoring I could remove those instances as well. Cobbler’s shoes and all that.

Rather than bring your favorite language’s paradigm to Erlang (Ruby, Python, Java, etc) try working with Erlang’s grain. The style of programming these techniques encourage, namely reducing side-effects and function complexity, are beneficial no matter what language you’re using.


  • Andrew
    Erlang is for wimps
  • "Reusing variables in the same function - Quit it. Really. Erlang is trying to tell you your function is either too long or doing too many things."

    Not only in Erlang, but also in Java:

    http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/

    "If you reassign a variable you most probably do something wrong. I’ll bet if you reassign a variable your method does at least two different things though every method should only do one thing."

    This post on my blog resulted in 12 down votes on Dzone ;-)

    Peace
    -stephan
  • A concise and definitive statement on the subject was needed. You delivered. Thank you Kevin.
  • You've nailed it in this sentence: "Erlang is trying to tell you your function is either too long or doing too many things." When I write code in Erlang I realy feel the physiological need to split up functions in order to make them shorter and ... well... nicer, so to speak
  • I've only been in the Erlang world for about a week but I'm amazed by how vitriolic this "debate" has become. Your lucid (and non-apologetic) explanation is highly welcome.
  • Awesome overview. I've modified one of my older posts on why Erlang is a functional language to reflect this idea. Up until now, I was just calling this "syntax" for lack of a better descriptor.
blog comments powered by Disqus