Search code examples
iosswiftcyclecyclomatic-complexity

Refactor a switch statement with cyclomatic complexity upper than 10


i've this code:

for notif in EnvironmentManager.notif {
    if let type = notif.type {
            switch type {
            case .SitterAvailable:
                self.manageNotif(notif, title: "Une sitter disponible", storyboardName: "searchGuard", vcName: "searchGuardNCSID")
            case .OccasionalAdjustmentReminder:
                self.manageNotif(notif, title: "Rappel", storyboardName: "searchGuard", vcName: "searchGuardNCSID")
            case .GuardRequest:
                self.manageNotif(notif, title: "Nouvelle garde urgente", storyboardName: "EmergencyGuardSitter", vcName: "EmergencyGuardSitterNavigationControllerSID")
            case .NewReservationRequest:
                self.manageNotif(notif, title: "Nouvelle garde", storyboardName: "GuardWebSitter", vcName: "WebGuardSitterNavigationControllerSID")
            case .NewMessage:
                self.manageNotif(notif, title: "Nouveau message", storyboardName: "MessageListSitter", vcName: "messageSitterViewNavigationControllerSID")
            case .SoonReservationStartReminder:
                self.manageNotif(notif, title: "Rappel", storyboardName: "GuardListSitter", vcName: "guardListSitterNavigationControllerSID")
            case .ReservationAccepted:
                self.manageNotif(notif, title: "Garde acceptée", storyboardName: "GuardListSitter", vcName: "guardListSitterNavigationControllerSID")
            case .ReservationRefused:
                self.manageNotif(notif, title: "Garde refusée", storyboardName: "GuardListSitter", vcName: "guardListSitterNavigationControllerSID")
            case .NewMessageParent:
                self.manageNotif(notif, title: "Nouveau Message", storyboardName: "MessageParent", vcName: "messageParentViewNavigationControllerSID")
            }
        }
    }

i want to know how to optimize that for counter cyclomatic complexity this, so without a array of string or something like that,

currently complexity equals 11

thanks for help


Solution

  • An array of a simple structure containing title, storyboardName and vcName would solve this very neatly.

    If the type is ordered from 0..10, then you can just use the index into your array. If type doesn't run from 0..10, you could include it in the structure, and first find the index you need.

    Define your structure like this

    struct NotificationTypeData
    {
        var notificationType    : Int    = 0
        var title               : String = ""
        var storyboardName      : String = ""
        var vcName              : String = ""
    }
    

    and then set up a variable to hold it

    var notificationTypeData : [NotificationTypeData]?
    

    You will need to populate it - from a file, or hard-coded. Here are the first few...

    notificationTypeData!.append(NotificationTypeData(notificationType: 0, title: "Une sitter disponible", storyboardName: "searchGuard", vcName: "searchGuardNCSID"))
    notificationTypeData!.append(NotificationTypeData(notificationType: 1, title: "Rappel", storyboardName: "EmergencyGuardSitter", vcName: "EmergencyGuardSitterNavigationControllerSID"))
    notificationTypeData!.append(NotificationTypeData(notificationType: 2, title: "Nouvelle garde urgente", storyboardName: "GuardWebSitter", vcName: "WebGuardSitterNavigationControllerSID"))
    notificationTypeData!.append(NotificationTypeData(notificationType: 3, title: "Nouvelle garde", storyboardName: "MessageListSitter", vcName: "messageSitterViewNavigationControllerSID"))
    notificationTypeData!.append(NotificationTypeData(notificationType: 4, title: "Nouveau message", storyboardName: "GuardListSitter", vcName: "guardListSitterNavigationControllerSID"))
    

    and then simplify the switch code that you already have

    for notif in EnvironmentManager.notif
    {
        if let type = notif.type
        {
             switch type
             {
                 let data = notificationTypeData!.filter{ $0.notificationType == type }.first
                 if data != nil
                 {
                     self.manageNotif(notif, title: data?.title, storyboardName: data?.storyboardName, vcName: data?.storyboardName
                 }
             }
         }