Re: Re: Switch - Case Statement Questions

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

 



On 17 Nov 2012, at 10:53, Tim Streater <tim@xxxxxxxxxxxxxxxx> wrote:

> 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.

I don't see how it breaks the "principle of least surprise" (which is a new one on me. KISS, yes. Least surprise? Wassat?). If you consider that behaviour to be surprising the don't ever go near Perl.

A switch statement is basically saying "take this value and compare it to each case below in the order they're presented until one matches, then execute that block of code until you hit a break which will jump execution out of the switch statement." I don't see how making "this value" true or false makes it any more surprising than if it's "squirlookle."

> 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 consider that usage of the switch statement to be "clever." It's a valid usage of the statement which gives it a lot more power. Specifically the ability to have the code for a given case deliberately fall through to the next case block is worth any price of admission. And finally, in my opinion, it's far cleaner than a succession of if/elseif/else statements, and ultimately gets compiled to very similar opcodes.

I agree that the job of a programmer is to balance providing a fast, efficient, correct solution and minimising maintenance issues. I see neither side being compromised by using a switch statement in this manner, in fact I see both sides potentially getting a boost. Just because the usage is unfamiliar or even surprising to you doesn't mean it's wrong.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[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