我的代码中的圈复杂度错误

Cyclomatic complexity error in my code

我的 g运行t 文件中有 2 个错误

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)

在这种情况下如何降低圈复杂度?我的功能没那么复杂

第一个错误

    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;
        }
      }
    }...

然后是第二个误差函数

    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;
        }
      }
    }

奇怪的是:这个错误只发生在我身上,我的同事打开相同的文件和 运行 g运行t 并且他们的终端没有错误。

降低函数的圈复杂度的方法是将其拆分为几个较小的函数,并将复杂度分布到易于理解的块中。例如,您可以提取 switch-case 语句,结果如下所示:

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;
}

然后,我们发现您也可以在第二个块中重用 getLineValue 函数:

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
}

因此,通过这一更改,您将复杂性分配给了另一个函数,并且还通过消除重复完全消除了一些复杂性。