简体   繁体   中英

Java Error/Exception handling with returning value

So my friend and I are programming Blackjack in Java, and we wanted to test our input fields for the correct input(eg only number input). So we sat at his PC and he wrote this solution:

    public static boolean testeTextFieldInt(JTextField textField,  int geld) {
    if (!textField.getText().isEmpty()) {
        try {
            if(Integer.parseInt(textField.getText())>0 && Integer.parseInt(textField.getText())<geld ) {
            return true;    
            }
        } catch (NumberFormatException e) {
            return false;
        }
    }
    return false;
}

now I disagree with this solution, because your code shouldn't depend on an error, or am I getting this wrong? so i sat down and wrote this:

    public static boolean checkInput(JTextField textField, int spielerGeld, String eingabe) {

    boolean matched = false;

    switch (eingabe) {

    case "num":
        if (!textField.getText().isEmpty() && textField.getText().matches("^[0-9]*$")) {

            int geldinput = Integer.parseInt(textField.getText());

            if (geldinput > 0 && geldinput < spielerGeld) {
                matched = true;
            }
        }
        break;

    case "string":
        if (!textField.getText().isEmpty() && textField.getText().matches("^[a-zA-Z]*$")) {
            matched = true;
        }
        break;

    default:
        break;
    }
    return matched;
}

Keep in mind, we yet dont have any textfields we have to check, but I just implemented it to get a grasp of how you could do multiple checks within one method.

So now my question is, what code is "better"? and what could we/I do better?

Thanks in advance!

EDIT1: So as some already have mentioned, you say my method is not build up after the Single responsibility principle. But if split up into 'checkInputIsnumber' and checkInputIsString' would the first solution(my friend), still be the "better" one?

EDIT2: Better is defined as in, the method should be of low cyclomatic complexity, easy readability and be easy to maintain in the long run.

The first approach is much better than the second one.

  1. Single responsibility: You should avoid creating methods that do more than one thing.
  2. Open–closed principle: Your 'validation' is not extensible. Try creating a Validator interface and then an implementation per validation type.
  3. Switch statements increase cyclomatic complexity and make testing harder.

Also, don't use textField.getText() everywhere, it's quite possible that it will change between calls. Assign it to a local variable or even better use a String as your argument and not JText . As Fildor pointed out you correctly avoid using exceptions for flow control and it is indeed better to have a single return point. Having said that, for simple cases when you just parse/check and return, it is acceptable.

You should put every check in a single function. After a while your "all in one function" will be unreadable an unmaintainable. Also it easier to change the checks if they are in single functions. Using try/catch for control flow is no good idea. It is expensive at runtime. It is not a good style and most developers won't expect control flow in a catch block.Excpetions are for exceptional situations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM