【问题标题】:How to reduce cognitive complexity in java code如何降低 Java 代码中的认知复杂度
【发布时间】:2019-09-18 23:53:45
【问题描述】:

请告知如何降低以下代码的认知复杂度

ids.add((row.getCell(11) != null) ? row.getCell(11).getStringCellValue() : "");

【问题讨论】:

  • 我会尝试的第一件事是只调用一次row.getCell(11),而不是两次:Cell cell = row.getcell(11); ids.add(cell != null ? cell.getStringValue() : "");
  • @Sony 你为什么编辑问题的答案?

标签: java sonarqube


【解决方案1】:

添加隐藏细节的方法,例如:

private String getCellValueOrDefault(Cell cell) {
    if (cell == null) {
       return "";
    }
    return cell.getStringValue();
}

然后使用方法:

ids.add(getCellValueOrDefault(row.getCell(11));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多