如何降低 if/else 复杂度?

How to reduce if/else 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 但仍然没有设法解决这个代码块的复杂性。

尝试提取公共部分,例如(未测试):

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

假设parentIndex始终为1或2,可以简化很多。因为我不知道它应该做什么,所以我没有测试过这个,变量名可能不合适:

const mainList = parentIndex === 1 ? list_1 : list_2;
const secondaryList = parentIndex === 1 ? list_2 : list_1;

if (currentIndex === 0) {
  parentIndex = parentIndex === 1 ? 2 : 1;
  updateIndexPos = array[parentIndex - 1];
  mainIndex = secondaryList.indexOf(updatedIndexPos);
  if (mainIndex === -1) {
    parentIndex = parentIndex === 1 ? 2 : 1;
    updatedIndexPos = filterIndexPos;
    mainIndex = mainList.indexOf(updatedIndexPos);
  }
} else {
  mainIndex = mainList.indexOf(mainIndexPos);
}

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

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

假设 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);
}