problems/risks due to programming language

Martin Weitzel martin at mwtech.UUCP
Sat Feb 24 12:33:36 AEST 1990


In article <8133 at hubcap.clemson.edu> billwolf%hazel.cs.clemson.edu at hubcap.clemson.edu writes:
[some lines deleted]

>   C's switch statement is badly designed, so badly designed that it is 
>   common practice to use break statements by the dozen in order to get 
>   it to behave reasonably.  A more sensible design would give the switch 
>   the semantics of the Ada case statement, thereby saving countless lines 
>   of code through the elimination of all those "break" statements. 

Though C has a badly designed switch statement (I don't doubt) it has
proven to be a useful language and seems to be in more widespread use
than 'better' ones. What I am missing in this discussion is to see:

	#define when break;case

so that you can write:

	switch(x) {
	default:
	when 1: /*stuff*/
	when 2: /*more stuff*/
	}

(O.K., not quite the ADA syntax, but it should work ...)

BTW: I would *not* like to go into a new round of the discussion
about using the preprocessor to define new "keywords". Everyone
who remembers my articles from the last war on this topic knows,
that I *don't* like to have new keywords. I have made the above
proposual only to heat this debate about good or bad design of
the switch statement :-)

Finally I would like to add another experience with switch:
Some day, I had written the following piece of code:

	enum sound { DING, DONG, DELL } x;
	....
	enum sound getsound()
	{
		....
	}
and later
	.....
	switch (x = getsound()) {
	DING:	/*stuff*/
		break;	<----- No, *this* was not missing
	DONG:	/*other stuff*/
		break;
	DELL:	/*still other stuff*
		break;
	}

Do you see, what is wrong with that?

I did not write "case DING:" aso., so DING: was considered to be a
goto-label (and as such had a different name space as enum names and
the program compiled without errors).

The problem was hard to find because this particular compiler
figured out, that the code within the case statement would never
be excuted (there were no case-label in it!) and optimized the
whole(!) statement away. Of course, this was a little too much
optimization, because the call to getsound() was also optimized
away (buggy compiler). Clearly, it was not at all obvious, why
my program never called getsound(): I did put a printf() right
before and after the switch, which both were executed, but
getsound() never was.

(Today I use #define rather that enum for such things, so that
this problem will not any more occur :-))
-- 
Martin Weitzel, email: martin at mwtech.UUCP, voice: 49-(0)6151-6 56 83



More information about the Comp.lang.c mailing list