What's the best way to refactor this code? The method does a lot of checking before showing an alert. And the method is too long for my taste.
I'm thinking of extracting the checkers into their own methods, but I'm not sure how to handle multiple instances that call return.
- (IBAction)saveCustomer:(id)sender {
@try {
[self.view endEditing:YES];
// Cannot save if customer is pending
if ([self.service isWaitingForApproval]) {
APAlert(WARNING_TITLE, NSLocalizedString(@"message.waiting-approval", nil));
return;
}
// Check required field
NSString *incompleteRequiredField = [self.service checkRequiredFields];
if (incompleteRequiredField) {
NSString *message = [NSString localizedStringWithFormat:NSLocalizedString(@"message.required field %@", nil), NSLocalizedString(incompleteRequiredField, nil)];
APAlert(WARNING_TITLE, message);
return;
}
if(![self.service isDistributorOrWholesalerInTW]) {
incompleteRequiredField = [self checkRequireCellOTM];
if(incompleteRequiredField) {
NSString *message = [NSString localizedStringWithFormat:NSLocalizedString(@"message.required otm %@", nil), NSLocalizedString(incompleteRequiredField, nil)];
APAlert(WARNING_TITLE, message);
return;
}
}
Country_Identifier countryID = (Country_Identifier)[self.service.customer.country_id integerValue];
if (countryID == Country_Taiwan || countryID == Country_Thailand) {
if (![self.service isHasContact]) {
APAlert(WARNING_TITLE, ALERT_CONFRIM_HAVE_CONTACT);
return;
}
}
[[SomeManager alertViewManager] showAlertWithTitle:SAVING_TITLE
message:SAVE_CUSTOMER_CONFIRM
target:self
actionView:nil
cancelButtonTitle:YES_BUTTON_TITLE
otherButtonTitles:NO_BUTTON_TITLE
andTag:AlertViewTag_Save];
}
@catch(NSException *exception) {
[self sendException:exception];
}
}