I have a class that has a method which takes a user as an argument and performs several validation checks on the user and returns a boolean to indicate if theyare valid.
I want to be able to get error messages back from the validation method along with the boolean return value.
I was thinking of doing it by passing a StringBuffer as an argument along with the user and in the validation code appending error messages to the buffer so that the caller gets the boolean returned and then can look at the StringBuffer for error messages.
// validation class
public boolean validateUser(User user, StringBuffer errors) {
...
}
// caller
StringBuffer errors = new StringBuffer();
boolean validUser = validateUser(user, errors);
if (!validUser) {
log(errors.toString();
}
Is this good practice in Java? Or would it be better to log the errors to a buffer in the validation class and have a public method to get the errors? Or should the validateUser() method return an object that contains the boolean and errors?
Thank you!
Appendable
orCharSequence
parameter instead of aStringBuffer
would be more flexible and allow e.g.StringBuilder
s. \$\endgroup\$