0

I am getting 2 errors in my grunt file

line 61 col 25 This function's cyclomatic complexity is too high. (10)

line 101 col 22 This function's cyclomatic complexity is too high. (10)

how could I reduce the Cyclomatic complexity in this case ? my functions aren't that complex

first error

    remove: function(line, row, type) {
      var spreadSelected = (row.spreadSelected && type === 'spread'),
        totalSelected = (row.totalSelected && type === 'total'),
        moneyLineSelected = (row.moneyLineSelected && type === 'moneyline'),
        lineValue;
      if (spreadSelected || totalSelected || moneyLineSelected) {
        switch (type) {
          case 'spread':
            lineValue = row.spread.line;
            break;
          case 'total':
            lineValue = row.total.line;
            break;
          case 'moneyline':
            lineValue = row.moneyLineId;
            break;
          default:
            break;
        }

        AuthFactory.getCustomer().then(function(credentials) {
          betSlipSelectionRequest('/betSlip/removeSelection', {
            customerId: credentials.customer,
            game: row.game,
            pair: row.pair,
            line: lineValue
          });
        });

        if (spreadSelected) {
          row.spreadSelected = false;
        }
        if (totalSelected) {
          row.totalSelected = false;
        }
        if (moneyLineSelected) {
          row.moneyLineSelected = false;
        }
      }
    }...

and then the 2nd error function

    add: function(line, row, type) {
      var spreadSelected = (row.spreadSelected && type === 'spread'),
        totalSelected = (row.totalSelected && type === 'total'),
        moneyLineSelected = (row.moneyLineSelected && type === 'moneyline'),
        lineValue;
      if (!(spreadSelected || totalSelected || moneyLineSelected)) {
        switch (type) {
          case 'spread':
            lineValue = row.spread.line;
            break;
          case 'total':
            lineValue = row.total.line;
            break;
          case 'moneyline':
            lineValue = row.moneyLineId;
            break;
          default:
            break;
        }
        AuthFactory.getCustomer().then(function(credentials) {
          betSlipSelectionRequest('/betSlip/addSelection', {
            customerId: credentials.customer,
            game: row.game,
            pair: row.pair,
            line: lineValue
          });
        });
        switch (type) {
          case 'spread':
            row.spreadSelected = true;
            break;
          case 'total':
            row.totalSelected = true;
            break;
          case 'moneyline':
            row.moneyLineSelected = true;
            break;
        }
      }
    }

the weird thing here: this error is only with me, my co-worker opens the same files and run grunt and there is no errors in their terminals.

Rosa
  • 642
  • 6
  • 20
Non
  • 8,409
  • 20
  • 71
  • 123

1 Answers1

3

The way to reduce cyclomatic complexity of a function is to split it into several smaller functions and distribute that complexity into easy-to-understand chunks. For instance, you can extract your switch-case statement, resulting in something like this:

remove: function(line, row, type) {
    var spreadSelected = (row.spreadSelected && type === 'spread'),
        totalSelected = (row.totalSelected && type === 'total'),
        moneyLineSelected = (row.moneyLineSelected && type === 'moneyline'),
        lineValue;
    if (!(spreadSelected || totalSelected || moneyLineSelected)) {
        lineValue = getLineValue(row, type);
    }
    // ... and so on, in reasonable chunks.
}

function getLineValue(row, type) {
    var lineValue;
    switch (type) {
        case 'spread':
            lineValue = row.spread.line;
            break;
        case 'total':
            lineValue = row.total.line;
            break;
        case 'moneyline':
            lineValue = row.moneyLineId;
            break;
        default:
            break;
    return lineValue;
}

Then, we find that that you can reuse the getLineValue function in your second block as well:

add: function(line, row, type) {
    var spreadSelected = (row.spreadSelected && type === 'spread'),
        totalSelected = (row.totalSelected && type === 'total'),
        moneyLineSelected = (row.moneyLineSelected && type === 'moneyline'),
        lineValue;

    if (!(spreadSelected || totalSelected || moneyLineSelected)) {
        lineValue = getLineValue(row, type);
    }
    // ... and so on
}

So with this one change, you've distributed the complexity to another function, and also completely eliminated some complexity by eliminating duplication.

Rosa
  • 642
  • 6
  • 20