Structuring helper functions

时间:2015-06-25 10:04:19

标签: javascript

I'm creating an abstraction above a couple very similar, but different interfaces and can't decide on the best way to structure my code to keep is as DRY as possible. So I'm trying to refactor out all the similar code into their own functions, but aren't sure where to place these functions.

Current setup:

var module = function module( API ) {
    var properties,
        doSomething = function doSomething( config ) {
            if (A) {
                foo();
            }
            else if (B) {
                // lotsa bar1 code
            }
            else if (C) {
                // lotsa bar2 code
            }
            else {
                error();
            }
        };
    return {
        // public interface
    };
};

I would like to fold the bar1 and bar2 code into its own function, but can't decide where to place this function. I don't really want to cause overhead by declaring the helper inside the function it's helping, although it's the most readable option.

Option A: Make the helper function another direct method of the module. This seems to be the easiest approach, but I don't like 'polluting' my main module namespace with functions "that don't represent a method of the module." edit: "that don't represent an action of the module."

var module = function module( API ) {
    var properties,
        bar = function bar() {
            // help doSomething
        },
        doSomething = function doSomething( config ) {
            if (A) {
                foo();
            }
            else if (B) {
                bar(B);
            }
            else if (C) {
                bar(C);
            }
            else {
                error();
            }
        };
    return {
        // public interface
    };
};

Option B: Create a seperate namespace for all the helpers. This is how I usually structure these kinds of extra functions, but the more I use this version, the less satisfying it seems.

var module = function module( API ) {
    var properties,
        helpers = {
           'bar' : function bar( input ) {
                // help doSomething
            }
        },
        doSomething = function doSomething( config ) {
            if (A) {
                foo();
            }
            else if (B) {
                helpers.bar(B);
            }
            else if (C) {
                helpers.bar(C);
            }
            else {
                error();
            }
        };
    return {
        // public interface
    };
};

Option C: Set the helper function as a method of the main function.

var module = function module( API ) {
    var properties,
        doSomething = function doSomething( config ) {
            if (A) {
                foo();
            }
            else if (B) {
                doSomething.bar(B);
            }
            else if (C) {
                doSomething.bar(C);
            }
            else {
                error();
            }
        };
    doSomething.bar = function( input ) {
         // help doSomething
    };
    return {
        // public interface
    };
};

And option D would be to create a closure around the function.

Maybe I'm putting way too much thought in this and it's all a matter of preference.

0 个答案:

没有答案