Good code for sometimes shared case stuff
Chandros
chandros at topaz.RUTGERS.EDU
Wed Apr 9 05:18:59 AEST 1986
/* you have found a food */
I just started reading this group regularly, sof please forgive me if I'm
repeating something that was already said.
David Linn suggests the following as a solution to the "shared code in a
switch some of the time" problem. (I removed the goto version
for the benefit of the younger readers. Shame, shame, shame on you.
Evil gotos, gasp, and in C no less, what will the world think of next. )
>In posting<1370 at ism780c.UUCP>, Tim Smith asks about a switch with common
>code for some cases. I have seen two solutions.
>1) put the common code in a subroutine
> switch(thing) {
> case A: A-code; break;
> case B: B-code; BCD-common-code(); break;
> case C: C-code; BCD-common-code(); break;
> case D: D-code; BCD-common-code(); break;
> case E: E-code; break;
> }
> [evil goto version was here]
You forgot your structured programming rules. What about a (here it comes
folks) a BOOLEAN variable called do_bcd_stuff?? Also, you could
set do_bcd_stuff to be true in the declaration, and only change it in the
case E to be false. Anyway, that's unimportant. I don't ken enough
C to be sure, but it seems to me that the do_bcd_stuff declaration could
go inside the switch along with the if statement. But I'm not sure, and
the following will work for sure. (I hope, otherwise, BOY did I just made a
fool out of myself............)
ex:
int do_bcd_stuff;
switch(thing) {
case A: A-code; do_bcd_stuff=TRUE; break;
case B: B-code; do_bcd_stuff=TRUE; break;
case C: C-code; do_bcd_stuff=TRUE; break;
case D: D-code; do_bcd_stuff=TRUE; break;
case E: A-code; do_bcd_stuff=FALSE; break;
}
if (do_bcd_stuff)
bcd stuff goes here;
This is much better than using those evil gotos, or paying the cost of
a subroutine call.
Jonathan A. Chandross
allegra!topaz!chandros (that's 1 s kids....)
"It is the little, pathetic attempts at Quality that kill...." -- R. M. Pirsig
[eof]
More information about the Comp.lang.c
mailing list