Methods that return meaningful return values

I work in C #, so I placed it under C #, although this may be a question that can be answered in any programming language.

Sometimes I create a method that will do something like registering a user on a website. Often I return a boolean from a method, but this often causes problems because the boolean return does not convey any context. If an error occurs during user registration, I do not know what caused the error.

Here is an example of a method that I am currently using, but would like to change it so that it returns more than just 0 or 1.

public bool LoginToTwitter(String username, String password) { // Some code to log the user in } 

The above method can only return True or False. This works well because I can just call the following:

 if(http.LoginToTwitter(username, password)) { // Logged in } else { // Not Logged in } 

But the problem is that I cannot understand why the user was not registered. There may be a number of reasons: an incorrect combination of username and password, a suspended account, an account requires the attention of users, etc. But using the following method and logic, it is impossible to find out.

What alternative approaches do I have?

+8
c #
source share
6 answers

You can create and return an enum with the expected LoginResults parameters.

 public enum LoginResult { Success, AccountSuspended, WrongUsername, WrongPassword, } 

Then return using the enum type in your method:

 public LoginResult LoginToTwitter(String username, String password) { // Some code to log the user in } 
+8
source share

You can either throw an exception with the appropriate message related (and have a call method associated with the exception), or return an enum function with different states (e.g. LoginState.Success , LoginState.IncorrectPassword , etc.).

If you are using exceptions, it is probably best for your function to return nothing ( public void LoginToTwitter ), but if you are using enum , make sure that your enum is set for the return type ( public LoginState LoginToTwitter ).

+8
source share

There are two standard methods. If you are only interested in the result, but not any metadata, return some listing. Set the available values ​​to Success , Suspended , etc. (All your possible results)

If you need more information, you can always use exceptions. Basically follow the idea of β€‹β€‹β€œtell, don’t ask” and write your function in such a way that it returns the required values ​​(for example, a user ID or maybe nothing if you have some kind of global login state) and throw an exception with a detailed description of the failure otherwise. As for the hierarchy itself, most likely you should implement a LoginException with some more specific subclasses and catch only those. (this makes it easier to check all relevant exceptions, and all unknowns are passed to higher levels)

+6
source share

Both return an enumeration or throw an exception, as suggested in the other answers, are reasonable. But I prefer to throw an exception. It sounds crazy, but it allows your subscriber to decide whether to use error checking or exception handling. And, unlike enumeration, exceptions are hierarchical, therefore, to simplify the processing of entire categories of failures and transfer arbitrary additional data.

I think Says had a similar idea, but he deleted his answer and never explained it.

+3
source share

In keeping with the pure trend of the code, you should provide a meaningful name for your method, which indicates the intention.

To find out what happened if the registration operation cannot be completed, you can enter Exceptions into the stream and process it in the context of the caller.

see http://msdn.microsoft.com/en-us/library/ms173160 (v = vs .80) .aspx

0
source share

You can use the idiom that is often used in c. The result of the assignment expression is the expression itself - this means that you can capture the resulting code while evaluating whether it is a specific value:

 if ((status = DoSomething()) == AnErrorEnum.NotAnError) {//success handler } else {//failure handler } 

I was asked to provide a link to an MSDN article - here is the old version of the specification: http://msdn.microsoft.com/en-us/library/aa691315(v=vs.71).aspx

"The result of a simple assignment expression is the value assigned to the left operand. The result is of the same type as the left operand and is always classified as a value."

0
source share

All Articles