2012/11/19 Tim Streater <[email protected]>
> On 18 Nov 2012 at 14:44, Jim Giner <[email protected]> wrote:
>
> >>>>> 2. Using Switch {ALWAYS FIRST CASE!!!}
> >>>>>
> >>>>> // $boxes = 1;
> >>>>> // switch ($count) {
> >>>>> // case ($count > 14):
> >>>>> // $boxes = 3;
> >>>>> // break;
> >>>>> // case ($count > 7 && $count <= 14):
> >>>>> // $boxes = 2;
> >>>>> // break;
> >>>>> // case ($count <= 7):
> >>>>> // default :
> >>>>> // $boxes = 1;
> >>>>> // break;
> >>>>> // }
>
>
> > To answer Iñigo's comment - the OP's version is very much like an
> > If-ElseIF structure, and nothing like a Switch.
>
> Just so. Perhaps those who are not grasping the point could re-read their
> copy of "The Elements of Programming Style" by Kernighan and Plauger where
> this sort of issue is covered.
>
> See, it's all about expectations. When I worked at SLAC, there was a wise
> guy there who had removed the catalytic converter from his Scirocco and who
> though it clever to race up 280 from San Jose to SLAC at 100mph, sometimes
> slipstreaming behind another idiot doing the same. If I look in my
> rear-view mirror, I expect those I can see to be doing 70mph tops (that's
> what a lot of people did 30 years ago on 280), not 100, and to make
> judgements accordingly. Equally, I have certain expectations when I see a
> switch statement; it trying hard to look like if-elseif-etc is not one of
> them.
>
>
Just a minor rewrite of the original solution
switch (true) {
case $count <= 7:
$boxes = 1;
break;
case $count <= 14:
$boxes = 2;
break;
default:
$boxes = 3;
break;
}
Now -- the idea of "switch" as "select on of some similar cases" in mind":
Is this really breaking any expectation? I think this one is a valid
example, when switch(true) doesn't break the semantic of the
switch-statement.
And if this is already the end of expectations, then (sorry for that) the
expectations are maybe slightly limited :X I don't say "please use
switch(true) wherever possible", but this one is nearly a prototype of a
valid use-case: You have several cases all depending on a single state (the
variable) and all of the same kind "<= constant".
Beside, because it is so extremely simple example, it can become even more
obvious
switch (true) {
case $count <= 7: $boxes = 1; break;
case $count <= 14: $boxes = 2; break;
default: $boxes = 3; break;
}
Guess some may argue about coding styles now, but thats not the questios
here (and anyway: Even coding styles could/should be broken, if it leads to
better code. Use it wisely ;)). And now: This _can_ break any expectation?
By the way, your car-expectation sounds dangerous...
> --
> Cheers -- Tim
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
--
github.com/KingCrunch