On Tue, May 05, 2009 at 12:12:33PM -0400, tedd wrote:

> At 11:29 AM -0400 5/5/09, Robert Cummings wrote:
>> On Tue, 2009-05-05 at 11:21 -0400, tedd wrote:
>>  > At 10:13 AM -0400 5/5/09, Robert Cummings wrote:
>>  > >Just so we all know why...
>>>  Yep -- just so we know why:
>>  >
>>>  <http://php1.net/a/if-v-switch/>
>>  >
>>>  It all depends upon how you use the tools at your command.
>>>  My preference is still valid and I think the code is more readable. 
>>> YMMV.
>> Extra level of indentation, needing to add a break statement for every
>> case. Yours is more verbose and less clear. Forgetting a break statement
>> would lead to a silent bug. Abuse of the switch statement... switching
>> on a constant is not a switch at all. I consider it obfuscated. The
>> techniques you've used are also non-portable to most other languages
>> with a switch construct. Sorry, your attempt to call this good advice
>> fails.

I hate to dogpile on Tedd, who's probably a better coder than I, but
I'll disagree on two points:

1. I have no problems with the execution or readability of elseif. In
fact, I'm grateful for it, since some languages don't have it.

2. I consider Tedd's use of the switch/case construct an abuse. The list
has seen this debate before, and Tedd's way of doing it is *legal*. But
in my opinion, the variable which switch tests should be the only one
tested, as:

switch ($var) {
    case 'alfa':
    case 'bravo':


switch (true) {
    case $var1 == 'alfa':
    case $var2 == 'bravo':

I also appreciate the argument about doing it as it's done in other
languages. Tedd's right, this isn't those other languages. But when you
code in other languages as well as PHP, doing it the same way in all of
them really helps. The lack of semicolon line endings in Python drives
me nuts. Perhaps worse, Python has no switch/case construct. For some
reason van Rossum eschewed this in favor of if/else/elif. Ugh.

Ultimately, it's Tedd's choice, and if the code works, it works. I just
wouldn't do it that way. I agree with Robert-- if I saw code like this I
had to maintain, I'd be slightly miffed.

Paul M. Foster

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to