The Daily WTF
There are certain things which you see in code that, at first glance, if you haven’t already learned better, look like they might almost be clever. One of those in any construct that starts with:
switch(true) {…}
It seems tempting at various points. Your cases can be boolean conditions now, but you can also collapse cases together, getting tricky with breaks to build complex logic. It’s more compact than a chain of ifs. It’s also almost always the wrong thing to do.
Kasha stumbled across this while tracking down a bug:
// The variable names in this code have been anonymized to protect the guilty. In the original they were actually ok. private function foo($a, $b) { switch (true){ case ($a&&$b): return ‘b’; break; case (!$a&&!$b): return ‘c’; break; case $a: return ‘a’; break; casedefault: return ‘unknown’; break; } }
As Kasha’s comment tells us, we won’t
To read the full article click on the 'post' link at the top.