- Home /
Are there any draw backs to using this kind of Switch statement?
Basically I discovered that I'm able to use switch statements to compare values other than just "==". I wondered as I've not seen this technique being used very widely are there any obvious draw backs to this approach, or is this a completely legitimate way of switching through value comparisons.
var value : int = 50;
var isWorking : boolean = true;
switch(isWorking){ //can be anything boolean
case (value > 10) :
//Do Stuff
break;
case (value > 5) :
//Do Stuff
break;
default :
//Do Default Stuff
break;
}
Answer by Bunny83 · Dec 05, 2011 at 11:53 AM
First of all i don't see the reason why you would do this. Isn't it much clearer and shorter this way:
var value : int = 50;
if (value > 10) {
} else if (value > 5) {
} else {
}
AFAIK the switch statement only works with constant expressions. I guess the compiler evaluates "(value > 10)" to "true" (because value is 50) so it won't work when you change the value at runtime. Beside that the case expressions have to be unique. You can't have two case statements with the same value. You use a boolean so there are only two possible values: "true" and "false". Your two first cases evaluates to true, so what case would you expect to run? The first? the second? both?
You should write code in a way that's clear and easy to read / understand. Even when it works in some way, most other people that have to work with your code don't have a clue what you want to do here.
edit
I've tested it. It seems that it works exactly like my if else chain in UnityScript. That means when you exchange your two case statements (value > 5) before (value > 10) it won't work. case statements usually don't have to be in an explicit order this "special" case requires it.
Okay a good reason as any is readability. Thanks for the feedback, and explanation.
Just to add, it does work when changing the value at runtime, in my own I have a value changing on update, and i still get the desired output.
edit (in response to your edit):
Obviously if the value is 50 then the first case will break the switch... almost the same as an IF/ELSE
Yes, it should work through lambdas. I take it your hint is if there is any performance considerations? You'd probably need a few ten thousand objects to even start noticing differences, if there are any. Then again, to be sure you could make a stress loop that go through a few million tests each frame.
There is absolutely no difference in the generated code except the extra (useless) boolean comparison each if statement. The compiler actually convert this into an if else chain, but in C sharp the case values have to be a constant expression. Your example won't compile in C sharp (i've tried it).
Answer by ks13 · Dec 05, 2011 at 11:21 AM
Hmm, from what i know of "switch", you're doing it wrong. In switch(true), the true should be the vriable to be compared to different cases, so in your example, you'd have case(true), case(false) and eventually default. If you want to compare value, then you should have switch(value).
Well when comparing values directly "==" you would be 100% correct.
But i found this does seem to work, I think whats happrening is if the switch input is "true" (which will be compared at the start of the statement) then calculate if "value > 10".
either way this works, I get the results I'm after... although I've never seen it used it this way so I was wondering if there was any obvious drawbacks?
Well I for one think it's a very creative use of the switch statement, and there's absolutely no reason why it shouldn't work!
@syclamoth, Creative? Yes, maybe. But it is also a bit unorthodox and can cause some serious wtf moments during a code review. I would be careful about doing too dodgy stuff even if it does work. Related topic: http://stackoverflow.com/questions/594135/whats-up-with-static-cast-with-multiple-arguments - even if for(int i = 0; ++i, i < 10;)
is valid code, is it still valid code? :)
Your answer
Follow this Question
Related Questions
Unity JS Syntax Coloring for MonoDevelop 0 Answers
Longest Common Subsequence Recursive Function Error. 1 Answer
Item Scripting Error 2 Answers
if ((this || that) && (that || this)).. Does that work? 3 Answers