Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site utcsri.UUCP Path: utzoo!utcsri!greg From: greg@utcsri.UUCP (Gregory Smith) Newsgroups: net.lang.c Subject: Re: An amusing piece of code Message-ID: <2502@utcsri.UUCP> Date: Tue, 8-Apr-86 14:20:38 EST Article-I.D.: utcsri.2502 Posted: Tue Apr 8 14:20:38 1986 Date-Received: Tue, 8-Apr-86 14:45:02 EST References: <1370@ism780c.UUCP> Reply-To: greg@utcsri.UUCP (Gregory Smith) Organization: CSRI, University of Toronto Lines: 93 Keywords: case goto if Summary: I'll say... In article <1370@ism780c.UUCP> tim@ism780c.UUCP (Tim Smith) writes: >Here is an amusing piece of code that someone who wants to remain >annonymous invented. > >The situation is that we have something that, if written with if-then-else >would be > > if ( thing == A ) > A-code; > else > if ( thing == B || thing == C || thing == D ) { > switch ( thing ) { > case B: B-code; break; > case C: C-code; break; > case D: D-code; break; > } > BCD-common-code; > } else > if ( thing == E ) > E-code; > >A, B, C, D, and E are constant expressions, so this is not elegant. >We would like to use a switch for everything. Here is a solution: > > 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. > Many points for creative use of if-statements... do you know about the obfuscated C contest..? :-) My $0.02: switch ( thing ) { case A: A-code; break; case B: B-code; BCD_common(); break; case C: C-code; BCD_common(); break; case D: D-code; BCD_common(); break; case E: E-code; } ... BCD_common(){ BCD-common-code; } The function call is very cheap if it has no parameters. I have done the above many times. If you must do nasty things, at least be honest about it: switch ( thing ) { case A: A-code; break; case B: B-code; goto BCD; case C: C-code; goto BCD; case D: D-code; BCD: BCD-common-code; break; case E: E-code; } Despite the fact that this contains 2 more ( pardon my language ) `goto's I feel it is much clearer than the example using castrated `if's, which after all are just `goto's in disguise. At least *real* `goto's are easily recognizable as such. There is another thing you can do, but only if the switch is to be followed by a return: switch ( thing ) { case A: A-code; return; /* skip BCD-common */ case B: B-code; break; /* do BCD-common */ case C: C-code; break; case D: D-code; break; case E: E-code; return; } BCD-common-code; } /* end of function */ This may look a little iffy but I would rank it above the goto solution in clarity. I think it is less clear than the function call, and probably less maintainable, but it would be faster. I have also done this one many times. -- "If you aren't making any mistakes, you aren't doing anything". ---------------------------------------------------------------------- Greg Smith University of Toronto UUCP: ..utzoo!utcsri!greg