Submitted by Garrett Albright (not verified) on Sun, 07/12/2009 - 18:19.
I never understood why people use the "switch" statement just to "break" at the end of each code block - see many implementations of Drupal's hook_block() or hook_nodeapi() which switch on $op. If you're not going to be making use of the fallthrough functionality switch allows, why not just use the "if/elseif" statements and avoid switch's stupid syntax?
Submitted by mpapec (not verified) on Thu, 07/16/2009 - 03:33.
with all these breaks the switch is sadly even more verbose than if/elseif blocks. Not to mention bugs if you forget breaks.
ideally switch should something be like,
Comments
I never understood why people
I never understood why people use the "switch" statement just to "break" at the end of each code block - see many implementations of Drupal's hook_block() or hook_nodeapi() which switch on $op. If you're not going to be making use of the fallthrough functionality switch allows, why not just use the "if/elseif" statements and avoid switch's stupid syntax?
Switches are easier to type
Switches are easier to type and read, and get optimized to jumptables in most language
$op will getting killed for
$op will getting killed for the next version of drupal.
why switch?
because it is less verbose than if/elseif :-)
Re: I never understood why people
I never understood why people use lots of if/elseif blocks to test the value of the same variable in each block ;-)
Bingo!
Bingo!
switch
with all these breaks the switch is sadly even more verbose than if/elseif blocks. Not to mention bugs if you forget breaks.
ideally switch should something be like,
switch ($foo) {
1: {
print 1;
},
2: {
print 2;
},
}
looks like hash, doesn't it?
with new closures that would be
$switch = array(
1 => function () {
print 1;
},
2 => function () {
print 2;
},
);
$switch[$i]->(); # or something like that
Anyone else use this idiom ?
<?phpswitch(true) {
$a === $b =>
echo 'a === b';
break;
$a == $b =>
echo 'a == b';
break;
$a < $b =>
echo 'a < b';
break;
$a > $b =>
echo 'a > b';
break;
}
?>
Post new comment