The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Closures vs Callbacks

I'm a little foggy on why you would use a closure instead of a callback. Can anyone provide a concise reason or example of why you would favor one over the other? Choose whatever language you want. Thanks.
Curious
Wednesday, March 01, 2006
 
 
Two that I can think of:
* closures are simpler
* access to variables in the closure scope

Callbacks are generally more clear to me as closures just seem to sort of happen and I am never sure what I can access or can't access.

I prefer a simple well documented abstract class. This is more code but has the benefit of being obvious by looking at the code.
son of parnas
Wednesday, March 01, 2006
 
 
Closure is great at simplifying code. I use it to capture the environment so I can declare some variables that will be accessed from many closures/blocks. I use it to create code that seems more functional than object oriented, though my publishing some closures you can hide the implementation details from other files, which means easier refactoring and easier maintenance of the hidden code. So far, my frameworks work greatly this way, and they include GUI, Web, Reporting, etc.

Front-end programming should use closures and short files whenever possible. By short file I mean file with less than 1000 lines of code.

Basic Object Orientation, in my opinion, can produce some spaghetti, but by using short files with closures for the front-end programming, you avoid such problems much more.

My environment is Ruby. YMMV. I think I have found some kind of nirvana in this environment. My problems have nothing to do with coding anymore, they have more to do with organizing things and thinking what to do next. For example, I still need to create some kind of user manuals, add support for more features of databases in my database administrator, like renaming columns, adding columns, changing column type, copying data between columns...

And I'm currently trying to finish my own Wiki system which will allow me to create web sites, documentation, test my web framework, etc.

Nowadays I consider closures one of the most important features for my front-end programming.
Lostacular
Wednesday, March 01, 2006
 
 
P.S. With closures it's possible to substitute methods. Maybe I should have mentioned that. Closures are local to a code execution. Methods can be global, just like classes can be global.
Lostacular
Wednesday, March 01, 2006
 
 
Am I missing something or difference is "you can use closure as callback, but you can't do otherwise"?

http://en.wikipedia.org/wiki/Closure_(computer_science)

http://en.wikipedia.org/wiki/Callback_%28computer_science%29

In most cases your callback function will have to have "context" parameter anyway. I.e. a lot of Windows functions that require callback will pass some DWORD value through to let programer to pass context information. In STL you often need to store your context inside funtion object. So you can say that closures are ideal callbacks as context is captured by definition...
WildTiger Send private email
Wednesday, March 01, 2006
 
 
I'm embarrassed to admiit it, but I had never heard the term "Closures" before this thread.  I had to look it up, and I can tell you that I'm not really comfortable with it. I saw the examples on wikipedia, and I can say that I have *SEEN* something like that once and didn't understand it.  Seems like some really strange stuff to me.  It amazes me how much stuff there is to learn out there.
newby
Wednesday, March 01, 2006
 
 
closures. that brings back unhappy memories of trying to learn scheme. that was painful.
starving coder
Wednesday, March 01, 2006
 
 
It's clear(er) now. I've been somewhat aprehensive like SonOfParnas in that I knew of them, but was never quite comfortable with the functionality which made it easier to say "I don't trust it, I'll put it on the back burner and learn more later." Well, it's later.

The links to wikipedia were quite helpful, especially the implementation portion. My distrust stemmed from the fact that I couldn't see how the the external variables were tracked in the lexical scope of the enclosure.
Curious
Thursday, March 02, 2006
 
 
"So you can say that closures are ideal callbacks as context is captured by definition..."

One would generally use it when the desired result is for the nth execution is influence the n+1 execution. I would say closures are ideal in the sense where context is necessary and you understand the circumstances that may make it harder to debug. It seems like it would be useful to add this information to whatever log file is tracking execution.
Curious
Thursday, March 02, 2006
 
 
I have several source-files where you won't find declarations of methods or classes -- all they use is closures/blocks. It's like the file is its own namespace. :-) The tendency is for files without methods and classes to grow much faster in number than the ones with methods and classes. I predict that in the future I might have 9 files with only closures/blocks to every 1 file with classes/methods. And the reason for that is that front-end programming demands more files. Whenever I need polymorphism, I use classes/methods, though.
Lostacular
Thursday, March 02, 2006
 
 
Ok, my perspective wasn't from the front end GUI. Most of my experience has been under the covers. I've done very little work in the presentation layer, but I can see how it would be useful in dealing with the meanderings of end users and encapsulating logic in script languages.
Curious
Thursday, March 02, 2006
 
 
I think that's part of what some folks like in Lisp and other similar languages. You got it.
Lostacular
Thursday, March 02, 2006
 
 
Closure is more than a callback - in fact it is like an object:
"An object is data that knows which code acts on it.
A closure is code that knows which data to act on."
from: http://perlmonks.org/?node_id=269271
Zbigniew Lukasiak
Friday, March 03, 2006
 
 
Another advantage is that closures are generally defined where they're used - always within the same scope, and often literally in place in the form of an anonymous function or code block.  This improves code locality.  (Compare C++, where the implementation, interface, and point of use of a function object are likely to be spread over several files.)
Iago
Monday, March 06, 2006
 
 
Nicely put, Iago.
Lostacular
Monday, March 06, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz