ios - Refactor a switch statement with cyclomatic complexity upper than 10 -


i've 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 know how optimize counter cyclomatic complexity this, without array of string or that,

currently complexity equals 11

thanks help

an array of simple structure containing title, storyboardname , vcname solve neatly.

if type ordered 0..10, can use index array. if type doesn't run 0..10, include in structure, , first find index need.

define structure

struct notificationtypedata {     var notificationtype    : int    = 0     var title               : string = ""     var storyboardname      : string = ""     var vcname              : string = "" } 

and set variable hold it

var notificationtypedata : [notificationtypedata]? 

you need populate - file, or hard-coded. here 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 simplify switch code 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              }          }      } 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -