in reply to Re: fall through switch/case in perl
in thread fall through switch/case in perl

Your code will either not print anything, or it'll never finish printing.

In practice, however, if your algorithm needs a switch equivalent, you're better off re-thinking your algorithm.

Eh? And why is that? That's a pretty bold statement, considering many languages do have a 'switch', perl6 will have a switch, there's Switch.pm, both the manual and the FAQ discuss it (without dismissing it as choicing a bad algorithm), and it has been on the wishlist since the very first release of Perl in 1987.

Replies are listed 'Best First'.
Re^3: fall through switch/case in perl
by radiantmatrix (Parson) on Sep 07, 2004 at 15:40 UTC
    Your code will either not print anything, or it'll never finish printing.

    You're right, I forgot the $junk--; after the print statement.

    That's a pretty bold statement, considering many languages do have a 'switch', perl6 will have a switch, there's Switch.pm, both the manual and the FAQ discuss it (without dismissing it as choicing a bad algorithm), and it has been on the wishlist since the very first release of Perl in 1987.

    I never said switch was bad code. Perl6 will have a switch because a lot of people want it, and it is very readable. However, switch statements are unbelievably over-used. I stand by my statement; chances are, if your algorithm needs switch, you can come up with a better algorithm. There are, of course, a few exceptions.

    As to your "considering many languages have a 'switch'", multiplicity does not correctness make.

    --
    $me = rand($hacker{perl});
      I never said switch was bad code.

      You mean, people should rethink good code?

      As to your "considering many languages have a 'switch'", multiplicity does not correctness make.

      No, but it does mean your bold statement needs at least a shred of backup. Just stating "switch statements are unbelievably over-used" does not make correctness either. Back up your claim.

        You mean, people should rethink good code?

        Well, yes. But that's not the point, exactly. Using switch statements doesn't automatically make bad code; however, this thread alone shows several ways to accomplish the same task without using a switch. Some of those ways are actually better than using a switch.

        Summary: always rethink good code, as long as you think it can be better.

        Just stating "switch statements are unbelievably over-used" does not make correctness either. Back up your claim.

        You do realize these are my opinions, right? That said, I repeatedly see programmers use switch syntax in odd places. When I ask them why they did it, the usual reply is "it was easiest". Switches are used (typically) to evaluate what might otherwise be a big chain of if/else/.../elsif statements. There are (obviously) other ways to accomplish that task, but that's not really important.

        The important bit is that there really aren't that many occasions for that level of nested if/else checking, and my personal experience shows that rethinking an algorithm requiring a switch almost always results in a better algorithm.

        Then again, as I've said before, there are cases where it really is the only reasonable way to approach a problem.

        Really, there is no need to assume that a statement of opinion is some kind of programmig law. I'm merely an acolyte, for cripe's sake: why on earth are you taking anything I say so seriously?

        --
        $me = rand($hacker{perl});