3 ms·
I'd like to see an example. Are you familiar with Duff's Device btw?
by kristopolous 15y ago
I'd like to see an example. Are you familiar with Duff's Device btw?
- Symmetry 15y agoAn example then: switch( machineState ){ case ALPHA: ... break; case BETA: ... goto location; case GAMMA: location: ... } No, I wasn't familiar with Duff's device until just now. I'd say that you only need to be explicit about pointing out fallthroughs if the blocks in the switch statements are more than a couple of lines long.
- tspiteri 15y agoI find that use of goto quite confusing, and would prefer a comment in such a case: switch( machineState ){ case ALPHA: ... break; case BETA: ... /* fallthrough */ case GAMMA: ... }
- JadeNB 15y agoThat's particular to situations that can be solved by fallthrough. What about switch ( machineState ) { case ALPHA: ... alpha prep ... goto gamma case BETA: ... beta prep ... goto gamma case GAMMA: gamma: ... } ? (EDIT: Not to say that you shouldn't still comment, of course.)
- nitrogen 15y agoIn this case it might be clearer to just run the switch again after ALPHA and BETA with GAMMA as the machineState, and if necessary, have logic in GAMMA for switching machineState back to ALPHA or BETA. It also might be clearer to move GAMMA outside the switch block entirely: int shouldGamma = 0; switch ( machineState ) { case ALPHA: ... alpha prep ... shouldGamma = 1; break; case BETA: ... beta prep ... shouldGamma = 1; break; case GAMMA: shouldGamma = 1; break; } if(shouldGamma) { // GAMMA stuff here } The shouldGamma flag would be unnecessary if all possible values for machineState should still run the code in GAMMA.