EntropySink
Technical & Scientific => Programming => Topic started by: Mike on December 11, 2014, 11:11:35 PM
-
I found this gem today (reduced down just a little):
<?php
switch (true) {
case ($perm->CanRead('permission1') || $perm->CanRead('permission2')):
break;
case ($some_other_condition_expression):
break;
case ($one_last_expression):
break;
}
I was floored. I was amazed that (and sickened) that first PHP allowed that and second that someone actually thought it was a good idea to use it. And to top it off, we found that the second two cases were never going to be true and were completely useless.
-
I've never seen that technique. Interesting.
-
I hope to never see it again.
-
I mean, it kinda makes sense if you're just trying to test which conditions true first in a given order.
-
Been looking around and it appears to be common(ish).
My issue is that it isn't immediately and intuitively obvious what the behavior will be. I'd go with the if..elseif..else chain, especially in cases where you aren't using case fall through.
-
Yeah, it does seem weird. Was there a default case or is it impossible for everything to be false?
-
No default, cases could all be false. The one that was found was adding additional where clasuses to the query to restrict it further.