【问题标题】:Are there anyway to reduced complexiy of following java code?反正有没有降低跟随java代码的复杂性?
【发布时间】:2019-07-16 14:51:31
【问题描述】:

我需要根据声纳可接受的级别降低以下 java 方法的复杂性。现在,它给出了类似声纳的问题。

需要一些专家的帮助才能做到这一点。

public List<X> Y(final DateTime treatmentDiscontinueTime,
                                                    final List< P> validPrescribedPrescriptions)
  {
    final List<x> doseWrapperList = new ArrayList<>();
    final int noOfPrescriptions = validPrescribedPrescriptions.size();

    for (int prescriptionIndex = 0; prescriptionIndex < noOfPrescriptions; prescriptionIndex++)
    {
      final BasePrescribedPrescription basePrescribedPrescription = validPrescribedPrescriptions.get(prescriptionIndex);
      final String firstDoseText = basePrescribedPrescription.getFirstText();
      final String secondDoseText = basePrescribedPrescription.getSecondText();
      final boolean accordingToSchedule = A.ACCORDING.equals(firstDoseText);
      final boolean specificPrescription = A.SP.equals(firstDoseText);
      final boolean specificVbTypePrescription = A.SPVB.equals(firstDoseText);

      List<D> doseDetails = new ArrayList<>(basePrescribedPrescription.getDoseDetails());


      final DateTime changedDosageEndDate =
        getChangedDoseEndDate(basePrescribedPrescription.getActualTerminateDate(), treatmentDiscontinueTime);
      final int noOfDosages = doseDetails.size();

      for (int doseIndex = 0; doseIndex < noOfDosages; doseIndex++)
      {
        final D doseDetail = doseDetails.get(doseIndex);

        if ((doseDetail.getStart().getStartDate() != null) && (changedDosageEndDate != null) &&
            doseDetail.getStart().getStartDate().isAfter(changedDosageEndDate))
        {
          continue;
        }

        String previewDoseText;

        if (accordingToSchedule)
        {
          previewDoseText = X
        }
        else if (specificPrescription)
        {
          previewDoseText = Y;
        }
        else if (specificVbTypePrescription)
        {
          previewDoseText = Z;
        }
        else if (noOfDosages == 2)
        {

          previewDoseText = ((doseIndex == 0) ? secondDoseText : firstDoseText);
        }
        else
        {
          previewDoseText = firstDoseText;
        }

        final boolean isUnplanned =isuNplaned()



        if (!isUnplanned)
        {
          doseStart = getStartDate();
          doseEnd = getEndDate();
        }


        doseWrapperList.add(new DoseInfoLiteDTOWrapper(previewDoseText, doseStart, doseEnd, doseDetail));

      }
    }
    return doseWrapperList;
  }

我需要一些专家的帮助来解决这个声纳问题。我想用不同的方法来提取代码片段,把这个方法分解成小部分。但仍然找不到合适的方法。

【问题讨论】:

  • 如果代码有效,请转至Code Review 询问。
  • 提示:只在有意义的地方使用final。将它附加到每个变量上都会产生很多无用的线路噪音。
  • 您可以尝试的一件事是将那个巨大的 if 块移动到像 previewDoseText = theNewMethod() 这样的方法中。
  • "previewDoseText = ((doseIndex == 0) ? secondDoseText : firstDoseText);" 看起来不对……如果是第 0 剂,那肯定不是第 2 剂?
  • @uma 它可能工作,但它看起来不对,从而增加了认知负担。

标签: java java-8 refactoring cyclomatic-complexity


【解决方案1】:

我认为不难清除:

  • 使用简单的 For 循环
  • 为 For 循环创建更多的小方法来做小(清晰)的事情
  • 阻止 if/esle:使用简单语句

提示:学习 TDD 以尽可能编写干净的代码

【讨论】:

  • 仅作记录:无需 tdd 即可遵循干净的代码。但是正确的 tdd 确实有帮助!
  • 是的,有很多方法可以做到这一点,tdd 学起来不难,但应用起来并不容易。对我来说,尽可能结合一切:+ OOP + 设计模式 + SOLID + TDD ....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-07
  • 2022-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多