Re: Re: Switch - Case Statement Questions
| From: | Sebastian Krebs | Date: | Sat, 17 Nov 2012 20:59:44 +0000 |
| Subject: | Re: Re: Switch - Case Statement Questions | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-319726@lists.php.net to get a copy of this message | ||
2012/11/17 Tim Streater <tim@clothears.org.uk>
> On 17 Nov 2012 at 01:33, Iñigo Medina <imedina@grosshat.com> wrote:
>
> > On Fri, 16 Nov 2012, Jim Giner wrote:
>
> >> Maybe I'm way out of touch, but when I look at php.net for the syntax
> of the
> >> switch statement I see:
> >> switch($var){
> >> case (value):
> >> (do something)
> >> case (other value):
> >> (do something else)
> >> }
> >>
> >> IN your example, you are using a switch syntax that is nothing like
> that. I
> >> think that your "case " statements are not actually evaluating the
> value of
> >> $count, but are themselves evaluating to a true value that has nothing
> to do
> >> with $count.
> >
> > That
switch is evaluating to true each case. It's an
> > alternative of
> using
> > if-elseif structure.
>
> It may be an alternative, but it breaks the principle of least surprise.
> If I'm looking through someone's code, and I see a switch, I expect to see
> it used as Jim described. If the writer needs a different type of logic to
> make a decision, that is what the cascaded if-elseif-elseif construct is
> for, when the tests don't fall into a simple set-of-values choice.
>
But switch(true) is not a "surprise" by itself, it becomes to, when the
cases are surprising. And the given example directly fall into "a simple
set-of-values choice", but the problem is, that switch(val) by design only
covers equality "==" and thats what this case is: A set like "(1) between 1
and 7, (2) between 8 and 14, (Default) greater than 14". On the other side
a looong if-elseif*-else-chain is often more confusing and needs
refactoring anyway.
(Beside: Was "the principle of least surprise" not a ruby-thing? ;))
>
> The job of a programmer is not to be "clever" with a view to impress those
> who follow, but to achieve the desired outcome while at the same time
> making life easy for those who follow.
>
I don't see a problem in "case ($count <= 7) or case ($count <= 14) or case
($count > 14)"? I think a developer (or maybe programmer) doesn't need to
be very clever to get the idea behind this.
>
> --
> Cheers -- Tim
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
--
github.com/KingCrunch