2

I wrote a node.js module which have a main function and two helper functions, I exported only main function and my question is: is it okay to keep two helpers like a global functions or I can find a better way?

function myFirstHelper(args) {
    // ...
    return result;
}

function mySecondHelper(args) {
    // ...
    return result;
}

module.exports = function main(args) {
    // ...
    return result;
};
rel1x
  • 2,351
  • 4
  • 34
  • 62
  • 1
    Those helpers are only global in the sense that they are global within the scope of the module. They don't leak outside of the module. If you wanted to make them global you could use `global.myFirstHelper = myFirstHelper`, [but you should probably avoid this](http://stackoverflow.com/questions/5447771/node-js-global-variables). – Andy Dec 09 '15 at 02:05

2 Answers2

1

I think you don't need to worry about the scope for other modules. However I understand what you feel. If you want to keep clean your feeling, you could use the Self invoking function like below. You know, you could use this pattern in all javascript environment even outside of nodejs.

(function(){

    function myFirstHelper(args) {
        // ...
        return result;
    }

    function mySecondHelper(args) {
        // ...
        return result;
    }

    exports.main = function(args) {
        // ...
        return result;
    };

})();
yazaki
  • 1,724
  • 1
  • 13
  • 17
0

You could use nested functions:

module.exports = function main(args) {
    function myFirstHelper() {
        // ...
        return result;
    }

    function mySecondHelper() {
        // ...
        return result;
    }
    // ...
    return result;
};

... and there would be no need to pass args since it would be accessible to those nested functions. Yet there's no reason to worry about this unless your module is very large.

The visibility/scope of something only tends to be problematic if the visibility/scope is much wider than its applicability.

For example, a variable declared at the top of a massive function might be a tripping point and a source of confusion and maintenance issues if it is declared/defined at the very top but only used by 3 lines of code towards the bottom. In that case, the visibility of the variable is significantly wider than its applicability, and therefore the reader must kind of keep it in account when tracing through the function for a much longer period of time than it is actually used, increasing the intellectual overhead. Of course, massive functions also tend to be a smell, but that's a different matter.

So here you have functions which are visible within the module. If the module is small and consists mostly of code using these functions, then the visibility/scope of these functions is not significantly wider than their applicability/usage within the module.

That said, nested functions are handy even for pure organizational purposes if you haven't encountered them before, and precisely because of how they allow you to avoid polluting an outer scope. So they might be a handy alternative to consider in this kind of case. But don't worry too much about the scope of a function (or anything) which is already pretty narrow.