home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / noswitch < prev    next >
Text File  |  2020-01-01  |  2KB  |  51 lines

  1. ; From: Dat Thuc Nguyen
  2. ; Newsgroups: comp.protocols.kermit.misc
  3. ; Subject: SWITCH statement considered harmful
  4. ; Date: Wed, 31 Mar 1999 15:09:20 GMT
  5. ; URL: http://www.smalltickle.com
  6. ;
  7. ; In languages such as Kermit, Tcl, C, etc, the SWITCH statement could be
  8. ; the source of subtle bugs caused by the intentional or unintentional
  9. ; omission of the break statement.
  10. ;
  11. ; 'Using C-Kermit' 2nd edition, page 385, displays a classical usage of the
  12. ; switch statement with the intentional omission of the break statement to
  13. ; achieve a "fall-through".
  14. ;
  15. ; This programming method is error prone and should be avoided.
  16. ;
  17. ; Consider the following alternative which is more defensive and maintenance
  18. ; friendly, since:
  19. ;
  20. ; 1. New cases can be added easily.
  21. ; 2. No break statement is needed to terminate a case.
  22. ; 3. Fall-through is explicit through the specification of the targeted case,
  23. ;    which can be any of the possible cases, even backward, skip intermittent
  24. ;    cases, whatever.
  25. ; 4. Each case label is expressive.
  26. ; 5. Default statement is replaced with the check on fail.
  27. ; 6. No subtle bugs caused by the implementation of the switch statemnet.
  28. ;
  29. ; Kermit scripting language is not C. When programming in Kermit, use Kermit
  30. ; idioms, don't mimic C.
  31. ;
  32. ; This is object-oriented programming in the small, the day_ is generic, when
  33. ; appended with a case specific value, it yields the name of a predefined
  34. ; macro, and get executed. This flexibility is very effective.
  35. ;
  36. define WEEKDAY {
  37.  
  38.     local day_0 day_1 day_2 day_3 day_4 day_5 day_6
  39.  
  40.     define day_0 { echo Sonntag }
  41.     define day_1 { echo Montag und uebermorgen ist, day_3 }
  42.     define day_2 { echo Dienstag und zunaechst kommt ..., day_3 }
  43.     define day_3 { echo Mittwoch }
  44.     define day_4 { echo Donnerstag }
  45.     define day_5 { echo Freitag und gestern war, day_4}
  46.     define day_6 { echo Samstag und da ist schon wieder der, day_0}
  47.  
  48.     day_\v(nday)
  49.     if fail echo Invalid day - \v(nday)
  50. }
  51.