Best way to return status flag and message from a method in Java

后端 未结 17 1189
清酒与你
清酒与你 2020-12-25 12:55

I have a deceptively simple scenario, and I want a simple solution, but it\'s not obvious which is \"most correct\" or \"most Java\".

Let\'s say I have a small authe

17条回答
  •  春和景丽
    2020-12-25 13:28

    I would most probably go for something like :

    
    class SomeClass {
    public int authenticate (Client client) {
    //returns 0 if success otherwise one value per possible failure
    }
    public String getAuthenticationResultMessage (int authenticateResult) {}
    //returns message associated to authenticateResult
    }
    

    With this "design", you can ask for a message only when authentication fails (which I hope is the scenario that occurs 99,99% of time ;))

    It may also be of good practice to delegate message resolution to another Class. But it depends of your application needs (mostly, does it need i18n ?)

提交回复
热议问题