• workmad3 (unregistered) in reply to Michel Colman
    Michel Colman:
    J.:
    I've had the opposite. One of my university lecturers in introductory programming told us using switch was bad because "it's almost like using goto". I stopped attending his lectures after that.
    I never use statements like "if", "for", "while", etc. because they are almost like using goto too. Granted, it makes coding a little bit more difficult, but at least I can rest assured that my code is clean and easy to maintain.
    How about functions? Those are exactly like using goto, as you 'label' a block of code with a name, and then use that label to jump to that block of code? Do you steer clear of functions to make your code nice and readable? :)
  • segmentation fault (unregistered)

    um...if youre going to hash, why not have a hash table that contains function pointers? or can you not do that in this language?

  • gu13 (unregistered)

    The really sensible way to do this if you're going to be doing something non trivial with command line parsing is to use an existing class to do it, or write your own (it isn't hard). Really, the only constraint on this sort of code is that it should be easy to maintain. Even if it runs in exponential time it isn't particularly significant. How long do you think it takes for the VM to start up? In other words, you're an idiot if you think the parse code is even worth thinking about in terms of performance. Robustness and maintainability are good things to think about. Worrying whether stringcmp is faster than a mersenne twsister like hash is not really relevant.

  • zzo38 (unregistered) in reply to J.
    J.:
    I've had the opposite. One of my university lecturers in introductory programming told us using switch was bad because "it's almost like using goto". I stopped attending his lectures after that.
    I have emulated goto using switch in PHP (because PHP doesn't have a goto command). People have told me it was bad but I think it would be better if PHP actually had a goto command.
    <?php
     function testfunction($x) {$label='';while(1){switch($label){
      case '':
       if($x==0) continue ($label='fail')*0+1;
      case 'ok':
       echo "ok\n";
      case 'fail':
       echo "fail\n";
       return;
     }}}
    ?>
    
    Of course this example is a simple code that doesn't need goto, it could be written like this instead: (it writes "ok" only when non-zero and it writes "fail" regardless of the parameter $x)
    function testfunction($x) {
     if($x) echo "ok\n";
     echo "fail\n";
    }
    
    But for more complicated things you do need goto. I used something like this when writing a program in PHP to emulate MESH:Hero (by Everett Kaser software). It isn't finish yet but will be open-source and free so you don't have to pay.
  • (cs) in reply to Devi
    Devi:
    gusmao:
    There is yet another gotcha. Since the return value of hashfunctions may collide, depending on the values of the String passed as argument he can have two different options being resolved into the same case.

    Would it even compile though? C++ throws an error with two identical case values in a switch statement...

    That is the horrible thing about hash systems though, I often use a crc32 algorithm to hash strings to make identifiers for class instances/whatever. So far I've never had a single collision (I'm guessing the chances are 1 to 2^32 against, but I'm probably wrong) but one day it'll happen, and everything will go bang...

    I think the problem was that two strings with non-identical hashes could, conceivably, have identical hashes if one were misspelled. Let's say you had an option --list for listing the contents of your database, and --delete for deleting from it. Let's further say that "--lsit" or "---list" or "-list" hashes to the same thing as "--delete", even though "--list" (the value in the switch) doesn't, so all the listed switch values are distinct.

    If that were the case, then a simple typo (which should be rejected) would have unintended negative consequences. True, it's incredibly improbable, but definitely not impossible.

  • (cs)

    Appearently "j(NQ, iGNQ, hfNQ, iFmQ, hemQ, j'mQ, j)/Q, iH/Q, hg/Q, j(O2, iGO2, hfO2, j)02, iH02, hg02, iFn2, hen2, j'n2, j(Mp, iGMp, hfMp, iFlp, help, j'lp, j).p, iH.p, hg.p" all have the same hashcodes under Java.

    Addendum (2010-09-11 15:11): By the way, on the subject of "multi", these also all have the same hashcode: "nWNW+, o8NW+, mvNW+, nVmW+, o7mW+, mumW+, nX/W+, o9/W+, mw/W+, nWO8+, o8O8+, mvO8+, nX08+, o908+, mw08+, nVn8+, o7n8+, mun8+, nWMv+, o8Mv+, mvMv+, nVlv+, o7lv+, mulv+, nX.v+, o9.v+, mw.v+, nWNVJ, o8NVJ, mvNVJ, nVmVJ, o7mVJ, mumVJ, nX/VJ, o9/VJ, mw/VJ, nWO7J, o8O7J, mvO7J, nX07J, o907J, mw07J, nVn7J, o7n7J, mun7J, nWMuJ, o8MuJ, mvMuJ, nVluJ, o7luJ, muluJ, nX.uJ, o9.uJ, mw.uJ, nWNUi, o8NUi, mvNUi, nVmUi, o7mUi, mumUi, nX/Ui, o9/Ui, mw/Ui, nWO6i, o8O6i, mvO6i, nX06i, o906i, mw06i, nVn6i, o7n6i, mun6i, nWMti, o8Mti, mvMti, nVlti, o7lti, multi, nX.ti, o9.ti, mw.ti"

  • DarrickPiero (unregistered)

    Hello Guys, Glad to Join! :)

  • DarrickPiero (unregistered)
    Comment held for moderation.

Leave a comment on “Classics Week: How Not to Parse Command Line Arguments”

Log In or post as a guest

Replying to comment #:

« Return to Article