HomeBlogThe buzz of closures

The buzz of closures

I found an interesting discussion about closures. I find this quote particularly true (and amusing):

There has been a great deal of interest in closures lately, driven in great part by the fact that there is talk of adding some form of anonymous functions to the Java. Most of the time, people talk about “adding closures” to Java, and that prompts a flurry of questions of the form “what is a closure and why should I care?”

Yes, why would they care? As someone put it, if you always programmed in Basic, you think you don't need recursion. You can't comprehend a complex language feature—such as recursion—unless you actually try to make some sense of it. Yes, no matter how many computer science courses you'd take, you can't do real programming unless, well, you do real programming. In other words, your teacher might have told you what recursion means, but unless you actually use it, you'll never know what is it good for.

Consequently, I think there are 2 types of programmers.

  1. Some would spend a lot of their free time trying to understand and make use of some techniques that might—or might not, but usually will—serve them for real-world problems.

  2. The other camp is very boring: those are the people that are payed for some job and only do what their boss asks.

Now, the boss will never ask you to use closures (or any other language feature for that matter). He'll just tell you “do this” ASAP, and you have to figure out how to finish it as soon as possible. Well, using closures, ASAP can mean a lot sooner than without them, no matter what language are you programming in (assuming of course that it supports this 30-years-old concept; some new and much hyped languages still don't.).

I have former colleagues that have completed their B.S. in computer science (which I still didn't). One of the courses you do in the college is an AI course which, of course, uses Lisp as the supporting language. Well, the horror is that none of them understands what's a closure, and asks me every time I mention the word “OK, Java/C# might not have that, but why would I need it anyway?”.

So after preaching functional programming for some time, and after I saw my fellows here ignore me, I've come to the conclusion that it's better for them not to understand it. A boss is a boss, and he'll favor someone who has a B.S. (which I don't). So I at least want to keep the technical advantage. :-p

So, ya Java folks, don't even think about closures. You don't need them. Just focus on writing specs and UML-s and Factories and Managers and Listeners and Executors.


  • By: Keith GaughanFeb 08 (11:48) 2007RE: The buzz of closures §

    ...which pretty much explains why so many programmers treat OO languages a procedural ones with shiny bits attached.

    But to quote a countryman of mine:

    "Progress is impossible without change, and those who cannot change their minds cannot change anything."

    "The reasonable man adapts himself to the world; the unreasonable man persists in trying to adapt the world to himself. Therefore all progress depends on the unreasonable man."

    If it wasn't for the unreasonable man, we'd still be bashing our heads off the wall with assembly language.

    Nice snipe at the end though. :-)

    • By: mishooFeb 08 (13:33) 2007RE[2]: The buzz of closures §

      Well, things are starting to change with all the hype around AJAX.  JavaScript is a functional language and sooner or later people will adapt.

      Not sure that's good news—I'll probably have to finish my CS degree after all. :)

      • By: MasklinnFeb 12 (20:46) 2007RE[3]: The buzz of closures §

        > JavaScript is a functional language

        No it isn't, It's an OO object with first-class functions, anonymous functions and closures, but that doesn't make it a functional language, by far.

  • By: JustinAug 01 (05:05) 2007Maybe you should get a CS degree... §

    Java already has closures.  If you had a CS degree you'd know that.

    Maybe these conversations with colleagues end with them asking what they're missing because **Java already has the feature you're talking about**.  Come on, dude.  You should read more about issues if you're planning to publicly comment on them.


    interface I { int F(); }

    class C {
        void DoSomething() {
            final int N = 10;

            // Look how this object depends on N
            I i = new I() { public int F() { return N; } }

    Read the Wikipedia article on closure.  http://en.wikipedia.org/wiki/Closure_(comput…

    "In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables.  In some languages (**HINT: JAVA**), a closure may occur when a function is defined within another function, and the inner function refers to local variables of the outer function."

    Local variable: N. Outer function: DoSomething. Inner function: F. Presto! Closure.

    • By: Crest da ZoltralAug 01 (06:06) 2007RE: Maybe you should get a CS degree... §

      Now try to return F and you may see the problem of simple stack based memory allocation and how it restricts you.

      • By: David GreenburgAug 01 (06:29) 2007RE[2]: Maybe you should get a CS degree... §

        If you returned F, it wouldn't make sense anyway, b/c N would be out of scope!

        • By: PeteAug 01 (07:42) 2007RE[3]: Maybe you should get a CS degree... §

          The whole point of closures (in languages that support them properly, ie. not Java) is that you *could* throw around the function F and it'd take its N along with it.

          See the nice little (Javascript) example here: http://programming.reddit.com/info/2b192/com…

          • By: Tom PrimožičAug 01 (15:45) 2007RE[4]: Maybe you should get a CS degree... §

            No that is not the point of closures. The point of closures is that the functions can access some data that is neither global (global variables), or local (declared in this function / as a parameter to this function).

            What you are talking about are functions as first-class values, meaning that they can be passed to and returned from other functions.

            Java supports closures (allegedly, as the previous example shows), but not first-class functions - they are usually stimulated using some anonymous dummy objects with one function only.

            Inspite of using wrong terminology and mixing the concepts, there is some logic in what you are saying. It is absolutely correct that the true power of clusures only becomes available with first-class functions (thou they can be perfectly emulated by anonymous objects, with a lot of boilerplate of course) and, additionally, partial evaluation (meaning that you can build a new closure from an existing closure by providing only some parameters of the function).

            Anyhow, Java and the like are not best suited for the discussion of functional concepts. Try ML (SML or OCaml) or Haskell.

    • By: mishooAug 01 (10:42) 2007RE: Maybe you should get a CS degree... §

      Ignoring the fact that the Java syntax looks awful, last time I checked, you could *only* do this if your variables are "final" (which is the case in your sample too).  This means you can't change such a variable from the returned closure, which misses the whole point of closures.

      Do this in Java:

      function uppercaseBuffer() {
          var s = "";
          return function(val) {
              if (val)
                  s += val.toUpperCase();
              return s;

      var buf = uppercaseBuffer();
      buf("hello ");
      alert(buf()); // "HELLO WORLD"

      So in JS, a variable is automatically kept alive as long as we can access it.  The "s" defined in uppercaseBuffer() is alive as long as "buf" is in scope, because "buf" is a function that can access (closes) "s".

      I know you *can* do this in Java (you don't even need closures) but it's horribly complex and people are starting to see this ("A language extension providing fully featured closures for Java is under consideration for Java SE 7." in the Wikipedia page that you pointed out).  Good luck guys, but keep in mind that Scheme had this 30 years ago.

Page info
2007/02/02 00:08
2007/02/22 21:40
Mihai Bazon
lexical closures, programming, rants
See also