[erlang-questions] Reassigning variables

Matthew Dempsky matthew@REDACTED
Tue Mar 17 22:19:48 CET 2009


On Tue, Mar 17, 2009 at 1:38 PM, Justin Sheehy <justin@REDACTED> wrote:
> Single assignment is one of the biggest ways in which Erlang helps
> reduce bugs.  Please don't try to introduce ways around it, as they
> will make programs worse.

I disagree.  There are ways that reassignment can make code easier to
analyze, but I'm not suggesting people use reassignment in those
cases.

Similarly, Erlang doesn't support 'return', but our ad server has
enough business logic where the code would be much simpler if it did.
We had some code that was effectively:

    if requested ad size is bad:
        return {skip, bad_size}
    if the game has ads disabled:
        return {skip, disabled}
    if the game is filtering a domain:
        return {skip, domain_filtered}
    return choose_ad()

I rewrote this code in 'Erlang style' with a list of funs to check,
and to short-circuit on the first that returns a skip, when I last
added to it, and so incrementally it's much less work to add new
checks than the old code.

But it's stupid.  I'm not going to add 100 new checks, there's only a
handful now, and there will almost certainly never be more than a
handful more in the future.  Rewriting 10 lines of code to be 60 lines
of code (having to define a new function for each check, having to add
code to handle the list of check funs, etc.) to deal with Erlang's
language limitations does not help with readability.

(In contrast, we similarly redesigned the per-campaign and per-ad
filtering code to use this style, and they do update much more
frequently, and it actually has been beneficial allowing us to
dynamically change what lists of filters are applied in different
situations.  But not everything merits that complexity.)

Programmers understand how variable reassignment and multiple function
call returns work: every beginner programming language includes them.



More information about the erlang-questions mailing list