Forum Moderators: coopster

Message Too Old, No Replies

Switch or multiple if

speed, readibility, organization pro and con

         

henry0

11:49 am on Oct 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Assuming an environment that calls for many "if"
Assuming that the same procedure could be done out of "switch".

What do you think works best (could it be subjective or factual?)

onlineleben

11:57 am on Oct 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In my opinion the switch / case command is easier to read and maintain than a multiple nested if / else.

eelixduppy

1:27 pm on Oct 20, 2006 (gmt 0)



I think it depends on the situation. It's probably fastest to use switch, though.

Conclusion: switch ;)

Birdman

1:48 pm on Oct 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Switch is faster, at least my tests show so.

As far as readability, if/elseif/else is plenty readable and takes less lines of code. The reason being, to stop the switch() from evaluating every case(even after a match) you need a break line in each one. if(), however, will stop automatically after a match


if ($a == $b) {
echo 'a = b';
} elseif ($a == $c) {
echo 'a = c';
} elseif ($a == $d) {
echo 'a = d';
} else {
echo 'a = a';
}


switch($a) {
case $b:
echo 'a = b';
break;
case $c:
echo 'a = c';
break;
case $d:
echo 'a = d';
break;
default:
echo 'a = a';
}

henry0

3:28 pm on Oct 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, so Switch seems faster
to me it also seems more readable, however this could really be a matter of own judgment

jatar_k

3:45 pm on Oct 20, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



often switch can't be adopted if there are multiple else statements on those ifs but most other times switch is your best bet

henry0

3:53 pm on Oct 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As a matter of facts I am looking at solutions to make more readable many if/else

I am moving chunks in functions and classes
it makes, using OOP, the code more readable but also portable although it could be a tad slower.

coopster

5:51 pm on Oct 24, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




often switch can't be adopted if there are multiple else statements on those ifs

I think I understand what you are getting at but there actually is a trick to that. You can reverse the expression/value (they are both acceptable in either place, the switch expression/value or the case expression/value, as long as the case evaluates to a simple type -- and this case you would use a boolean value in the switch expression when you know that the case statements are structured to return a true/false boolean simple type). Some examples ...

[webmasterworld.com...]
[webmasterworld.com...]

If I'm off-base, forgive me, but at least here are some examples for those that have never considered this possibility.

jatar_k

5:55 pm on Oct 24, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



good point, I love those and I bet lots of folk haven't used them

I was just thinking structure wise based on this statement

>> Assuming an environment that calls for many "if"

if you have

if
if
if

probably could be a switch if there is corelation between conditions

if you have

if
else if
else if
else

this is exactly what a switch is and, in my mind, these should always be made into switches

if you have

if else
if else
if else

even with conditional corelation these are tricky, your method is one way but sometimes they are just better as is

I just didn't want people to think

multiple ifs == switch

it doesn't work quite that simply

coopster

7:13 pm on Oct 24, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No doubt. Great clarification, thanks.

henry0

7:42 pm on Oct 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nice to see a little call cascading in a wider river :)
Quite some food for thoughts!

That's like it:
<<<
if you have

if else
if else
if else
>>>