An amusing piece of code

Marcus Hall thoth at tellab1.UUCP
Wed Apr 9 02:16:02 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.
...
>	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



More information about the Comp.lang.c mailing list