Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning from a method with implicit or explicit "else" or with a single "return" statement?

Some people consider multiple return statements as bad programming style. While this is true for larger methods, I'm not sure if it is acceptable for short ones. But there is another question: Should else explicitly be written, if there is a return statement in the previous if?

Implicit else:

private String resolveViewName(Viewable viewable) {
    if(viewable.isTemplateNameAbsolute())
        return viewable.getTemplateName();
    return uriInfo.getMatchedResources().get(0).getClass().toString();
}

Explicit else:

private String resolveViewName(Viewable viewable) {
    if(viewable.isTemplateNameAbsolute())
        return viewable.getTemplateName();
    else
        return uriInfo.getMatchedResources().get(0).getClass().toString();
}

Technically else is not necessary here, but it make the sense more obvious.

And perhaps the cleanest approach with a single return:

private String resolveViewName(Viewable viewable) {
    String templateName;
    if(viewable.isTemplateNameAbsolute())
        templateName = viewable.getTemplateName();
    else
        templateName = uriInfo.getMatchedResources().get(0).getClass().toString();
    return templateName;
}

Which one would you prefer? Other suggestions?

like image 806
deamon Avatar asked Aug 06 '10 11:08

deamon


People also ask

What is implicit else?

The best part of the article is the author's explanation of what the implicit else is: Using the implicit else with if statements takes advantage of the fact that return statements end function execution. In certain cases, this lets us eliminate else statements, resulting in shorter code. The author is good so far.

Do All methods need a return statement?

Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so.

How do you return an if statement in C#?

C# if (if-then) StatementThe boolean-expression will return either true or false . If the boolean-expression returns true , the statements inside the body of if ( inside {...} ) will be executed. If the boolean-expression returns false , the statements inside the body of if will be ignored.

What is the purpose of a return statement in a function?

A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.


2 Answers

Other obvious suggestion: use the conditional operator.

private String resolveViewName(Viewable viewable) {
    return viewable.isTemplateNameAbsolute()
        ? viewable.getTemplateName()
        : uriInfo.getMatchedResources().get(0).getClass().toString();
}

For cases where this isn't viable, I'm almost certainly inconsistent. I wouldn't worry too much about it, to be honest - it's not the kind of thing where the readability is like to be significantly affected either way, and it's unlikely to introduce bugs.

(On the other hand, I would suggest using braces for all if blocks, even single statement ones.)

like image 182
Jon Skeet Avatar answered Sep 28 '22 02:09

Jon Skeet


i prefer the cleanest approach with single return.To me code is readable, maintainable and not confusing.Tomorrow if you need to add some lines to the if or else block it is easy.

1.) code should never be clever.

like image 25
Dead Programmer Avatar answered Sep 28 '22 04:09

Dead Programmer