views:

718

answers:

9
Switch(some case) {
    case 1:
           // compute something ...
           return something;
           break;
    case 2:
           // compute something ...
           return something;
           break;

/* some more cases ... */

    case X:
           // compute something ...
           return something;
           break;
    default:
           // do something
           return something;
           break;
}

In my opinion:

Assuming this switch statement is justifiable, the return and break just doesnt look right or feel right.

The break is obviously redundant, but is omission poor style (or is this poor style to begin with?) ?


I personally dont do this, but there is some of this in the codebase at work. And no, im not going to be self-righteous and correct the codebase.

+5  A: 

In my opinion, I would omit the 'break' keyword. I personally think it helps remind people that 'Execution has ended! Nothing more to see here!'.

MPritch
+5  A: 

I would make a small change:

switch(some case) {
    case 1:
           // compute something ...
           break;
    case 2:
           // compute something ...
           break;
/* some more cases ... */
    case X:
           // compute something ...
           break;
    default:
           // do something
           break;
}
return something;
Elie
I would advise against this as it means you need to read the entire case statement to check to see if anything else happens to the 'something' variable. If you just return, it's immediately clear that, no, nothing does :-)
MPritch
Since you have a break statement at the end of each case, nothing else could change it. Having read some of the other answers, though, I would agree that the alternative would be to replace each break with the return statement.
Elie
Elie - I see where you are coming from, I would consider this the text-book answer
I guess the question is whether an additional temporary adds more complexity than multiple returns. Personally, I'm skeptical of the "one return per function" guideline.
Tmdean
i personally prefer this form over the one with one return per case. I consider this form easier to read
Nuno Furtado
+19  A: 

No, omission is not poor style - inclusion is poor style. Those are unreachable statements. Get rid of them.

I like the fact that the cases return directly instead of setting a local variable and then returning just at the bottom - it means that it's incredibly clear when you're reading the code that it does just need to return, and that's all.

Side-note in terms of switching in the first place:

As for whether using a switch statement is the right thing to do here, it really depends on other things. Would it make sense to use a polymorphic type instead? If you're in Java, could you use a smart enum? (You can mimic these in C#, but there isn't as much support.)

I'd say this should at least prompt considering different designs - but it may well be the simplest way to do what you want.

Jon Skeet
Putting a return statement in every case is fine until the day you decide to do one more thing to 'something' before returning. Say, for example, that you want to log the somethings. Now instead of one central place at the bottom of your routine, you've got to wade through and perhaps rethink all the cases. Like everybody else I don't always adhere to the "one point of return" guideline, but there are reasons for it and this many-case switch example begs for it.
I. J. Kennedy
So at that point you either extract it into a separate method, or you change it to use a local variable. There's no harm in leaving that until you actually need it though... and it's a mechanical refactoring, after all.
Jon Skeet
A: 

Code smell implies a design problem. This is just an issue of formatting. I think most people would agree that a version with breaks omitted is superior.

Tmdean
+10  A: 

The C# Compiler gives a warning if you do this saying that the break is unreachable code. So in my book it is bad form to have both return and break.

Martin Brown
great food for thought for people using interpreted languages
A: 

The break is redundant.

Some rule of thumb, It is always good to exit a function in one place only, But since this rule was made Try-Catch was invented (oop goto).

If you keep the same style all over your application, I guess you can live with the return in the switch.

Itay Moav
+1  A: 

I'm not sure how literal that code is intended to be so some of these observations may not be applicable...

"case 1" If you're truly hard-coding numers like this I think it is poor style and you should look into using an enumeration.

If you are simply returning something and there is no additional logic in a subset of the cases, you might consider putting the "somethings" in an array or dictionary and simply addressing them by their index rather than using a switch statement...

return somethings[index]

lJohnson
A: 

The code smell here is the hard coded values in the case statements.

As for the returns it is more a question of taste and good judgement. Sure if your case spans mutiple pages it is easier to read if the return is in the case, but then the problem is the big switch to begin with.

Personnaly I prefer the single return option because if ever you need to add some more processing then it is cheaper to refactor than visiting all the cases. Also, if given to someone else to refactor they will not have the temptation to copy paste the code in every case, least I would hope so ... if they do they better hope I am not the one doing their code review (for the copy paste obviously).

Newtopian
were you joking about the hard coded values? can you not see i was using pseudocode?
yes I can see you are using pseudo code, and I also see that the case values are not constants but numbers. All I am saying is that numbers as case values smell bad and usually indicate something that need attention.
Newtopian
the return... break; is not a code smell it is just a formatting issue, whichever way you slice it it will never cause error. Thus, I was saying, that IF there is a code smell here it sould have to be the numbers as for the rest it does not qualify as a code smell, IMHO that is.
Newtopian
A: 

Switch statements are themselves a code smell.

Along the lines of what l99057j said, if all you're doing is mapping inputs to constant values, this is a place for an appropriate lookup structure, such as an array/list for sequential inputs or a dictionary/map for a sparse one, not a switch statement. If you're calculating something, then the value would be a delegate that you call with the input.

Steven Sudit