So, here's the crazy assertion from chapter 8:
Although a multiple selector can be built from two-way selectors and gotos, the resulting structures are cumbersome, unreliable, and difficult to write and read. Therefore, the need for a special structure is clear.Really? So this:
if ( x == 1 ) y = 2;
else if ( x == 2 ) y = 3;
else if ( x == 3 ) y = 5;
else if ( x == 5 ) y = 8;
else if ( x == 8 ) y = 13;
else y = 21;
is more cumbersome, unreliable, and difficult to read and write than:
switch (x)
{
case 1: y = 2; break;
case 2: y = 3; break;
case 3: y = 5; break;
case 5: y = 8; break;
case 8: y = 13; break;
default: y = 21;
}
How so? I'm not against switch statements because they are more efficient when the number of options is large, but I don't see how they are any more readable. And, in the case of C/C++, they are a whole lot LESS reliable because a misplaced break completely changes the execution path without any warning from the compiler.
Then, just before conceding that multiple selection can be implemented perfectly well with if statements, he drops this gem:
Of course, choosing among these approaches [the most efficient means of implementing a switch] is an additional burden on the compiler. ... As in other situations, determining and using the most efficient method costs more compiler time.Dude! Are you still using a 386 machine? I can compile the entire GPS Engine at work (our core program; about 200,000 lines of C# code) in about 4 minutes. Why do you keep stressing about compile time?
I swear I'm going to chuck this book in the campfire when this class is over.
No comments:
Post a Comment