The D Programming Language: cases

Gregg Wonderly gregg at a.cs.okstate.edu
Tue Mar 8 12:44:59 AEST 1988


>From article <25200 at cca.CCA.COM>, by g-rh at cca.CCA.COM (Richard Harter):
> In article <2403 at umd5.umd.edu> chris at trantor.umd.edu (Chris Torek) writes:
>>-In article <24996 at cca.CCA.COM> g-rh at cca.CCA.COM (Richard Harter) writes:
>>-}As a side point, I like the suggestion that someone made that there be
>>-}a fallthrough statement rather than automatic fallthrough.
> 
>>While this would work, if we assume D has aggregate constructors,
>>there is a handsomer way:
> 
>>	switch (e) {
>>	case [1, 2, 3, 5, 7, 13, 19, 23]: ...
>>	}
>>	/* syntax above is only for demonstration */
> 
> Well, yes, one ought to be able to do that.  However it is isn't quite
> as strong as fallthrough, where one can say
> 
> 	switch (e) {
> 	  case foo:
> 	    some code;
> 	    fallthrough;
> 	  case baz:
> 	    some more code;
> 	  }
> 
> 
> In C as it stands now you can do this -- indeed, the complaint is that
> one can do this unintentionally.  If one adds aggregate constructors and
> takes away automatic fallthrough, it seems to me that you weaken the
> language.   No doubt there are purists that say you shouldn't do the
> sort of thing given above.  I wouldn't go that far, but I would agree
> that one should be able to use aggregate constructors when cases actually
> share code.

This is just one of the many things that people gripe and complain about
when writing C.  In my eyes it comes down to poor/bad (which is your favorite?)
program writing practices.  You can eliminate the majority of the problems
associated with `break' in a switch by prototyping the entire switch before
filling in the code (you mean you are coding off the top of your head :-).
e.g.

	switch (val) {
		case 'a':
		case 'b':
			break;
		case 'c':
			break;
		case 'f':
		case 'h':
		case 'v':
			break;
		default:
			wrong_again_honey();
	}

Then you can add the code for each case by opening a line above the `break'
statements.  If you know that you need a switch, you should know what the
structure of it will be initially (prior to adding that other feature), so
just type it (you know, like stubbing out a program).  While I am at it,
one of my biggest gripes is that too many people exploit C's idea of a 
statement so that they can write

	if (some_expression)
		for (init; continuation_condition; repeat_code)
			switch (val) {
				forty jillion case's
			}

without placing braces around the `switch' and the `for'.  I guess these
people either don't use VI, or haven't got a bracket matcher in their
favorite editor.  This kind of code is terribly difficult to get through.
I prefer...

	if (some_expression) {
		for (init; continuation_condition; repeat_code) {
			switch (val) {
				forty jillion case's
			}
		}
	}

Then there are those people who lost sight of their space bar and insist
on typing things like,

	for(a=4,b=5,s=&c;((a<b&&d>>4)+(int)s+1)!=c<d;++a,--d,c=a)

or some such arcane expression without a single space to try and make the
code even slightly more readable.

Well, I will stop here, as programming style is kind of like editors and
religion.  Everybody has there own, and nobody can make them change... sigh!!!

-----
Gregg Wonderly
Department of Computing and Information Sciences
Oklahoma State University

UUCP:      {cbosgd, ihnp4, rutgers}!okstate!gregg
Internet:  gregg at A.CS.OKSTATE.EDU



More information about the Comp.lang.c mailing list