Re: goto... is it so bad?
Walter Bright wrote:
An interesting point of view. Do you also consider replacing
switch(type) constructs by class hierarchies as a contortion?
Consider this excerpt from the D programming language compiler lexer
that scans for the end of a text line:
---------------------
while (1)
{ unsigned char c = *p;
switch (c)
{
case '\n':
p++;
break;
case '\r':
p++;
if (*p == '\n')
p++;
break;
case 0:
case 0x1A:
break;
default:
if (c & 0x80)
{ unsigned u = decodeUTF();
if (u == PS || u == LS)
break;
}
p++;
continue;
}
break;
}
------------------------
Yeah, I think it would be ridiculous to replace this with a class
hierarchy.
Maybe my wording was too terse. I was not talking about replacing just
about *any* switch statement by a class hierarchy, but switches on
*type*: if it's this type, do this, if it's that type, do that, etc. The
textbook OO solution for this sort of thing is to use runtime
polymorphism (in C++ virtual functions). Yet this seems to fit your
description of "creating classes for the sole purpose of manipulating
control flow", so that made me wonder whether you regard OO programming
by and large as a contortion.
--
Gerhard Menzl
Non-spammers may respond to my email address, which is composed of my
full name, separated by a dot, followed by at, followed by "fwz",
followed by a dot, followed by "aero".
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]