0

I am want to know following two code complexity can be reduced further. They are given complexity issues in my sonar server. need some experts help to resolve this issues without damage existing logic. I am using java 8 , it also fine are their any easy way in java 8 too.

Code 1 :

private DateTime GDT(final P1 prescriptionStartTimeHelper, P2doseDetailModel, P3 prescriptionModelSpi,
    final P4 prescriptionFacade) {
    final DepositModelSpi depositModel = //
    final DateTime userSetDateValue = //
        DateTime depositTime = //

    if ((userSetDateValue != null) && (DepositType.DATE == depositModel.getDepositTypeConstraint().getValue())) {
        final DosageMixedType doseType = //
        final LocalTime userSetLocalTime = //

        switch (doseType) {
            case A:

                // find the unit time list
                final List<LocalTime> unitTimeList = //

                if (!unitTimeList.contains(userSetLocalTime)) {
                    final LocalTime firstOccasionTime = unitTimeList.get(0);
                    depositTime =//

                } else {
                    depositTime = //
                }

                break;
            case B:

                final LocalTime doseTime = //;
                    depositTime = //

                break;
            case C:
            case D:
            case E:
            case F:
            case G:
                depositTime = //;
                break;
            default:
                depositTime = //

        }
    }

    return depositTime;
}
Madplay
  • 1,027
  • 1
  • 13
  • 25
uma
  • 1,477
  • 3
  • 32
  • 63
  • 1
    Wouldn't it matter, how you evaluate the `depositTime`? – Naman May 16 '19 at 01:58
  • 2
    The code you've posted looks utterly straightforward: if, switch, if (in one branch of the switch). Unless there's more complexity in the computations you didn't show us, I see little to be concerned about. –  May 16 '19 at 02:32
  • @Naman deposit time calculate different way calling different methods.I need to know , are there any way to combine C,D,E,F,G – uma May 16 '19 at 02:34
  • 1
    In terms of cyclomatic complexity, C,D,E,F,G are already combined. – Rocky Sims May 16 '19 at 02:47
  • @RockySims nope. sonar appending +1 , point each C,D,E,F – uma May 16 '19 at 02:54
  • 1
    Well, you could change to a chain of if else statements (where one if statement's condition is C || D || E || F || G) though that won't actually speed it up it should cause your software to report a lower cyclomatic complexity. – Rocky Sims May 16 '19 at 02:57
  • @RockySims i will try. can be do it using map. in separate method instead connect with or ? – uma May 16 '19 at 04:23
  • 1
    This method seems kind of long, as well as cyclomatically complex. I'd be looking to split it down into smaller methods, based on the length alone. Would it make sense to refactor everything inside the `if` into a separate method? This would certainly solve your Sonar problem. – Dawood ibn Kareem May 16 '19 at 20:59

0 Answers0