Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^3: fall through switch/case in perl

by radiantmatrix (Parson)
on Sep 07, 2004 at 15:40 UTC ( [id://389106]=note: print w/replies, xml ) Need Help??


in reply to Re^2: 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.

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});

Replies are listed 'Best First'.
Re^4: fall through switch/case in perl
by Anonymous Monk on Sep 07, 2004 at 15:59 UTC
    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});

        I'm merely an acolyte, for cripe's sake: why on earth are you taking anything I say so seriously?

        Because XP doesnt mean anything. TimToady is "just" an abbot. We listened to him before he was even an acolyte, when he was an acolyte and afterwards.

        Anyway, I dont know how the AM got the wrong side of the points you were making, but they sounded to me like there was some truth in what you say. Especially as "switch" in C only handles integers.


        ---
        demerphq

          First they ignore you, then they laugh at you, then they fight you, then you win.
          -- Gandhi

          Flux8


        So, we should dismiss anything you say as just an opinion? Postings that don't make clear what are facts and what are opinions are worthless.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://389106]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 16:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found