Little more than a structured goto with a nicer getout, every C programmer knows (and many love) the switch statement. Even if you don't love it, there are obvious uses that produce cleaner code than strings of else-ifs and help prevent many simple kinds of errors.
Take for instance the following code, for which Gulliver writes in asking, "how many times do you have to check a condition?"
void ReplaceCommas (char *buffer)
{
int i,j;
j=strlen(buffer);
for (i=0; i < j; i++)
{
if (buffer[i]==',')
{
if (buffer[i]==',') buffer[i]='.';
}
if (buffer[i]==',')
{
if (buffer[i]=='"') buffer[i]='\'';
}
}
return;
}
Still, it's not all sunshine and lollipops. Call it "Pallas' Law," but I am convinced that given any language construct, for every use there is an equal and opposite abuse. Christian sends along the following specimen.
BOOL function(zControl **key,zControl **focus,BOOL bf) {
zWindowDlistIter trav(&this->zWindow::kidslist());
zWindow * child;
zControl * f;
assEvH()->SetMenuItems(0, -M_EXECUTE, 0);
while ( ( child = trav() ) != NULL )
{
if ( ( f = dynamic_cast<zControl *>(child) ) != 0 )
{
int DomId = f->zChildWin::ctrlId();
switch ( DomId )
{
default:
break;
}
}
}
return(TRUE);
}
The first corollary to the above statement is, of course, that these abuses are the cornerstone of a wide range of liver conditions.