home *** CD-ROM | disk | FTP | other *** search
- ; From: Dat Thuc Nguyen
- ; Newsgroups: comp.protocols.kermit.misc
- ; Subject: SWITCH statement considered harmful
- ; Date: Wed, 31 Mar 1999 15:09:20 GMT
- ; URL: http://www.smalltickle.com
- ;
- ; In languages such as Kermit, Tcl, C, etc, the SWITCH statement could be
- ; the source of subtle bugs caused by the intentional or unintentional
- ; omission of the break statement.
- ;
- ; 'Using C-Kermit' 2nd edition, page 385, displays a classical usage of the
- ; switch statement with the intentional omission of the break statement to
- ; achieve a "fall-through".
- ;
- ; This programming method is error prone and should be avoided.
- ;
- ; Consider the following alternative which is more defensive and maintenance
- ; friendly, since:
- ;
- ; 1. New cases can be added easily.
- ; 2. No break statement is needed to terminate a case.
- ; 3. Fall-through is explicit through the specification of the targeted case,
- ; which can be any of the possible cases, even backward, skip intermittent
- ; cases, whatever.
- ; 4. Each case label is expressive.
- ; 5. Default statement is replaced with the check on fail.
- ; 6. No subtle bugs caused by the implementation of the switch statemnet.
- ;
- ; Kermit scripting language is not C. When programming in Kermit, use Kermit
- ; idioms, don't mimic C.
- ;
- ; This is object-oriented programming in the small, the day_ is generic, when
- ; appended with a case specific value, it yields the name of a predefined
- ; macro, and get executed. This flexibility is very effective.
- ;
- define WEEKDAY {
-
- local day_0 day_1 day_2 day_3 day_4 day_5 day_6
-
- define day_0 { echo Sonntag }
- define day_1 { echo Montag und uebermorgen ist, day_3 }
- define day_2 { echo Dienstag und zunaechst kommt ..., day_3 }
- define day_3 { echo Mittwoch }
- define day_4 { echo Donnerstag }
- define day_5 { echo Freitag und gestern war, day_4}
- define day_6 { echo Samstag und da ist schon wieder der, day_0}
-
- day_\v(nday)
- if fail echo Invalid day - \v(nday)
- }
-