which of these faster using switch or using multiple if conditions
Hi Priya,
This doesn't matter at all. Regardless of what everyone else says here, OutSystems Switches are basically a list of Ifs. It is true that in some high-code languages like C# or Java, a Switch can be more efficient than an If. But in these languages, switches can only be done on one variable, and the various branches are taken based on the variable's value.
However, in OutSystems, the conditions for each Switch branch can contain any condition, so is very much like an If. I haven't checked the actual generated code, but I'm pretty sure that it can't use the optimizations that languages have for actual switches.
So, the only matter that is of concern, is code readibility. I find that have more than a few switch branches can make the code become pretty cluttered, vs. a series of Ifs that look a lot cleaner. Example:
I agree with @Kilian Hekhuis . I have checked the JavaScript generated for a Client Action, and it is a chain of if statements.
OutSystems Switch constructs can’t use the same optimizations as switch statements in C# or Java because they handle complex, multi-variable conditions instead of single-variable comparisons. This flexibility makes them more like if-else chains, which prevents the use of efficient optimizations like jump tables.
Thanks Siya for the technical confirmation.
Hello @Priya Naveen
This is completely depend on the number of condition you are checking.
If you have small number of conditions like less than 5 then you can go for the multiple if else. Or if the conditions are more than 5 then I suggest you to use switch.
Switch statement are generally faster as compare to the multiple if-else.
Regards
Prince
A switch statement is typically considered better than multiple if statements in situations where you have a single expression (or variable) that you want to compare against multiple possible values. Here are some scenarios where using a switch statement might be preferable over multiple if statements:
I hope this helps
Thanks
A switch block is generally more readable and maintainable than chained if-else statements. It is easier to extend a switch case block than an if-else block because with if-else statements, you must evaluate all previous conditions to correctly insert a new else block.
thanks
cv
This is also true for Switches! OutSystems switches allow any condition, so if your first condition is A < 5, and your second one A = 2, the second one will never run!