Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a standard way to name functions which returns another function?

I have a function that takes a string and a list, and returns a function that toggles the string in/out of the list. So if the function gets called twice, the string will not appear in the list, but calling it once, the string will be in the list.

Is there some standard convention (like callback/handler) for a function that generates another function? I'm working with JavaScript.

Thanks.

Code below:

function wrappedToggleGenerator(id, list) {
    return function (flag) {
        if (flag) {
            // Clear the list if flag is true
            list.splice(0, list.length);
        }
        if (list.indexOf(id) == -1) {
            list.push(id);
        }
        else if (list.indexOf(id) != -1) {
            list.splice(list.indexOf(id), 1);
        }
    }
}

EDIT: I have added my code. Thanks for the suggestions, I think I will prefix function generators with "wrapped".

like image 802
kudos Avatar asked Oct 06 '16 18:10

kudos


People also ask

How do you call a function that returns another function?

To call a function inside another function, define the inner function inside the outer function and invoke it. When using the function keyword, the function gets hoisted to the top of the scope and can be called from anywhere inside of the outer function.

What should be the name of function?

The name should make it clear what is the purpose of the function. Long names are okay. Make the names as long as needed, but no longer than that. For example, if you have a function createLocalDatabase and there is only one database, replace it with createDatabase.


1 Answers

coding conventions are defined by the project owners, based on some best practices. Its up to you to decide, which conventions you want to follow.

It may be beneficial to have some meaningful name to the method which also includes return value type, but you may as well have a good documentation in place.

In any case, you might want to read on the Hungarian notiation i fyou want to include indicators of intended use to method names. https://en.wikipedia.org/wiki/Hungarian_notation

like image 132
Vladimir M Avatar answered Oct 20 '22 16:10

Vladimir M