We all know that the
gotostatement should only be used on very rare occasions if at all. It has been discouraged to use the
goto statement countless places countless times. But why it there never anything like that about the
switch statement? I can understand the position that the
switch statement should always be avoided since anything with
switch can always be expressed by
if...else... which is also more readable and the syntax of the switch statement if difficult to remember. Do you agree? What are the arguments in favor of keeping the 'switch` statement? It can also be difficult to use if what you're testing changes from say an integer to an object, then C++ or Java won't be able to perform the switch and neither C can perform switch on something like a struct or a union. And the technique of fall-through is so very rarely used that I wonder why it was never presented any regret of having switch at all? The only place I know where it is best practice is GUI code and even that switch is probably better coded in a more object-oriented way.
We all know that the
|show 2 more comments|
closed as not constructive by Robert Harvey, GrandmasterB, World Engineer♦, Stephen C, ZJR Dec 3 '12 at 5:13
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance.
Switch statements are occasionally much more readable than cascading sets of
Fall through is occasionally useful, for example it lets you implement duffs device. But, I think that most people agree fall-through by default is a mistake. Language like Go use an explicit
Languages with Algebraic Data Types like Haskell and ML show that a more general form of the switch (in this case it is an expression) is a very elegant code structuring tool well suited for a large variety of problems.
Perhaps I'm misinterpreting this question, but there is nothing wrong with a switch statement. There are cases where you simply want to operate on a particular value, and a switch statement is perfectly clear syntax. An if/else statement is, of course, more logical in the strictest if/else sense.
A visual example helps; which of these looks better?
Traditional switch statement:
Total lines: 14
Compacted if/else statement:
Total lines: 9
Expanded if/else statement:
Total lines: 16
The compacted if/else statement is technically the shortest in terms of lines of code, but looks the most cluttered. There are exceptions to this, of course, as many languages support the case for one line statements in an if/else (or many don't use parentheses/braces in such statements at all), but the general idea gets across that switches do have their place and are not inelegant if operating on one variable.
Edit: I see that some of this was referenced in Robert Harvey's comment.