9

Possible Duplicate:
Should a function have only one return statement?

My teacher took points off on some java code I wrote (I'm still getting an A, this is just the first non 100% grade I've gotten in the java course) I'm not going to argue with the teacher on the point, but I would like to get some advice from actual programmers. Here is the offending piece of code:

private char byte2suit(byte val) {
    switch(val) {
        case 0: return 's';
        case 1: return 'c';
        case 2: return 'h';
        case 3: return 'd';
        }
    //fallback value
    return 'h';
    }

In my opinion this is much clearer that initalizing a return value, assigning in the case (and adding break; every line) then returning the value. Of course, in code other people see, my opinion isn't all powerful, so I would like to know what you guys think about multiple return statements in java (or C/C++ for that matter), if you do use multiple return statements do you use it more than the "if (this) return a; else return b;" statement? If you don't use multiple return statements, can you give a really convincing reason ( unreadable code is not a reason if the above code IS readable and is the maximum extent of my multiple return coding practice )

5
  • 9
    I'd probably write it just like that, with one minor difference: I'd use default: for the fallback. I see no reason to sacrifice something as readable as that at the altar of the Single Return gods. Oct 17, 2012 at 14:15
  • A better design would be to use an enum Suit Oct 17, 2012 at 14:17
  • Typical code standards I've seen (at the jobs I've worked) state it's OK to use more then 1 return if the readability is enhanced. Given the simplicity of the function, I'd say it was fine, as long as you changed your fall back to a default, as it's written right now you could have other code after the switch but before the return; in which case I'd want to see just 1 return.
    – Mike
    Oct 17, 2012 at 14:23
  • Your teacher must be a refugee from the 1960s when Wirth and Djkstra decreed that a method should have one entry and one exit, as part of the Structured Programming movement which killed the GOTO statement. Tell him that most compilers (except javap) will translate that kind of code into that canonical form anyway, so it must be logically equivalent. In practice, out there in the 'real world', multiple return statements are used all the time. He has no business marking you down for this.
    – user207421
    Oct 17, 2012 at 23:22
  • @EJP after posting this question (and seeing that its almost an exact duplicate of a very popular question) I did some research and saw how overwhelmingly recommended multiple return statements are in the 3 cases where it's much more code to do it the "Single Return" way. If only I had some researched/documented "citeable" work that proves this, I could try to convince my teacher, otherwise I'll get the "internet isn't always correct" response
    – Seth
    Oct 19, 2012 at 15:46

1 Answer 1

14

Some academics believe that there should only be one return statement at the end of the function. They believe that multiple return statement make the code impure in some way.

However most language designers and professonal programmers disagree with this purist view. Multiple return statements can be confusing if your method is long and complicated (but if this is the case, you have bigger problems then multiple return statements). But often multiple return statements can make the code more readable.

Your code is fine, apart from one change I'd make: use default.

switch(val) {
    case 0: return 's';
    case 1: return 'c';
    case 2: return 'h';
    case 3: return 'd';
    default: return 'h';
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.