Re: Re: Switch - Case Statement Questions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2012/11/17 Tim Streater <tim@xxxxxxxxxxxxxxxx>

> On 17 Nov 2012 at 01:33, Iñigo Medina <imedina@xxxxxxxxxxxx> 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

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux