An amusing piece of code

Geoff Kuenning geoff at desint.UUCP
Sun Apr 6 07:00:12 AEST 1986


In article <1370 at ism780c.UUCP> tim at ism780c.UUCP (Tim Smith) writes:

> 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;
> 
[followed by code that makes me puke]

(1)	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;
	    }

(2)	switch (thing)
	    {
	    case A:
		A-code;
		break;
	    case B:
	    case C:
	    case D:
		switch (thing)
		    {
		    case B:
			B-code;
			break;
		    case C:
			C-code;
			break;
		    case D:
			C-code;
			break;
		    }
		BCD-common-code ();
		break;
	    case E:
		E-code;
		break;
	    }

(3)	switch (thing)
	    {
	    case A:
		A-code;
		break;
	    case B:
		B-code;
		goto common;
	    case C:
		C-code;
		goto common;
	    case D:
		D-code;
common:
		BCD-common-code;
		break;
	    case E:
		E-code;
		break;
	    }

Moral:  don't be so rabid about avoiding goto's that you wind up with
something even uglier.  It makes you look foolish.
-- 

	Geoff Kuenning
	{hplabs,ihnp4}!trwrb!desint!geoff



More information about the Comp.lang.c mailing list