Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice Return Value vs Exception vs Enum [closed]

I am trying to find out the advantages and disadvantages for a method with multiple result values.

For example I'm using a login-method. If the login was successful, it will pass, otherwise I need to know why it failed.

1. Return true or false (Not enough information)

bool Login(string user, string password);

2. Return true, if it was successful, otherwise throw an exception

public class UnknownUserException : Exception { }
public class WrongPasswordException : Exception { }
bool Login(string user, string password);

3. Return nothing. Throw an exception, if it wasn't successful

public class UnknownUserException : Exception { }
public class WrongPasswordException : Exception { }
void Login(string user, string password);

4. Return an enum value

enum LoginResult
{
    Successful
    UnknownUser,
    WrongPassword
}
LoginResult Login(string user, string password);

"Login" is just one example case. I would like to know what the advantages and disadvantages of the different implementations is, and for which cases they are more or less appropriate.

like image 570
Jan Avatar asked Apr 02 '14 09:04

Jan


People also ask

Why is throwing an exception better than returning an error value?

Return codes are more brittle The error is ignored when "returned", and will possibly explode later (i.e. a NULL pointer). The same problem won't happen with exception. The error won't be ignored.

Why is enum not good?

It's hard to voice what your actual needs are to a potential partner for fear that they may write you off. Transparency requires over communication. If your communication skills are already not that great, ENM is going to be more of a headache than an opportunity to be your most authentic self.

Does enum valueOf throw exception?

The valueOf() enum method converts a specified string to an enum constant value. An exception is thrown if the input string doesn't match an enum value.

Why enums are better than constants?

The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).


1 Answers

Definitely not exceptions. A failed login is hardly an "exceptional" case and it just a normal course of logic for the application. If you use an exception then you'll always have to wrap logging in with an exception handler for no other reason than to handle the case of a failed login. That seems like the very definition of using exceptions for logic flow, which isn't right.

If you need to return specific information (which isn't always necessary from a login function, but might be in your case), #4 seems reasonable. You could take it a step further and make it an object:

public class LoginResult
{
    // an enum for the status
    // a string for a more specific message
    // a valid user object on successful login
    // etc.
}

Or, depending on the logic for it, an immutable struct instead of a class. (Make sure the struct is immutable, mutable structs are just asking for trouble.) The point being that you can apply all sorts of logic and functionality on the result object itself, which seems to be the direction you're heading.

like image 61
David Avatar answered Sep 17 '22 16:09

David