It's valid and very useful in some cases.
Consider the following code:
switch(poll(fds, 1, 1000000)){
default:
// here goes the normal case : some events occured
break;
case 0:
// here goes the timeout case
break;
case -1:
// some error occurred, you have to check errno
}
The point is that the above code is more readable and efficient than cascaded if
. You could put default
at the end, but it is pointless as it will focus your attention on error cases instead of normal cases (which here is the default
case).
Actually, it's not such a good example, in poll
you know how many events may occur at most. My real point is that there are cases with a defined set of input values where there are 'exceptions' and normal cases. If it's better to put exceptions or normal cases at front is a matter of choice.
In software field I think of another very usual case: recursions with some terminal values. If you can express it using a switch, default
will be the usual value that contains recursive call and distinguished elements (individual cases) the terminal values. There is usually no need to focus on terminal values.
Another reason is that the order of the cases may change the compiled code behavior, and that matters for performances. Most compilers will generate compiled assembly code in the same order as the code appears in the switch. That makes the first case very different from the others: all cases except the first one will involve a jump and that will empty processor pipelines. You may understand it like branch predictor defaulting to running the first appearing case in the switch. If a case if much more common that the others then you have very good reasons to put it as the first case.
Reading comments it's the specific reason why the original poster asked that question after reading Intel compiler Branch Loop reorganisation about code optimisation.
Then it will become some arbitration between code readability and code performance. Probably better to put a comment to explain to future reader why a case appears first.
goto
(and therefore evil) about using this behaviour - I'd suggest avoiding it. – Ledbettergoto
isn't evil. Cargo cult followers are! You could not imagine to what extremes people can go to avoidgoto
because it is alledgedly so evil, making a real unreadable mess of their code. – Readilyevil
was a bad choice of words, maybeeasily abused
oreasily misunderstood
. There's no reason why avoiding goto's needs to lead to less readable code and generally, if you need goto's I'd suggest it's a sign that the code code be better structured in the first place. If you've got any concrete examples which disprove my point I'd be really interested to see them. I'm not bashing here, I've just had to fix some nasty bugs caused by goto's and similar language constructs in the past :-) – Ledbettergoto
mainly to simulate something like afinally
clause in functions, where ressources (files, memory) have to be released when stopping, and repeating for every error case a list offree
andclose
doesn't help for readability. There's though one use ofgoto
that I'd like to avoid but can't, is when I want to break out of a loop and I'm within aswitch
in that loop. – Readily