An amusing piece of code

Rick Richardson gemini at homxb.UUCP
Thu Apr 10 12:50:31 AEST 1986


The ongoing debate (?):

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

	- VERSUS -

> switch ( thing ) {
> case A:	   A-code; BCD_later_flag = FALSE; break
> case B:	   B-code; BCD_later_flag = TRUE; break
> case C:	   C-code; BCD_later_flag = TRUE; break
> case D:	   D-code; BCD_later_flag = TRUE; break
> case E:	   E-code; BCD_later_flag = FALSE; break
> default:   default-code; BCD_later_flag = FALSE;
> }
> if (BCD_later_flag) {
> 	whetever();
> }

Essentially, both pieces of code are equivalent in "confusion factor".
The goto version leaves the reader wondering "goto WHERE?",  the
BCD_later version leaves the reader wondering "BCD how much LATER?".

Six one way, half a dozen the other.  Me? I'll either put BCD in
a function, or, if I don't think I want to incur the function call
overhead, will use the goto version.  After all, if I didn't want
to do a function call then I'm probably after every last cycle I can
squeeze out of the code, in which case its cheaper to just jump to
where I want to go, rather than setting a flag.  Then again, there's
always MACRO's to make it look like the function call version without
the subroutine call overhead...  ...I'm easy, as long as it works.

Rick Richardson, PC Research, Inc. (201) 922-1134, (201) 834-1378 @ AT&T-CP
..!ihnp4!castor!{rer,pcrat!rer} <--Replies to here, not to homxb!gemini, please.



More information about the Comp.lang.c mailing list