No comment



  • Just found this little gem in the deeps of our codebase:

    sub num_8_edit:method

    {

         my ($numValue);

         $numValue = $_[1];

        

         if   ($numValue < 10)

         {

             $numValue = "0000000".$numValue;

         }

         elsif ($numValue < 100)

         {

             $numValue = "000000".$numValue;

         }

         elsif ($numValue < 1000)

         {

             $numValue = "00000".$numValue;

         }

         elsif ($numValue < 10000)

         {

             $numValue = "0000".$numValue;

         }

         elsif ($numValue < 100000)

         {

             $numValue = "000".$numValue;

         }

         elsif ($numValue < 1000000)

         {

             $numValue = "00".$numValue;

         }

         elsif ($numValue < 10000000)

         {

             $numValue = "0".$numValue;

         }

    EXIT_num_8_edit:

         $_[1] = $numValue;

    } #ende num_8_edit

     

    P.S. 

    This was my first post and this forum software is a wtf in itself. "Paste from word" but no obvious code formatting. :(



  • Don't really recognize the language here, but apparently it is not strongly typed.

    Still, if you can read a number as a string for concatenation, why can't you just  concatenate it with an empty string, find out how long it is and build the number of zeros you need in one call and append?

     Does the language not have that ability?  If it does, this was obvious a senior developer that wrote it, anyone below that would have been let go.  ;)
     



  • It's perl.  You could do any of these things:

     $numValue = sprintf "%08d", $numValue;

    OR

     if  (length($numValue) < 8) {
       $numValue = "0" x (8 - length($numValue)) . $numValue;
     }

    OR

     $numValue = substr("0000000" . $numValue, -8);
     



  • I particularly like

    $_[1] = $numValue;

    (I never trusted that 'return' statement either)

     

    And perldoc -f sprintf gives a good solution *in the first example*
     



  • This is perl, and of course the function they want is printf.

    printf("%08d",$numValue) would suffice. How many other num_X_edit methods did they have to create?



  • By now I've found from 1 to 16, cluttered over a bunch of modules named A1E100.pm .. B3300.pm. Ack! Want another one?

    sub transform_min_to_std_min:method
    {
        my ($mm, $hh_mm);
        $mm = $_[1];
       
        my ($hh, $mmRest);
       
        goto EXIT_transform_std_min_to_min if (not defined $mm);
       
        $mmRest    = $mm % 60;
        $mm    -= $mmRest;
        $hh    = $mm / 60;
        $mm    = "0".$mm if ($mm < 10);
        $hh    = "0".$hh if ($hh < 10);
        $hh_mm    = $hh.":".$mm;
    EXIT_transform_min_to_std_min:
        $_[2] = $hh_mm;
    } #ende transform_min_to_std_min



  • @holli said:

    By now I've found from 1 to 16, cluttered over a bunch of modules named A1E100.pm .. B3300.pm. Ack! Want another one?

    sub transform_min_to_std_min:method
    {
        my ($mm, $hh_mm);
        $mm = $[1];
       
        my ($hh, $mmRest);
       
        goto EXIT_transform_std_min_to_min if (not defined $mm);
       
        $mmRest    = $mm % 60;
        $mm    -= $mmRest;
        $hh    = $mm / 60;
        $mm    = "0".$mm if ($mm < 10);
        $hh    = "0".$hh if ($hh < 10);
        $hh_mm    = $hh.":".$mm;
    EXIT_transform_min_to_std_min:
        $
    [2] = $hh_mm;
    } #ende transform_min_to_std_min

     

    I'll golf that:

    First, the ':method' part is unnecessary, and won't even be accepted by Perl 5.6.

    # this actually takes a number of seconds and formats it as hh:mm
    sub transform_min_to_std_min
    {
        my $m = int($[1]/60);
        $
    [2] = defined($_[1]) ? sprintf('%02d:%02d', int($m/60), $m%60) : undef;

    I would just like to note that THIS EDITOR SUCKS 



  • I'd be tempted to write the original as

    sub whatever {

        "0" x (7 - (log($_[1])/log(10))) . $_[1]

    But then, it would probably be easier on the maintainer to use printf instead...

    Also, what happened to the first argument? My best guess is that the call was going to contain the max number of digits, but that was scrapped and they didn't feel like changing the calls... :)

     



  • Ack. Do people here know about 'return'? And what's with the labels? :(



  • @fennec said:

    Ack. Do people here know about 'return'? And what's with the labels? :(

    'return' is for people obsessed with C. :)

    Those of use who write any or all of Perl, Ruby, Lisp, Haskell, ML, erlang or Perl are used to doing it the other way. Perl actually has some nice functional features to complement... Well, to complement the ultra-procedural features like all the things that operate on $_ by default.

    Furthermore, it's a performance defeat in Perl and Ruby to do "return" instead of a default last value in a tight loop. I've had to optimize this out in the Computer Language Shootout, in both languages.

    It always irks me that the cardinal demonstration of continuations in Scheme is to have a "return" statement by making the code twice as long. >:|

    But yeah, I would have if'd out that whole section rather than using GOTO. Some people will go to great lengths to use the end-of-statement form of 'if'.


  • To answer some of the previous posts regarding Perl.. 

    The first argument to a method in Perl is a reference to the receiver (self). That's why the code is operating on $_[1].

    The $_[1] = '...' thing is setting the value of the parameter 1 passed to the method.  This is how functions like chomp() work.

    For instance you can say:

        my $var = "some text\n";

        chomp( $var );

     And $var will be chomped afterwards because chomp operated on the reference. So for some reason the original author decided that was the way his methods would work.

    Of course nobody can explain the reason the code is so complex except the original author.. ;-) 



  • WTF indeed! Why didn't they do something like this? 

    num_16_edit(int num) { num=num_15_edit(num); if(num<1000000000000000) num='0'+num; return num; }

    num_15_edit(int num) { num=num_14_edit(num); if(num<100000000000000) num='0'+num; return num; }

    (I know, it should be in Perl, but Tom Christiansen was an asshole to me once and I swore I'd never write it again. That was about... hmm, 14 years ago. Geek grudges are so petty.)

     



  • Well, leaving out the weirdness with using the 2nd argument, there's the problem that the label doesn't exist for the goto, which I assume

    is a transcription error, it'w worth noting that this returns hh:mm where hh = number of hours, 0 filled, mm = hh * 60. The minutes

    which were in $mmRest, so return values of  "07:420" are expected results.

     



  • @quamaretto said:

    Also, what happened to the first argument? My best guess is that the call was going to contain the max number of digits, but that was scrapped and they didn't feel like changing the calls... :)

    The sub is to be called like a method. That is, the first argument will be the object on which it is called. That's how object orientation works in Perl 5...
     


Log in to reply