skip to Main Content

I am trying to get rid all of the warnings in my project, but i stumbled onto "warning Function ‘getPositionValue’ has a complexity of 7. Maximum allowed is 5" and I am not sure how to solve it. It is a .js file.

This is the code:

export function getPositionValue(station) {
  const { latitude, longitude } = station;
  if (latitude && longitude) {
    return `${latitude}, ${longitude}`;
  }
  if (latitude && !longitude) {
    return `${latitude}, 0`;
  }
  if (!latitude && longitude) {
    return `0, ${longitude}`;
  }
  return `${0}, ${0}`;
}

Any idea how to solve this warning is welcome!

2

Answers


  1. You can take advantage of using default values in destructuring so your function could look like:

    export function getPositionValue(station) {
      const { latitude = 0, longitude = 0 } = station;
      return `${latitude}, ${longitude}`;
    }
    
    Login or Signup to reply.
  2. If you are using ESLint, this errors refers to the amount of paths that your code can pass until return something. You can see the details of this rule at https://eslint.org/docs/latest/rules/complexity.

    For solve the problem, you can use "if – else" according to the docs instead of "if – if – if" sequence.

    also, you can Change the maximum of complexity in your ES configs (.eslintrc.cjs) for 7 (not recommended).

    Hope I could help you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search