lundi 2 septembre 2019

How can the if-else conditions be more optimally coded?

func validateAllField (array : [FormPreferencesModel]?) -> Bool {
    let isEmailFieldVisible = self.isEmailFieldVisible(array)
        let isCustomFieldVisible = self.isCustomFieldVisible(array)
        let isCompanyFieldVisible = self.isCompanyFieldVisible(array)
        let purpose = getSelectedPurpose()
        if purpose.captureGuestImage == true {
            if (self.emailTextField.tag == 101) {
                if (isEmailFieldVisible == false) {
                    if (!self.userName.isEmpty && self.userName.count > 2) && (!customField.isEmpty) && (self.visitorImageView.isHidden == false) && (self.visitorImageView.image != nil) {
                        activateNextButton()
                        return true
                    }
                } else {
                    if (!self.userName.isEmpty && self.userName.count > 2) && (!self.userEmailId.isEmpty && self.isValidEmail(email: self.userEmailId)) && (!customField.isEmpty) && (self.visitorImageView.isHidden == false) && (self.visitorImageView.image != nil) {
                        activateNextButton()
                        return true
                    }
                }

                if (isCustomFieldVisible == false) {
                    if (!self.userName.isEmpty && self.userName.count > 2) && (!self.userEmailId.isEmpty && self.isValidEmail(email: self.userEmailId)) && (self.visitorImageView.isHidden == false) && (self.visitorImageView.image != nil) {
                        activateNextButton()
                        return true
                    }
                } else {
                    if (!self.userName.isEmpty && self.userName.count > 2) && (!self.userEmailId.isEmpty && self.isValidEmail(email: self.userEmailId)) && (!customField.isEmpty) && (self.visitorImageView.isHidden == false) && (self.visitorImageView.image != nil) {
                        activateNextButton()
                        return true
                    }
                }
                if (isEmailFieldVisible == false && isCustomFieldVisible == false) {
                    if (!self.userName.isEmpty && self.userName.count > 2) && (self.visitorImageView.isHidden == false) && (self.visitorImageView.image != nil) {
                        activateNextButton()
                        return true
                    }
                }
            } else {
                if (isCompanyFieldVisible == false) {
                    if (!self.userName.isEmpty && self.userName.count > 2) && (!customField.isEmpty) && (self.visitorImageView.isHidden == false) && (self.visitorImageView.image != nil) {
                        activateNextButton()
                        return true
                    }
                } else {
                    if (!self.userName.isEmpty && self.userName.count > 2) && (!self.userEmailId.isEmpty && self.userEmailId.count >= 2) && (!customField.isEmpty) && (self.visitorImageView.isHidden == false) && (self.visitorImageView.image != nil) {
                        activateNextButton()
                        return true
                    }
                }

                if (isCustomFieldVisible == false) {
                    if (!self.userName.isEmpty && self.userName.count > 2) && (!self.userEmailId.isEmpty && self.userEmailId.count >= 2) && (self.visitorImageView.isHidden == false) && (self.visitorImageView.image != nil) {
                        activateNextButton()
                        return true
                    }
                } else {
                    if (!self.userName.isEmpty && self.userName.count > 2) && (!self.userEmailId.isEmpty && self.userEmailId.count >= 2) && (!customField.isEmpty) && (self.visitorImageView.isHidden == false) && (self.visitorImageView.image != nil) {
                        activateNextButton()
                        return true
                    }
                }
                if (isCompanyFieldVisible == false && isCustomFieldVisible == false) {
                    if (!self.userName.isEmpty && self.userName.count > 2) && (self.visitorImageView.isHidden == false) && (self.visitorImageView.image != nil) {
                        activateNextButton()
                        return true
                    }
                }
            }
        } else {
            if (self.emailTextField.tag == 101) {
                if (isEmailFieldVisible == false) {
                    if (!self.userName.isEmpty && self.userName.count > 2) && (!customField.isEmpty) {
                        activateNextButton()
                        return true
                    }
                } else {
                    if (!self.userName.isEmpty && self.userName.count > 2) && (!self.userEmailId.isEmpty && self.isValidEmail(email: self.userEmailId)) && (!customField.isEmpty) {
                        activateNextButton()
                        return true
                    }
                }

                if (isCustomFieldVisible == false) {
                    if (!self.userName.isEmpty && self.userName.count > 2) && (!self.userEmailId.isEmpty && self.isValidEmail(email: self.userEmailId)) {
                        activateNextButton()
                        return true
                    }
                } else {
                    if (!self.userName.isEmpty && self.userName.count > 2) && (!self.userEmailId.isEmpty && self.isValidEmail(email: self.userEmailId)) && (!customField.isEmpty) {
                        activateNextButton()
                        return true
                    }
                }
                if (isEmailFieldVisible == false && isCustomFieldVisible == false) {
                    if (!self.userName.isEmpty && self.userName.count > 2) {
                        activateNextButton()
                        return true
                    }
                }
            } else {
                if (isCompanyFieldVisible == false) {
                    if (!self.userName.isEmpty && self.userName.count > 2) && (!customField.isEmpty) {
                        activateNextButton()
                        return true
                    }
                } else {
                    if (!self.userName.isEmpty && self.userName.count > 2) && (!self.userEmailId.isEmpty && self.userEmailId.count >= 2) && (!customField.isEmpty) {
                        activateNextButton()
                        return true
                    }
                }

                if (isCustomFieldVisible == false) {
                    if (!self.userName.isEmpty && self.userName.count > 2) && (!self.userEmailId.isEmpty && self.userEmailId.count >= 2) {
                        activateNextButton()
                        return true
                    }
                } else {
                    if (!self.userName.isEmpty && self.userName.count > 2) && (!self.userEmailId.isEmpty && self.userEmailId.count >= 2) && (!customField.isEmpty) {
                        activateNextButton()
                        return true
                    }
                }
                if (isCompanyFieldVisible == false && isCustomFieldVisible == false) {
                    if (!self.userName.isEmpty && self.userName.count > 2) {
                        activateNextButton()
                        return true
                    }
                }
            }
        }
        return false
    }

I just want to reduce the code complexity by more efficiently implementing the if-else conditions. There are three types of fields which require validation. All of the conditions are mandatory; is there a way to implement this more efficiently?

Aucun commentaire:

Enregistrer un commentaire