9

Why is there so much buzz about closures among developers? In my career I never intentionally used them, though don't clearly understand what they are.

UPD: just to clarify. The question is about why the closure concept became so talky these days.

  • 3
    How would you be able to intentionally use something which you don't know what it is? So basically you should first be asking (or searching rather) what closures are. – Steven Jeuris Jul 06 '11 at 14:15
  • 1
    Did you read the wikipedia article? What did you fail to understand about it? – kevin cline Jul 06 '11 at 14:18
  • 2
    What is your career based on? –  Jul 06 '11 at 14:20
  • 4
    What kind of a question is this? What "buzz"? What don't you understand? Why didn't you ask *that* question instead? – Aaronaught Jul 06 '11 at 14:24
  • @Thorbjørn it was java swing in early years and it's common there to use such techniques, but I didn't call that closure I didn't even knew that word. So I'm curious. –  Jul 06 '11 at 14:38
  • @Aaronaught buzz in computer articles, everywhere. –  Jul 06 '11 at 14:44
  • 1
    Instead of upvoting my and Aaronaught's comment, people should be downvoting this question ... Why oh why [don't people downvote more](http://meta.stackexchange.com/q/97022/157047)? ... or worse, upvote this? :/ – Steven Jeuris Jul 06 '11 at 14:46
  • @gasan, Closures is the _concept_, Java anonymous functions is just _one_ partial implementation. But if you are familiar with those, you are almost there. –  Jul 06 '11 at 14:52
  • @Steven for me it seems that people are talking about an ordinary thing as if it will be a blessing from a God. So that's why I'm asking why they are so popular and why people are talking so much about them. –  Jul 06 '11 at 14:54
  • @gasan: "so much buzz", "people are talking". It helps to provide links or quotes to support this kind of claim. Who is talking? What volume leads to "buzz". This is an ancient programming concept. What magazines are you reading that have rediscovered this? Please provide some hint as to why this appears new to you. – S.Lott Jul 06 '11 at 15:20
  • @kevin, if the question is a duplicate then close it. –  Jul 06 '11 at 16:24
  • 1
    @Steve Jeuris: Reputation whores, nothing more nothing less. I am happy to down vote but prefer to address via comments and in hopes the OP can resolve it without a down vote. – Chris Jul 06 '11 at 16:31
  • 4
    In defense of the OP, closures are an old concept but only recently have they been the talk of the town, so to speak. And the concept is indeed very simple to anyone who's learned a bit more maths, I also can't understand why people talk about them as if they were something very difficult to comprehend. Maybe that's just a way to make themselves look smarter: "look, I grok closures which are so hard to grok, so I rock". – quant_dev Jul 06 '11 at 16:42
  • @TRA: You are right. I did. – kevin cline Jul 06 '11 at 17:01
  • @quant_dev yes that's exactly what I'm talking about. And people are trying to make a kind of a mysterious cult around it. –  Jul 06 '11 at 17:18
  • @S.Lott I just checked with google, amount of search results for a word `closure` for custom periods of time, here it is: 2004-2005: 11.9m; 2005-2006: 12m; 2006-2007: 19.7m; 2007-2008: 19.6m; 2008-2009: 21.9m; 2009-2010: 29.1m; 2010-2011: 105m. `m` stands for million. Quite self-descriptive numbers. Also worth to mention `Clojure` language appeared in 2007. –  Jul 06 '11 at 17:59
  • @S.Lott I've checked for a specified time intervals. So you can see statistics and a trend. If google doesn't illustrate (not directly but anyway) amount of mentions, then I don't know what does. It is to prove the "buzz" around closures. My question is, WHY is that? Is there some matter besides that or it's just a "buzz" that makes ones who mention that word look smarter. I by myself recently saw a question on an interview that was about closures (didn't see that kind before). So I'm interested, why is all this itch is happening? Maybe there is no answer to that question, then please close it –  Jul 06 '11 at 18:12
  • @gasan: Which level of google hits crossed into "so much buzz"? More than 21M? More than 29M? When did it go from "normal" to "so much buzz"? – S.Lott Jul 06 '11 at 18:58
  • Downvoting is now free people use! –  Jul 06 '11 at 19:27
  • @kevin, you need to use "@Thor" or something similar for me to see it. –  Jul 06 '11 at 19:34

3 Answers3

22

A closure is code that remembers the world where it came from while still being usable where it has been brought to.

An example is defining an anonymous function in Java which knows that it is inside YourObject and can manipulate its methods and functions. This function is then delivered to e.g. Swing where it goes deep inside e.g. a Listener but still has a lifeline back to its roots.

This is a very powerful concept as it allows you to deliver code which - unbeknownst to the code using it - can reach back into other parts of the code.

  • 1
    Yes. It's a way to inject not only procedure, but state, into other code. This allows for a very high degree of flexibility in designing algorithms; you can design templated methods which depend on stateful subroutines, which it may not even know about at compile-time. The downside is that the link to that method's original source, and thus state, can be broken without the consuming method knowing about it; when that happens you get hard-to-find runtime errors. – KeithS Jul 06 '11 at 16:45
  • 1
    Ha ha, anonymous function in Java... Funny. – configurator Jul 06 '11 at 19:20
  • 1
    @configurator, not funny, a fact. –  Jul 06 '11 at 19:33
  • http://stackoverflow.com/questions/2755445/how-can-i-write-an-anonymous-function-in-java Java 7 is not out yet, no? – BlueRaja - Danny Pflughoeft Jul 06 '11 at 20:19
  • java 7 does not have closures. –  Jul 06 '11 at 20:21
  • And since when does Java have anonymous functions exactly? – configurator Jul 06 '11 at 23:21
  • @configurator, instead of poking fun, feel free to fix mistakes by editing the question if needed. –  Aug 10 '11 at 12:34
  • I don't see a way to edit the question to make it better. The example would be better in my mind if it was in e.g. C# where you can define an anonymous delegate and it has _modifiable_ access to the outer scope. As it is, the example is still valid Java closures do something similar but as far as I know the closed-over variables need to be final which limits their usage quite a bit. And, they're not inside anonymous functions, but anonymous types with named functions (a very lacking feature in my mind). I never meant to offend and if I did I apologize. – configurator Aug 10 '11 at 19:33
  • @configurator, ah, I missed that you have too little reputation to edit, sorry. –  Aug 10 '11 at 19:45
  • "java 7 does not have closures": It does, they are called anonymous inner function. What Java 7 does not have are anonymous functions. – Giorgio Mar 09 '13 at 00:23
6

Closures are just something to solve a variety of problems in an elegant way. Actually, in programming languages where no closures exist, techniques are created to implement similar functions.

Just think of functors in C++ or Runnables in Java. They are just techniques that allow functionalities similar to closures. Some kind of « manual closures ».

Closures are getting more popular because they are integrated into popular languages : Javascript is growing due to online applications (like google doc for example), C# implemented it, PHP implemented it since 5.3, and so on.

Now that closures are available in more and more technologies, it becomes quite straightforward that more and more people are interested in them.

So now, what are closures ? This is quite simple. A closure is a function and a context to execute it within. This is manipulated as an object. Why is this useful ? This is useful to hook your own code into existing code.

Here are two common situations which require that : when actions are managed by another piece of code, like in multithreading with a thread pool, or when an action has to be executed on a choosen event (used often in javascript for the UI).

Steven Jeuris
  • 5,804
  • 1
  • 30
  • 52
deadalnix
  • 5,973
  • 2
  • 31
  • 27
  • Did I clearly understand you? There are some "manual closures" (not really a closures, but something that resembles their behavior) and there some "TRUE closures"? –  Jul 06 '11 at 14:35
  • Closure is a language capability. Some language doesn't have that capability, so you have do closure manually using tools that the language provides to you. Saying that they are TRUE or not isn't a very interesting debate IMHO. – deadalnix Jul 06 '11 at 14:41
  • 2
    @gasan, you can always whip out the assembly and manually perform whatever pattern you want with a bucketful of GOTO statements. Similarly you can manually perform a "for" loop with a "while" loop (in C++). The more advanced, higher levels offer a standardized way of doing common things. – Philip Jul 06 '11 at 15:05
6

I saw an amusing quote the other day, it was along the lines of "classes are data with functions. closures are functions with data".

Yes, it's an oversimplification, but it helps to get the point across.

geoffjentry
  • 870
  • 1
  • 6
  • 8