Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/5/84; site tellab1.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxn!ihnp4!tellab1!thoth From: thoth@tellab1.UUCP (Marcus Hall) Newsgroups: net.lang.c Subject: Re: An amusing piece of code Message-ID: <885@tellab1.UUCP> Date: Tue, 8-Apr-86 11:16:02 EST Article-I.D.: tellab1.885 Posted: Tue Apr 8 11:16:02 1986 Date-Received: Mon, 14-Apr-86 06:16:02 EST References: <1370@ism780c.UUCP> Reply-To: thoth@tellab2.UUCP (Marcus Hall) Organization: Tellabs, Inc., Lisle, IL Lines: 47 Summary: 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. ... > 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. > >Tim Smith sdcrdcf!ism780c!tim || ima!ism780!tim || ihnp4!cithep!tim I always do this kind of things with gotos. Controlled use of gotos don't present serious readability problems, and in comparison to the code above it seems much clearer to me. switch ( thing ) { case A: A-code; break; case B: B-code; goto BCDcommon; case C: C-code; goto BCDcommon; case D: D-code; goto BCDcommon; /* This goto is optional... */ BCDcommon: BCD-common-code; break; case E: E-code; } Note that if BCD-common-code isn't very complex, most optimizers will combine the common tail code if it's given this input: 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; } marcus hall ..!ihnp4!tellab1!thoth