如何减少if / else复杂度?

时间:2018-05-03 12:37:59

标签: javascript jquery if-statement cyclomatic-complexity

我有一些嵌套的if / else语句,但我想减少他们的开销。

在这个例子中,我正在评估哪个下拉列表中有一个li项目,如果这个li项目是第一个(currentIndex === 0)。

代码:

if (parentIndex === 1) {
  // But its own index is 0
  if (currentIndex === 0) {
    parentIndex = 2;
    updatedIndexPos = array[1];
    mainIndex =list_2.indexOf(updatedIndexPos);
    // If the previous line is -1:
    if (mainIndex === -1) {
      parentIndex = 1;
      updatedIndexPos = filterIndexPos;
      mainIndex = list_1.indexOf(updatedIndexPos);
    }
  } else {
    mainIndex = list_1.indexOf(mainIndexPos);
  }
} else if (parentIndex === 2) {
  // But its own index is 0
  if (currentIndex === 0) {
    parentIndex = 1;
    updatedIndexPos = array[0];
    mainIndex = list_1.indexOf(updatedIndexPos);
    // If the previous line is -1:
    if (mainIndex === -1) {
      parentIndex = 2;
      updatedIndexPos = filterIndexPos;
      mainIndex = list_2.indexOf(updatedIndexPos);
    }
  } else {
    mainIndex = list_2.indexOf(mainIndexPos);
  }
}

看着它有很多重用的代码和eslint给了我一个复杂的7。我已经尝试将其分解为更小的函数并传递args但仍未设法解决此代码块的复杂性。

4 个答案:

答案 0 :(得分:2)

尝试并提取出常见部分,例如(未经测试):

if (parentIndex === 1) {
  potentialNewParent = 2;
  potentialUpdatedIndexPos = array[1];
  nullParentIndex = 1;
  mainList = list_1;
  otherList = list_2;
} else {
  // Do the same
}

if (currentIndex === 0) {
  parentIndex = potentialNewParent;
  updatedIndexPos = potentialUpdatedIndexPos;
  mainIndex = mainList.indexOf(updatedIndexPos);
  // If the previous line is -1:
  if (mainIndex === -1) {
    parentIndex = nullParentIndex;
    updatedIndexPos = filterIndexPos;
    mainIndex = otherList.indexOf(updatedIndexPos);
  }
} else {
  mainIndex = otherList.indexOf(mainIndexPos);
}

答案 1 :(得分:2)

将逻辑建模为state-machine通常很有帮助。 所以你已经定义了它们之间的状态和转换。

var action = 'foo';
var actionParams = {...};
var currentState = getState({parentIndex: parentIndex, ...});
if (currentState.canPerform(action)) {
  currentState.perform(action, actionParams);
}

答案 2 :(得分:1)

假设parentIndex总是1或2,它可以简化很多。由于我不知道它应该做什么,我还没有对此进行测试,变量名称可能不合适:

<table>
<tr>
  <th>Number</th>
  <th>Details</th>
</tr>
<tr>
  <td>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
  proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</td>
  <td></td>
</tr>
<tr>
  <td></td>
  <td></td>
</tr>
<tr>
  <td></td>
  <td></td>
</tr>
</table>

答案 3 :(得分:0)

假设parentIndex只能是1或2,这对你有用吗?

var elements = [2, 1];
if ((parentIndex === 1 || parentIndex === 2) && currentIndex === 0) {

  oldParentIndex = parentIndex;
  parentIndex = elements[oldParentIndex-1];
  updatedIndexPos = array[oldParentIndex%2];
  mainIndex = this["list_"+parentIndex].indexOf(updatedIndexPos);

  if (mainIndex === -1) {
    parentIndex = oldParentIndex;
    updatedIndexPos = filterIndexPos;
    mainIndex = this["list_"+oldParentIndex].indexOf(updatedIndexPos);
  }
} else if ((parentIndex === 1 || parentIndex === 2) && currentIndex !== 0) {
  mainIndex = this["list_"+parentIndex].indexOf(updatedIndexPos);
}
相关问题