0

I have two similiar functions that both require the same parameter checks

function doThis(foo, bar) {
    if (foo.length === 0) foo = 'foo';
    if (bar.length === 0) bar = 'bar';

    foo = encodeURI(foo);
    bar = encodeURI(bar);

    // ... some other checks and enforcements...

    // Output as alert
    alert(foo + bar);
}

function doThat(foo, bar) {
    if (foo.length === 0) foo = 'foo';
    if (bar.length === 0) bar = 'bar';

    foo = encodeURI(foo);
    bar = encodeURI(bar);

    // ... some other checks and enforcements...

    // Output on console
    console.log(foo + bar);
}

What is a proper way to dry this up? I came up with this:

function paramsCheck(foo, bar) {
    if (foo.length === 0) foo = 'foo';
    if (bar.length === 0) bar = 'bar';

    foo = encodeURI(foo);
    bar = encodeURI(bar);

    // maybe some other checks and enforcements...

    return { foo: foo, bar: bar };
}

function doThis(foo, bar) {
    var params = paramsCheck(foo, bar);

    // Output
    alert(params.foo + params.bar);
}

function doThat(foo, bar) {
    var params = paramsCheck(foo, bar);

    // Output
    console.log(params.foo + params.bar);
}

But I'm not really happy with it. I would rather like pass the parameters as references to paramsCheck(), so I can directly modify them instead of returning a new object.

5
  • No, this is the correct and most readable way IMO. Commented Jun 27, 2013 at 8:24
  • 1
    This way is fine and there is nothing better (with the possible exception of merging all parameters in one object, in which case you can pass it to a function for massaging). Commented Jun 27, 2013 at 8:28
  • I would only suggest to extract setting default value to an util function. Gruss aus Winterthur. :) Commented Jun 27, 2013 at 8:33
  • @Jon That's what I've done in the end. Thanks! Commented Jun 27, 2013 at 9:24
  • @Thomas Zuberbühler: Einen festlichen Gruss zurück ;-) (Bin gerade in Zürich) Commented Jun 27, 2013 at 9:24

2 Answers 2

2

That could work, although with what you've given I would suggest something like this:

function paramsCheck(params) {
    var l = params.length, i;
    for( i in params) if( params.hasOwnProperty(i)) {
        params[i] = encodeURI(params[i] === 0 ? i : params[i]);
    }
}
function doThis(foo,bar) {
    var params = {foo:foo,bar:bar};
    paramsCheck(params);
    // now do stuff
}

This solution is more flexible because it doesn't make assumptions about the input, so you can very easily add a third parameter.

Sign up to request clarification or add additional context in comments.

2 Comments

what about the default value?
Well, that's what the ` ? i` part is for - the keys are the default values in the examples, so I rolled with it.
1

You can do

function doThis(foo, bar) {
    foo = clean(foo, 'foo');
    bar = clean(bar, 'bar');

    // ... some other checks and enforcements...

    // Output as alert
    alert(foo + bar);
}

function clean(value, defaultValue){
    if (value.length === 0) value = defaultValue;
    return encodeURI(value);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.