Ala Eddine JEBALI
Ala Eddine JEBALI

Reputation: 7841

Looking for a better way to call a function with too many parameters most of them are not necessary

For some reasons, colleagues write functions with too many parameters like this:

function toBeUsedByOthers(a, b, c, d, e, f, g, h, x, y, z, userName, userId, userInfo3, userInfo4){
    // ...
}

I know that when I call toBeUsedByOthers and passing it the userId 789 it does what I need (all others parameters are not used).

Is there a better and proper way to call this function than what I'm doing now:

toBeUsedByOthers(undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, 789, undefined, undefined)

?

Note:

Upvotes: 0

Views: 60

Answers (4)

vgoreiko
vgoreiko

Reputation: 45

I advice you to rewrite that function. Its example of anti-pattern. Its recommended to have not more then 3 arguments. If function has more, than: - it must be divided to more sub-functions; - it`s arguments can be converted to object (instance of class);

Anyway, if it`s critical for you to have such function or you do not have access to modify that code - use spread operator "...args" from es6 or write your own decorator for function. For example:

function multiply(x1, x2){
    return x1*x2;
}
function multiplyByTwo(a){
  return multiply(a, 2);
}

var b = multiplyByTwo(3);
console.log(b);

Upvotes: 0

Win
Win

Reputation: 5584

Use a payload object, that you can check against in your function, and then you can perform actions against the payload based on if they exist.

function helloWorld(payload){
   console.log(payload.text);
   if (typeof payload.variable !== 'undefined'){
      console.log(payload.variable);
   }
   if (payload.hasOwnProperty('variable2')) {
      console.log(payload.variable2);
   }
}

helloWorld({ text: 'hello', variable : 'world', variable2: 'cake' });

Of course you can do a check as soon as the function is called and check if the payload is valid. Which will verify that the payload can go ahead with the function. Check if all the properties exist and if it doesn't return from the function saying the payload is invalid.

var validPayloadProperties = ['a, b, c, d, e, f, g, h, x, y, z, userName, userId, userInfo3, userInfo4']

function helloWorld(payload){

   var payloadValid = true;

   // check payload to see if it has all the properties
   validPayloadProperties.forEach( function(property) {
     if (! payload.hasOwnProperty(property)) {
       payloadValid = false;
     }
   });

   // payload is not valid
   if(! payloadValid){
     return console.log('payload is not valid');
   }

   // payload is valid
  console.log('payload is valid');

}

// this is not valid...
helloWorld({ text: 'hello', variable : 'world', variable2: 'cake' });

Upvotes: 1

brk
brk

Reputation: 50316

You can pass an object with some key & values . Inside the function you can validate if the ke exist.

Also can use ... with argument object.Rest arguments are a real Array, and not merely Array-like like arguments

function toBeUsedByOthers(obj) {
  if (obj.a) {
    //rest of code
  }
  console.log(obj)
}

function toBeUsedByOthers2(...args) {
  if (args[0].a) {
    console.log(args[0].a);
  }

}

var myObject = {
  a: 'somVale1',
  b: 'someVale2',
}
toBeUsedByOthers(myObject)

toBeUsedByOthers2(myObject)

Upvotes: 0

Jonas Wilms
Jonas Wilms

Reputation: 138317

toBeUsedByOthers(undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, 789, undefined, undefined)

can be simplified with the spread operator

toBeUsedByOthers(...(new Array(12)).fill(undefined),789);

But generally, you should avoid that many unused params, may pass an object instead:

function better({a,b,c}){
  console.log(a,b,c);
}

better({c:0});

Upvotes: 1

Related Questions