"IF THEN - $SELECT - $CASE" - What is the best way?
Hi all,
I'm wonder what could be the best way convert the sentence "Switch" C# into Cache code.
int caseSwitch = 1; switch (caseSwitch) { case 1: Console.WriteLine("Case 1"); break; case 2: Console.WriteLine("Case 2"); break; default: Console.WriteLine("Default case"); break; }
My first attempt was:
set caseSwith = 1 if (caseSwith = 1) { w "Case 1" } else { if (caseSwith = 2) { w "Case 2" } else { w "Default case" } }
Maybe, for complex conditions, it should be the best option, however I've used the $SELECT command
set caseSwith = 1 w $SELECT(caseSwith=1:"Case 1",caseSwith=2:"Case 2",1:"Default case")
It seems the best option for simple sentence (one line and clear)
Other options is using the $CASE command
set caseSwith = 1 w $CASE(caseSwith, 1:"Case 1",2:"Case 2",:"Default case")
so, it is easier because it is checking only one variable to evaluate the condition.
My question is, what should be the best way for complex conditions?
set val1 = 1 set val2 = 2 set val3 = 3 if ((val1>=3)&&(val1<=9)) { if (val2=0) { w "Condition 1" } else { if (val2>0) { w "Condition 2" } } } else { if (val2=1) { if (val3<3) { w "Condition 3" } else { w "Condition default 1" } } else { if ((val2>1) && (val2<9)) { w "Condition 4" } else { w "Condition default 2" } } }
As you see, it so complex because the condition is linked to 3 variables.
Trying to use $SELECT with this example
set val1 = 1 set val2 = 2 set val3 = 3 w $SELECT((val1>=3)&&(val1<=9): $SELECT(val2=0:"Condition 1",val2>0:"Condition 2"),val2=1:$SELECT(val3<3:"Condition 3",1:"Condition default 1"),val2'=1:$SELECT((val2>1) && (val2<9):"Condition 4",1:"Condition default 2"))
The line is longer and quite complicated to read
I haven't used $CASE because it affect to more than one variable.
So I don't want to create hundreds line of IF THEN conditions, but I want to have a clear code and easy for maintenance.
Please comment on how do you would solve this problem