3

Here is my code

public class Validator {
private String message = "ok";

public String mainValidate(String value) {
    if(!isAccept1()) {
        message = "fail1";
        return message;
    }

    if(!isAccept2()) {
        message = "fail2";
        return message;
    }

    if(!isAccept3()) {
        message = "fail3";
        return message;
    }
    return message;
}

public boolean isAccept1() {}

public boolean isAccept2() {}

public boolean isAccept3() {}

Requirement is: If the code meet any error, return message immediately. As you can see, with current code, I repeat myself very much. How can I structure the code and still keep requirement. If any error occur, the code skip other validate and return error message

Many thanks!

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
duy
  • 579
  • 5
  • 16
  • This seems to be a very broad topic which is mostley opinion based. I recommend to use a way that works for you and your application. – B001ᛦ Oct 23 '15 at 12:40
  • You already return imemdiately. I did not understand what you try to achieve here. – newbieee Oct 23 '15 at 12:41
  • Can you recommend a way for this case? – duy Oct 23 '15 at 12:42
  • 1
    Shouldn't this belongs to [programmers](http://programmers.stackexchange.com/) forum ? Please refer to this [answer](http://programmers.stackexchange.com/a/234617/167527) – Jude Niroshan Oct 23 '15 at 12:42
  • You could put all checks in one method and just call the one method... – brso05 Oct 23 '15 at 12:44

3 Answers3

3

You can put all the checks in one method:

public String mainValidate(String value) {
    String message = isAccept();
    if(!message.equalsIgnoreCase("ok")) {
        return message;
    }
}

private String isAccept() {
    String returnString = "ok";
    //check1 - change returnString to whatever message if check fails
    //check2 - change returnString to whatever message if check fails
    //check3 - change returnString to whatever message if check fails
    //etc...
    return returnString;
}
brso05
  • 13,142
  • 2
  • 21
  • 40
  • 1
    How about using **Decorator Pattern** for those different types of validations? I think that would be easier when another new type of validations come in future. – Jude Niroshan Oct 23 '15 at 12:57
2

You could store the actions / messages in a map and iterate over it:

private static final Map<Predicate<String>, String> VALIDATIONS = new LinkedHashMap<> ();

static {
  VALIDATIONS.put(Validator::isAccept1, "fail1");
  VALIDATIONS.put(Validator::isAccept2, "fail2");
  //etc.
}

public String mainValidate(String value) {
  for (Entry<Predicate<String>, String> v : VALIDATIONS.entrySet()) {
    Predicate<String> validator = v.getKey();
    String errorMsg = v.getValue();
    if (!validator.test(value)) return errorMsg;
  }
  return "ok";
}

public static boolean isAccept1(String value) { return /* ... */; }
assylias
  • 321,522
  • 82
  • 660
  • 783
1

This is known as:

See also A good Design-by-Contract library for Java?

Community
  • 1
  • 1
Verhagen
  • 3,885
  • 26
  • 36