Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!mit-eddie!uw-beaver!tektronix!teklds!zeus!tekla!dant From: dant@tekla.TEK.COM (Dan Tilque;1893;92-789;LP=A;60HC) Newsgroups: comp.lang.c Subject: Case fall throughs Message-ID: <2201@zeus.TEK.COM> Date: Thu, 13-Aug-87 21:58:47 EDT Article-I.D.: zeus.2201 Posted: Thu Aug 13 21:58:47 1987 Date-Received: Sat, 15-Aug-87 11:39:38 EDT Sender: news@zeus.TEK.COM Reply-To: dant@tekla (Dan Tilque) Organization: First National Security Trust Bar and Grill Lines: 67 I (Dan Tilque) say that it's bad programming to have this kind of thing: > > case 'a': > a = b + count; > case 'b': > count += 2; > > Lint should definitely give warning since it's almost always the case that > what's desired is a break before case 'b': I expected a number of people to respond saying "I often write these kind of fall throughs". So far only one person has done so. I admire everyone else's restraint (or else because of net lag I haven't seen the articles yet). You might notice that, unlike Karl Heuer, I do not advocate removing this feature completely from the language and substituting goto case labels instead. The reason is that a fall through is the fastest possible transfer of control and there are some time critical routines which use fall through for that very reason. However, in the common ordinary program, I think it's cleaner to do something like this: case 'a': a = b + count; common_statements(); break; case 'b': common_statements(); break; In those cases which have to have fall throughs, the /*FALLTHROUGH*/ comment to lint (as suggested by Karl Heuer) would also be a good thing to put in even if lint didn't check for it. It tells any maintenance programmer that the break was deliberately left out and is not the cause of the hard to track down bug. ) = Karl Heuer: )In article <2182@zeus.TEK.COM> dant@tekla.UUCP (Dan Tilque) writes: )>[If switch statements had automatic break instead of fall-through, the )>current idiom "case CR: case LF: ..." wouldn't work.] One possibility is )>separating them with commas: "case CR, LF, TAB, ' ':" which changes the )>meaning of the comma operator. Probably not a good idea. ) )The comma operator is neither necessary nor useful in a constant expression, )and is explicitly disallowed by ANSI. I checked in K&R and it's not allowed there either (by omission rather than explicitely). ] = David Collier-Brown. ] ... ] switch (foo) { ] case 'f' | 'e': -- one possible syntax... ] /* whatever */ ] } I thought of using | also. Unfortunately, | is already legal here and has a different meaning. Looks like a comma is the better notation. Actually, I like things the way they are except to have all lints check for fall through. --- Dan Tilque dant@tekla.tek.com or dant@tekla.UUCP