vendredi 26 février 2016

Reducing the Quantity of Nested If-Statements

How can I reduce the number of if-statements in this (below) situation? I'm aware of switch-statements but can't imagine how that would help in this situation.

Here's what's happening below:

  1. The first IF safely checks for a date stored in NSDefault, that may or may not exist
  2. If the date exists (true), it checks if the date is before "now"
  3. If the date is before "now" (true), it safely checks for a user preference of On/Off in NSDefault, this too may or may not exist
  4. If it does exist, it checks if that its not equal to off
  5. Finally, if #4 was true, the function is called

    //Queue up more quote notifications?
    if let notif_EndDate = (userPref_NSDefault.objectForKey("notification_EndDate")) as? NSDate {
        if notif_EndDate.isLessThanDate(self.now) {
            if let quoteNotif_Pref = (userPref_NSDefault.stringForKey("WeeklyQuote_Pref")) {
                if quoteNotif_Pref != "Off" {
                    quoteNotifications()
                } else {
                    print("Use has set 'Weekly Quotes' to 'Off'")
                }
            } else {
                quoteNotifications()
            }
        }
    } else {
        quoteNotifications()
    }
    
    

Aucun commentaire:

Enregistrer un commentaire