Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to name a function that produces a closure

I understand closures, even though I scarcely use them, but whenever I can squeeze one I have no idea of how to name it.

The best I can think of is sticking a "make" before what would be the name of the function:

function makeSortSelection(settings1, settings2) {
    return function() {
        /* sort stuff attending to settings1 and settings2 */
    };
}

$("#sort-button").click(makeSortSelection('ascending',foo));

(I almost always use them in Javascript, but I guess this is a very language-agnostic question)

Sadly, most examples I found of closures just use "foo" or "sayHello". I like to give all my functions a verb as name: functions "do stuff", and their name reflects it ("sortSelection", "populateForm"). In the same spirit, how should I name closures, that "do things that do stuff"? What conventions do you use, and what are the most common?

PD: I tend to use Google's style guide when in doubt, but it says nothing about this.

like image 618
jesusiniesta Avatar asked Jan 20 '14 11:01

jesusiniesta


People also ask

How does a function creates a closure?

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.

What creates a closure?

A closure is created every time an enclosing outer function is called. In other words, the inner function does not need to return for a closure to be created. The scope of a closure in JavaScript is lexical, meaning it's defined statically by its location within the source code. Closures have many practical use cases.

Is every function a closure?

Yes, exactly. As you've identified, every function in JavaScript is a closure over at least one context: The global context.


1 Answers

Closures aren't nameable entities. Functions are nameable but a closure isn't a function.

Rather than define "a closure" it is easier to define the circumstances under which closure arises.

A (lexical) closure (in javascript) occurs and continues to exist as long a persistent external reference to an inner function exists, but neither the outer function nor the inner function nor the reference to the inner function is, in itself, a closure. Pragmatically speaking, a closure is a construct comprising all these elements plus a feature of the language by which garbage collection is suppressed when they exist.

In my opinion it is wrong, as some claim, that "all functions are closures". Yes, all functions have a scope chain, but a closure should only be regarded as existing once an outer function has completed and returned, and a persistent reference to an inner function exists.

By all means give the returned function a verb-based name, just like any other named function - there's no need to regard it differently just because it is was returned by another function. In every respect the returned function is just a function - it just happens to be a function with access to the scope chain of the (execution context of the) outer function that brought it into being - nothing more than that.

EDIT:

I just realised I didn't address the critical point of the question - rephrased "how to name a function that exists for the express purpose of forming a closure by returning a function".

Like you, I have a problem here.

Most often I use the "make" prefix, as in your example. This seems to be best most of the time.

I have also used "_closure" as a suffix, This doesn't obey the "verb rule" but has the advantage of being independent of natural language. "Make" is strictly an English word and speakers of other languages will probably choose to use their own "faire", "machen" etc. On the other hand, "closure" is universal - it remains, as far as I'm aware, untranslated in other languages. Therefore, "closure" as a suffix (or prefix) could be better in scripts that are likely to be used/modded on a world-wide basis.

like image 173
Beetroot-Beetroot Avatar answered Sep 27 '22 21:09

Beetroot-Beetroot