Essentially while this is certainly a basic question with numerous reasons, I cannot seem to understand why it would be good idea to use throw when writing functions that validate.
For example if you take this simple username and password validation scenario, why would anyone want to use throw to help with the validation?
Does it just come down to coding style preference? Or is there a good reason one would opt for using throw for the return of this function?
enum ReturnMessages: String {
case passed, failed, tooshort, nonumbers
}
func validateCredentials(username: String, password: String) -> ReturnMessages {
guard (username.count >= 4 && password.count >= 4) else {return ReturnMessages.tooshort}
guard (password.rangeOfCharacter(from: .decimalDigits) != nil) else {return ReturnMessages.nonumbers}
return ReturnMessages.passed
}
validateCredentials(username: "jeff", password: "password")