16 ms·
You have to look at the candidate's overall performance in the interview; not the answer to a single question. Describing a closure is actually a great example
by bigtunacan 13y ago
You have to look at the candidate's overall performance in the interview; not the answer to a single question.
Describing a closure is actually a great example of something many great software developers WON'T know based upon their prior domain experience.
A strong Java or old school C/C++ programmer (yes I know C++11 has closures) may actually be an excellent candidate, but have no idea what a closure is.
I would expect that a front end JavaScript developer, RoR dev, or anyone else "experienced" in a language with some functional support (Scala, Haskell, F#, etc...) should be able to explain a closure.
- jimmaswell 13y agoI can get anything I need to done in JS and I have no idea what a closure is. I've even written an emulator in it. Took me a little while reading to figure out what it is. Apparently it's just using vatiables in a lambda that are from a scope higher than it. I do that all the time, I just didn't know it had a special name. To be fair, I didn't know JS could do lambdas. I've only used them in C# and possibly Python. Well, I guess it's like a closure when you use functions as methods in objects in JS, since they can be defined with this.MyMethod = function(){...} which can then access the object's stuff.
- tonyarkles 13y agoYou've never passed a callback to a JS function?
- elbear 13y agoThat's an anonymous function not a closure. A closure references non-local environments and can "access those non-local variables even when invoked outside of its immediate lexical scope" as Wikipedia puts.
- bjterry 13y agoI don't understand your contradiction to the parent poster. All JavaScript functions are closures, including anonymous functions that don't reference any lexically-scoped variables, and even named functions at the top level, which close over the global environment[1]. Also, callback functions don't have to be anonymous functions, you can just as easily pass named functions. 1: http://javascriptweblog.wordpress.com/2010/10/25/understanding-javascript-closures/ http://javascriptweblog.wordpress.com/2010/10/25/understandi...
- elbear 13y agoI know that confusing anonymous functions with closures is a common misconception. I also know that callback functions don't need to be anonymous, but a lot of the times they are, at least in the JavaScript code that I've seen. Thank you for the clarification that all JavaScript functions are closures though.
- mattmanser 13y agoC# people tend to use lambda to mean lambdas, closures and sometimes anonymous functions. Javascript people use closure to mean lambdas, closures and sometimes anonymous functions. Ask two programmers the difference between lambdas, closures and anonymous functions and be prepared for a different and probably wrong explanation from most of them. Including me. And that's the only reason you don't know what a closure is, if you hung out around javascript programmers a bit more in RL or online you'd have heard it constantly. If it was described to you, you'd know what it is.
- enjo 13y agoWhich is why when I want to know that you actually understand something as basic as closures in Javascript, I don't ask for the definition. I ask you to demonstrate it.
- s73v3r 13y agoBut if you don't know what it's called, or what the term means, then you're not going to be able to do that either.
- Peaker 13y agoI don't think everyone agrees, but I think usually, a closure refers to the implementation of that concept, and not the concept itself. The implementation being a pair of the static code alongside the captured environment. Also, I think most would agree that if the "higher scope" is the global scope, then it isn't actually a closure. For example, C functions can access globals, but C has no closures (barring various extensions).
- pjakma 13y agoYeah, it's language sugar to capture variables (including automatics) from an outer scope and make them available to an anonymous function. This may involve copying the variables to other storage (e.g. to heap) if the anonymous function will be run outside the context of its caller. Also, the language may, if it provides some sugar to allow the anonymous function to appear to return to the caller, e.g. through async/yield/co-routine type semantics, also transparently copy back the variables to the callers' locations. As per another commentator, 'lambdas', 'closures' and 'anonymous functions' are used equivalently by different people and/or fields. As a C programmer primarily, I was long confused why higher-level programmers went on about "closures". They're nothing special, we do this kind of stuff in C all the time. We have to do it by hand ourselves, the language doesn't give us shortcuts. It's not really mysterious and we don't have fancy names for it. Sometimes I wonder if the reason some higher-level programmers treat these things so reverentially is, perhaps, because there is an element of mysticism to it, borne of lack of understanding of what is happening "under the hood"?