【问题标题】:To refactor multiple || and && conditions inside If statement in java重构多个 || java中If语句中的&&条件
【发布时间】:2021-08-16 10:48:29
【问题描述】:

重构多个 || java中If语句中的&&条件

重构多个 || java中If语句中的&&条件

重构多个 || java中If语句中的&&条件

for (Student students : studentList) {

    if ((Constants.CODE1.equals(students.getactivities().getCode())
            && ValidationRepository.validateStudentId1(department.getId()))
            || ((Constants.CODE2).equals(students.getactivities().getCode())
                    && ValidationRepository.validateStudentId2(department.getId())
                    && ValidationRepository.validateStudentId3(department.getId()))
            || ((Constants.CODE3
                    .equals(students.getactivities().getCode())
                    || Constants.CODE4
                            .equals(students.getactivities().getCode()))
                    && (ValidationRepository.validateStudentId4(
                            department.getId(),
                            students.getactivities().getCode())))
            || ((Constants.CODE5.equals(students.getactivities().getCode())
                    || Constants.CODE6
                            .equals(students.getactivities().getCode())

                    || Constants.CODE7
                            .equals(students.getactivities().getCode())
                    || Constants.CODE15
                            .equals(students.getactivities().getCode())

                    || Constants.CODE8
                            .equals(students.getactivities().getCode())
                    || Constants.CODE9
                            .equals(students.getactivities().getCode())

                    || Constants.CODE10
                            .equals(students.getactivities().getCode())
                    || Constants.CODE11
                            .equals(students.getactivities().getCode()))

                    && (ValidationRepository.validateStudentId4(
                            department.getId(),
                            students.getactivities().getCode())))

    )
    { 
        some statements
    }

【问题讨论】:

  • 请提供格式良好的代码和相关的类定义。
  • 创建一个包含所有可能性的列表,并使用“包含”

标签: java if-statement refactoring


【解决方案1】:

创建一个方法来验证学生 在方法内部创建常量代码和验证 repo 函数的映射

public boolean validateStudent(String constantCode, departmentId){
Map<String, Boolean> map = new HashMap<>();
map.put(Constants.CODE1,  
ValidationRepository.validateStudentId1(departmentId));
map.put(Constants.CODE2,  
ValidationRepository.validateStudentId2(departmentId));

if(map.containsKey(constantCode)){
        return map.get(operation);
    
    }
    
    return false;


 }

现在你可以像下面这样调用验证方法

validateStudent(constantCode, departmentId)

在内部,常量代码键的映射值将是您的函数。 确保在 map 中使用相同的方法参数名称和键值。

【讨论】:

    【解决方案2】:

    让我给你一个建议:

    1. 我相信您可以包含列表中的所有代码并检查天气 students.getactivities().getCode() 是否存在于列表中。
    listOf(Constants. CODE7, Constants.CODE8, Constants.CODE9 ...).contains(students.getactivities().getCode());
    
    1. 您还应该将一段代码提取到外部方法中:
    private validateStudentWithCode1(Students students) 
    {
    return Constants.CODE1.equals(students.getactivities().getCode())
                             && ValidationRepository.validateStudentId1(department.getId());
    }
    

    【讨论】:

      【解决方案3】:

      这个问题没有明确的答案,但我有以下建议:

      1. 不要每次都打电话给students.getactivities().getCode()。它不仅使代码的可读性降低,而且效率低下。 (除非此方法每​​次都返回相同的值!) 而是在 if 语句之前使用变量:
      int code = students.getactivities().getCode();
      

      然后,您可以随时引用该变量。 department.getId() 也可以这样做。

      1. 您可以使用辅助方法来避免将该代码与多个常量进行比较,如下所示:
      private boolean matchesAny(int code, int... possibleMatches) {
          for (int match: possibleMatches) {
              if (code == match) return true;
          }
          return false;
      }
      

      这样,您的代码将如下所示:

          int departmentId = department.getId();
          for (Student students : studentList) {
              int code = students.getactivities().getCode();
              // Case 1
              if ((Constants.CODE1.equals(code)
                      && ValidationRepository.validateStudentId1(departmentId))
                      // Case 2
                      || (Constants.CODE2.equals(code)
                      && ValidationRepository.validateStudentId2(departmentId)
                      && ValidationRepository.validateStudentId3(departmentId))
                      // Case 3
                      || (matchesAny(code, Constants.CODE3, Constants.CODE4)
                      && ValidationRepository.validateStudentId4(departmentId, code))
                      // Case 4
                      || (matchesAny(code,
                          Constants.CODE5,
                          Constants.CODE6,
                          Constants.CODE7,
                          Constants.CODE8,
                          Constants.CODE9,
                          Constants.CODE10,
                          Constants.CODE11,
                          Constants.CODE15)
                      && ValidationRepository.validateStudentId4(departmentId, code))) {
                  // some statements
              }
          }
          // rest of method
      
      private boolean matchesAny(int code, int... possibleMatches) {
          for (int match : possibleMatches) {
              if (code == match) return true;
          }
          return false;
      }
      

      仍然不漂亮,但已经好多了。为了使它更好,您可以尝试将这 4 个案例转换为单独的语句,然后让它们都调用相同的代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-21
        相关资源
        最近更新 更多