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 classpublic boolean validateUser(User user, StringBuffer errors) {    ...}// callerStringBuffer 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!
Is it good practice in Java to pass a StringBuffer to a method to get error messages
java
I quite like the idea when implementing error handlers to do stuff through an interface to remove any dependencies on modules.  Sometimes I've used that in conjunction with a global static Log method.  I've typically done this in c# but the theory is the same across to Java I believe.So how about something like this?interface IErrorHandler{   void addError(string message);   bool hasErrors();   void persist();}Then you could do either:// Possibly return an IErrorHandler instead of boolean here?public boolean validateUser(User user, IErrorHandler errorHandler) {   // on error   errorHandler.AddError(...);}Then create an implementation of errorHandler that wraps a StringBuffer (if you wish or whatever i.e. FileWriter, DbWriter etc)class MemoryErrorHandler implements IErrorHandler{   private final StringBuffer _errors;   public MemoryErrorHandler() {      _errors = new StringBuffer();   }   public void addError(string message) {      _errors.append(message);   }   public bool hasErrors() {      _errors.length() > 0;   }   public void persist() {      System.out.console.printlin(_errors.toString();   }}Then from the caller something likeIErrorHandler errorHandler = new MemoryErrorHandler();validateUser(user, errorHandler);if (errorHandler.hasErrors()) {    // do something with the errors    errorHandler.persist();}A further enhancement would be to consider DI of the IErrorHandler into your application so that it is only ever defined in one place.  That way will end up having dependencies on IErrorHandler throughout the code which would make it easier to unit test as required.Of course the IErrorHandler interface could include other methods, these were just suggestions based on your original code.