New super switch, was Re: The final word on GOTO
Michel Jackson
jackson at jabberwock.shs.ohio-state.edu
Mon Oct 9 11:43:39 AEST 1989
Press, Flannery, Teukolsky, & Vetterling remark, in their very useful
book _Numerical Recipes: The Art of Scientific Computing_
Every programming language has some number of "goodies" that the
designer just couldn't resist throwing in ... In FORTRAN we consider the
ill-advised control structures to be
-assigned GOTO and ASSIGN statements
- computed GOTO statement
- arithmetic IF statemetn
In Pascal an example is
-CASE ... OF
In the companion volume, _Numerical Recipes in C_ (yes, i have & use
both ...), they remark
In C, the most dubious control structure is the switch...case...default
construction, recognizable to FORTRAN programmers as a kind of elaborate
"computed goto". Not only is the structure a confusing one, with a
bizarre "drop through" feature, it is also burdened with uncertainty,
from compiler to compiler, about what data types are allowed in its
control expression. It can virtually always be easily replaced by a
more recognizable and translatable if ...else construction. (p.13)
I generally agree with them. Some of the most confusing code I have
ever written relied on (long) switch statements and various drop
throughs (exhaustive testing of a function that had some 8-10
arguments & needed to be demonstrated correct & also had to be shown
to handle various combinations of incorrect inputs properly) ... i
eventually gave up trying to debug my own code & rewrote it from scratch
with a pile of if's. Surely adding "cases" will make switch statements
even harder to figure out ...
i must admit, though, that "tame" switch statements are a great convenience.
---michel jackson
More information about the Comp.lang.c
mailing list