Branching in switch statement

Started by
35 comments, last by wack 10 years, 2 months ago

<Unreal Tournament Announcer Guy> RAMPAGE!!

Advertisement

I kind of agree that switch fallthrough is rather unintuitive. Yes, it does work, but it's not what most people think of when they see a switch statement. In the mind of most people a switch statement is a simple one-to-one mapping of condition and resulting action (without any notion of it being a chain of goto's and labels) and the fallthrough is an antiquated way of mapping multiple conditions to a single action without too much repetition, and should probably have been deprecated by a more modern approach such as range cases (or even arbitrary conditions, which when known to be constant could be made into an efficient jump table). There are probably very good reasons languages did not decide to do this, and that's fine, I'm just saying that most people not familiar with the fallthrough behaviour of the switch statement are not going to understand when they see stuff like "case 0: case 1: ..." and are more likely going to go WTF. In other words, it's not intuitive, and that IMHO is a rather big issue in a programming language (even if it's easy to learn about the fallthrough), and personally I feel the switch statement lies a bit too far on the "terse" side of things for my taste. But that of course is subjective.

On the other hand, not using the switch statement out of fear sounds like superstitution, something a programmer should never have to deal with. Purposely avoiding a valuable (if slightly convoluted) tool out of your toolbox doesn't strike me as particularly rational. But, hey, whatever works for you!

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

JohnnyCode:

Everyone else is trying to have a sensible discussion here, and those users who have chosen to disagree with you have done so politely and taken the time to explain their reasoning and/or provide examples. Please don't derail what could be valuable discussions for other users by degrading into ramblings and off-topic nonsense -- in particular, I never want to see six separate posts in a row with little-to-no on-topic discussion again -- if you find you have additional points to add after posting, you're welcome to use the edit function to add them.

You should also try not to take it personally when people provide alternative explanations or opposing points of view. If you can actually provide a clearly explained logical reason why you think switch statements should not be used I'm sure people will be more than happy to listen.

- Jason Astle-Adams


I am not saying switch is invalid to use, but, you might have used standard native instructions instead no? A returnal function upon constant provided into it, to analize 20 possible values of it, skipping after a hit.

This isn't very clear (perhaps you might try re-phrasing it more clearly when you're not upset! smile.png) but if I understand what you're saying here correct, then no -- as a number of the above posts have mentioned, in many languages you simply can't encode the same logic as a switch via alternative control structures, unless you're talking about manually writing your own jump table in assembly.

You can always achieve the same result using a chain of if statements, but because each individual statement must be evaluated for the logic to function correctly the performance will likely be much worse than a switch which can simply jump to the correct block without having to examine the non-relevant cases.


but this totaly braeaks a program priciples ({} blocks), do this in a function or whatever definition and see what restrictions in using your function fires out! And performance speed out is... none, or, compiler smartness dependant.

Could you explain what you're trying to say here more clearly?

You seem to be suggesting that a switch statement somehow disobeys or confuses scoping rules, but that simply isn't the case, and if you understand how they work there's absolutely no confusion about how program flow will proceed.

You are correct that the possible performance gain is dependent on how smartly the compiler is able to optimise... but the same is true of every single optimisation used, and in this case we're talking about a common and well-understood optimisation that you would be surprised not to find in any well-known and popular compiler.

Yes, switch statements can be hard to understand -- especially if they're (ab)using fall-through or contain unusual jumps -- but they're also a common and well-defined part of many programming languages, and they're not some arcane construct that's impossible to understand.

If we're misunderstanding you please do try to explain yourself more clearly, but honestly it seems like you're applying a little superstition or misunderstanding some things rather than presenting a well-reasoned objection to the use of switches.

//EDIT: I've probably been a little imprecise here, but thought that simple/clearer language might be better suited to the conversation.

- Jason Astle-Adams

If I want to ease up and funny up the discusion does not mean I am targeting some insults. I understand you removed Cypress Hill song I love you mary jane. The fact someone is not totaly strict in discussing does not mean he tryies to say something negative at all. I hope no one found it negative or insulting.

I yet wish to say, that all my conding career I very insisted on every junior to not use switch and goto. Only those two particular instructions.

On the other hand, not using the switch statement out of fear sounds like superstitution, something a programmer should never have to deal with.

Yes, but if programmer cannot learn and master something very properly or easily, he should reflect that fact. I dropped switch statement for myself at the moment I realized I will never need it, not at the moment of not undestanding it. Sort of an intuition maybe, I had no idea some languages does not implement it. all of the other instructions were clear to me at the very work they perform.

The runtime behaviour of switch was never clear to me, but this made me to inspect switch a bit more, finding out it is not very good to make me less stressed about my code, or port it. As an older programmer I very target expresivity and resistance of code. Alvaro used switch statement. becouse he is extremly inteligent, and perhaps his code never exhousted him.

When one really restricts self to structured code, instructions like continue and break (where the latter is used in C/C++ switch statements, too) have to be avoided, and also return has (with the exception of being the very last instruction of a function body, but then only for the purpose to name the result variable). This is because all of these instructions terminate execution of a block somewhere between.

We could live without them, that is true. But avoiding them will introduce more hassle in some situations than using them would do: You may introduce more variables to hold resulting or temporary values; you may need to implement more or more complex conditional clauses. You may get less performance because the additionally introduced (and, let's say it: unnecessary) jumping.

As game programmers we're after performance. Some things are easy to get: Shortening function execution by early returning, serving the far more probable branch in the "if" instead of the "else" block to not break CPU pipelining, and similar things. I do not count such things to premature optimization. Compiler optimization can do some things, but usually it does not hurt to support it (I hope ;))

IMHO the switch statement falls into the same category. It isn't a necessity, but it is helpful in some situations and hence should be considered to be used. My 2 Cents; of course do I respect your current dislike for it.

BTW: I've started programming in 1983, so I think cannot be counted to a young generation anyhow. OTOH I'm not a nerd w.r.t. todays compilers and CPU utilization and hence only scratch the surface.

If I could change one thing about the switch statement, it would be that you would always have to specify either "break" or, for those rare occasions when it's non- empty, and you want to go on: "pass".

But I guess it would break too much existing code to ever come to pass. ;)

This topic is closed to new replies.

Advertisement