Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Adding clarity to switch statements: a humble PHP feature proposal (shinyplasticbag.posterous.com)
11 points by markchristian on Oct 7, 2010 | hide | past | favorite | 17 comments


I don't see what this provides aside from redundant verbosity... How much a stumbling block is switch really?

"Oh you mean it just keeps going until you break? Okay, duly noted."

Fall through switch is quite useful, and if you really wanted an if-else, you'd use one, right?!


I like what Perl does: it separates cases by making them look like regular blocks:

    given($cat_macro){
        when(/OH HAI/){ 
            say "ceiling cat is watching you switch statements";
        }
        when(/OH NOES/){
            die "FAIL";
        }
        default { 
            say "<picture of a walrus with a bucket>";
        }
    }
Pretty easy to follow the control flow, even when they aren't oneliners.


That's not bad. I take it you you could have "when(foo or bar)" to handle multiple cases?


given/when are better than that; see http://perldoc.perl.org/perlsyn.html#Smart-matching-in-detai... for all of the possibilities.


I've got this great idea. Instead of :

next;

have:

// Continue through next case

The best part is that it's built into the language!!


As mentioned in the entry, the idea of having an actual keyword is a bit better than that. In particular, it would make it feasible for a strict notice to be raised if neither keyword was used.


The example implementation loses out on what actually putting it in the language would gain, though: causing an error when forgetting to include next; or break;.


I would really love for it to be an error to include neither, but that would be a massively backwards-incompatible change.


Sure, but it could raise an E_STRICT.


That would still be a massively backwards-incompatible change. Lots of people run using E_STRICT.


Better solution would be getting rid of "break" and breaking by default like VB but no one would like that...


Actually, that's not a better solution -- case fall through is remarkably handy. It's just a problem if it happens by accident.


switch in C syntax is terrible implemented, VB done this one so much better. That's one of the reasons why switch is not that popular in C languages (people tend to do crazy if-then-else stuff) and so popular in VB, most devs tries to use select where possible.


Does VB have a switch statement, or do you mean select? (Not nitpicking, just curious. It's been a while since I used it.)


I mean select


switch($some_value) { case 1 : doSomething(); case 2 : doSomething(); break; case 3 : doSomethingElse(); break; default : doSomething(); doSomethingElse(); }


switch($some_value) { case 1 : doSomething(); case 2 : doSomething(); break; case 3 : doSomethingElse(); break; default : doSomething(); doSomethingElse(); }

Is there any reason why keeping things simple is not an option? The number of possible outcomes in a switch case can often be so many that falling through would just be the route to complicated and difficult to manage code.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: