An amusing piece of code
David Herron, NPR Lover
david at ukma.UUCP
Mon Apr 7 04:05:10 AEST 1986
In article <1370 at ism780c.UUCP> tim at ism780c.UUCP (Tim Smith) writes:
>Here is an amusing piece of code that someone who wants to remain
>annonymous invented.
Good idea :-).
> ...
> switch ( thing ) {
>case A: A-code; break;
>case B: B-code; if ( 0 ) {
>case C: C-code; if ( 0 ) {
>case D: D-code; }}
> BCD-common-code; break;
>case E: E-code;
> }
>
>Noone here has been able to come up with a reasonable style for this. The
>example above is not to bad, but if B-code, C-code, etc, are complicated,
>then it starts to get ugly.
I don't like this either... it's not very straightforward, but ALSO,
there's a potential problem I think. "case D:" exits TWO levels
of context and never entered ANY, case C: exits TWO but only entered ONE.
(Is this ever a problem? I can see that it "might" be, but I'm also
sure that the PCC will run it fine.)
What about:
switch(thing) {
case A: A-code; break;
case B: B-code; goto BCD-common;
case C: C-code; goto BCD-common;
case D: D-code;
BCD-common: BCD-code; break;
case E: E-code;
}
I know, EVIL NASTY goto statements. But this is a limited local use
of goto's and surely that should be allowable except for the most
rabid of structurists. Especially if there is commentation pointing
the reader toward BCD-common:.
--
David Herron, cbosgd!ukma!david, david at UKMA.BITNET, david at uky.csnet
More information about the Comp.lang.c
mailing list