multiple arguments and conditions in a function in javascript

1k Views Asked by At

I have a function that return some informations about employees from an object database

I'm facing some complexity ESlint Problems, so i need to find a way to minimize it or find a smart way to do this instead of using a whole set of if statements.

I'm also trying to find a way to something like this: if I have name, I don't need to do the id verification and vice-versa and i just don't know how to do this... ‍♂️

The function must receive as argument an object of options that will determinate how it will behave:

  • name: the first name or last name of the person that need to be searched at database
  • id: the id of the person that need to be searched

e.g:

getEmployeesCoverage({ name: 'Sharonda' });
getEmployeesCoverage({ name: 'Spry' });
getEmployeesCoverage({ id: '4b40a139-d4dc-4f09-822d-ec25e819a5ad' });

I have 3 conditions:

  1. verify if i receive any arguments e.g name or id is undefined
  2. verify if name exists in database
  3. verify if id exists in database

I tried this:

function getEmployeesCoverage({ name, id }) {

  if (employees.find((employee) => employee.id === id)) {
    return getEmployeeById(id); // 3. verify if id exists in database
  }
  if (
    employees.find((employee) => employee.lastName === name) ||
    employees.find((employee) => employee.firstName === name)
  ) {
    return getEmployeeByName(name); // 2. verify if the name exists in database
  }
  if (!name || !id) { 
    return getAllEmployees(); // 1. verify if i have any args e.g name or id undefined
  } 
  throw new Error('Invalid Information');
}
3

There are 3 best solutions below

0
Rafo On BEST ANSWER

I resolve that problem by just checking the arguments in the main function:

function getEmployeesCoverage({ name, id } = {}) {
  if (name) return getEmployeeByName(name);
  if (id) return getEmployeeById(id);
  if (!name || !id) return getAllEmployees();
}

And then calling the unique job function that verifies if that argument exist in database and if not throw and error.

function getEmployeeByName(name) {
  if (employees.find((employee) => employee.lastName === name)
  || employees.find((employee) => employee.firstName === name)) {
    const {
      id: employeeId,
      firstName,
      lastName,
      responsibleFor,
    } = employees.find((employee) => employee.lastName === name)
    || employees.find((employee) => employee.firstName === name);
    return {
      id: employeeId,
      fullName: `${firstName} ${lastName}`,
      species: getSpeciesByIds(...responsibleFor).map((animal) => animal.name),
      locations: getSpeciesByIds(...responsibleFor).map((animal) => animal.location),
    };
  }
  throw new Error('Invalid Informations');
}
2
Mike Clark On

Cyclomatic complexity is just a style suggestion that ESLint provides to encourage you to use fewer code branches (e.g. ifs) in a single function. You can ignore the warning or disable it with ESLint pragmas (the rule in question is named complexity).

If you want a single function that is able to query in different ways (as you're doing in your code), it stands to reason that your function will need to branch out based on the input data.

For example, Python functions do this all the time by querying which kwargs were and weren't supplied to an "overloaded" function and the function changes behavior based on the suppied args. I'm not sure why your ESLint is configured with such a low complexity value, or maybe it comes with a low value.

Last time I used ESLint I found myself disabling three or four "code style" suggestions right away. I personally think it's overly opinionated by default.

2
Redu On

Perhaps you can do like;

var employee = o.id   ? employees.find(e => e.id === o.id) :
               o.name ? employees.find(e => e.lastName === o.name || e.firstName === o.name)
                      : undefined;

So if ESLint complains for nested ternary perhaps you may try;

var employee = (o.id   && employees.find(e => e.id === o.id)) ||
               (o.name && employees.find(e => e.lastName === o.name || e.firstName === o.name));

and if all fails you get false.