• Mike (unregistered)

    Rainbows just appeared on the page. NICE! Leave the page up for a while... you should get rainbows too.

    Only thing missing are unicorns!

  • Blue Collar Astronaut (unregistered) in reply to fragilist
    fragilist:
    He obviously used TDD, and just made the tests pass, one by one.

    I once worked with a co-worker turned contractor who created a "Julian Date" converter method using "TDD" principles, and the results were less than stellar (I put quotes around both of those terms deliberately becase I don't think he knew what either of them were).

    By Julian Date, he meant a date represented by the three digit day of year followed by the four digit year, and he spent somewhere around two weeks creating this thing (complete with unit tests! he boasted) before I took a look at it.

    Sure enough, it did have a slew of tests associated with it, but none that covered anything prior to the 100th day of the year. So I tried to gently prod him in the right direction by suggesting that he test something with some earlier dates. A week later, he had updated his code with something like this:

    if (dayOfYear.Length == 2) dayOfYear = "0" + dayOfYear;

    I almost felt bad suggesting that maybe, just maybe, he add a test for January 1...just to make absolute sure everything worked as expected.

    All that to say, I think Brian is a lucky guy to work with someone so thorough.

  • Zachary (unregistered)

    Perfect, this was just what I was looking for.

  • (cs) in reply to Mike

    You have to click on something to make that happen. I won't say what. And if you click in the right place enough times, you will get unicorns.

  • (cs)

    Where's the joke about embedded systems?

  • David Robinson (unregistered)

    I'd say the real WTF is using a googol (10^100) as a tracking number. What are they tracking? All the subatomic particles in the visible universe?

  • SR (unregistered) in reply to Matt
    Matt:
    Somebody's never heard of the switch statement.

    HAHAHAHAHAHAH

    You win sarcasm

  • Robyrt (unregistered) in reply to David Robinson
    David Robinson:
    I'd say the real WTF is using a googol (10^100) as a tracking number. What are they tracking? All the subatomic particles in the visible universe?
    The 100-digit routing number is probably a concatenation of several smaller numbers, say ten 10-digit identifiers with only the last one being mandatory.
  • Someone (unregistered) in reply to ParkinT

    Unless the user can enter an empty string.

    By the way, writing length-99 etc as first argument of substring() instead of 0 is my favorite twist.

  • Menno (unregistered)

    I like the fact that even while he checks the length: if(length==99) (for example) he still does a substr(length-99, length) xD Like wtf?

  • DeepThought (unregistered) in reply to Matt
    Matt:
    Somebody's never heard of the switch statement.

    That's the least of the problems with this code.

  • Matt Williams (unregistered)

    Nah, the rwtf is using a language which requires more than one line to do the padding.... whatever happened to sprintf?

  • jrh (unregistered) in reply to NoAstronomer
    NoAstronomer:
    What I like about this one is that you just know one (at least) of those pads has the wrong number of zeros in it. Leading to a beautifully hard to diagnose intermittent error.

    How is this not the featured quote?

  • jrh (unregistered)

    Wouldn't it be easier to maintain if he had written it like this?

       var length = Data.length;  
       var NewData;  
       if (length = 1) {  
          NewData = "0" = Data;  
       }  
       if (length == 2) {  
          NewData = "0" = Data;  
       }  
       if (length == 3) {  
          NewData = "0" = Data;  
       }  
       /* 
        ... 
        SNIPPED FOR REALLY LONG AND REPETITIVE CODE 
        ... 
       */  
       if (length == 98) {  
          NewData = "0" = Data;  
       }  
       if (length == 99) {  
          NewData = "0" = Data;  
       }  
       return NewData; 
    

    That's how I would have done it...

  • BSDPwns (unregistered) in reply to Matt Williams
    Matt Williams:
    Nah, the rwtf is using a language which requires more than one line to do the padding.... whatever happened to sprintf?

    Comment win.

    CAPTCHA: dolor, dull colours

  • jes (unregistered)

    what about using the library functions? Perl, but works simialrly in C or Python

    [code]

    sub pad { my ($n, $w) = @_; my $format = sprintf("%%%d.%dd", $w, $w); return sprintf ($format, $n); }

    printf "%s\n", pad(123, 50); 00000000000000000000000000000000000000000000000123 [code]

  • Rodrigo (unregistered) in reply to ParkinT

    I must agree, the function works flawlessly, with no leaks, let's admit this!

  • (cs) in reply to Brent
    Brent:
    Jaime:
    akatherder:
    Rusty:
    Heh...we have to do this all the time in our app.

    RIGHT('0000000...(one hundred zeros)' + userInput, 100)

    Under the circumstances, that's a clever way to deal with it.

    Nah, the clever way is ...

    REPLICATE('0', 99 - CAST((LOG(userInput)/LOG(10)) AS int)) + CONVERT(varchar(100), userInput)

    However, clever is a derogatory term in programming.

    A clever perl way:

    $new_data = '0' x 100; $new_data++ until (!$data--);

    But yours doesn't have the added benefit of improperly padding about 0.001% of the time. Mine woks on 999 and 1001, but fails on 1000. However, this is not a formula bug, since it works on 10, 100, and 10000. I think it's a LOG lookup table rounding anomaly in SQL Server.

  • (cs)

    After reading the front page summary, I pretty much figured that it would be a hundred item long if statement. Guess I know this site too well.

  • Buddy (unregistered) in reply to jes
    jes:
    what about using the library functions? Perl, but works simialrly in C or Python

    [code]

    sub pad { my ($n, $w) = @_; my $format = sprintf("%%%d.%dd", $w, $w); return sprintf ($format, $n); }

    printf "%s\n", pad(123, 50); 00000000000000000000000000000000000000000000000123 [code]

    Cool. Dynamically creating a format string. Have to be careful in C with buffer overflow or handling negative widths.

  • Mike (unregistered)

    I would have done this in COBOL. Perfect for that!

  • Jim (unregistered)

    Here, I fixed it (except number of zeroes). For an extra bonus, it now takes the length at least seven times (instead of once).

    function ZeroFill(Data) {   
           var NewData = Data;   
           if (NewData.length <= 100 - 64) {   
              NewData = "000000000000000000000000" + "00000000000000000" +    
                  "00000000000000000" +  "00000000000000000" +  
                  "0000000000000000000000000" + NewData.substring(NewData.length-64,NewData.length);   
           }   
           /* STILL SNIPPED, BUT NOW MUCH SHORTER! */
           if (NewData.length <= 100 - 2) {   
              NewData = "00" + NewData.substring(NewData.length-2,NewData.length);   
           }   
           if (NewData.length <= 100 - 1) {   
              NewData = "0" + NewData.substring(NewData.length-1,NewData.length);   
           }   
           return NewData;   
    }  
    
  • Dave-Sir (unregistered) in reply to Brent
    Brent:
    Jaime:
    akatherder:
    Rusty:
    Heh...we have to do this all the time in our app.

    RIGHT('0000000...(one hundred zeros)' + userInput, 100)

    Under the circumstances, that's a clever way to deal with it.

    Nah, the clever way is ...

    REPLICATE('0', 99 - CAST((LOG(userInput)/LOG(10)) AS int)) + CONVERT(varchar(100), userInput)

    However, clever is a derogatory term in programming.

    A clever perl way:

    $new_data = '0' x 100; $new_data++ until (!$data--);

    An obvious Python way (error handling elided for clarity):

    new_data = (100-length)*' ' + data

  • Ford (unregistered)

    You know what's the best part (besides the carefully typed out 0s)? That they used "substring" at all. I can't see any reason, every time they called it they used the entire string!

  • (cs) in reply to Dave-Sir
    Dave-Sir:
    Brent:
    Jaime:
    akatherder:
    Rusty:
    Heh...we have to do this all the time in our app.

    RIGHT('0000000...(one hundred zeros)' + userInput, 100)

    Under the circumstances, that's a clever way to deal with it.

    Nah, the clever way is ...

    REPLICATE('0', 99 - CAST((LOG(userInput)/LOG(10)) AS int)) + CONVERT(varchar(100), userInput)

    However, clever is a derogatory term in programming.

    A clever perl way:

    $new_data = '0' x 100; $new_data++ until (!$data--);

    An obvious Python way (error handling elided for clarity):

    new_data = (100-length)*' ' + data

    You guys are using the wrong definition of clever. My code is far more WTF worthy than even the orginal article due to the insidiousness of the bug. Clever is a bad thing, I think the quote is "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."

  • (cs)

    Obviously, this person either a) gets paid by LOC or b) works for BP or c) Gets paid by LOC AND works for BP. Because writing 400 lines of code is much more profitable than writing 1:

    NewData = String.format("{0,-100:s0}",Data);

    Or even the more esoteric:

    string newData = "";
    for(int i=0; i<( 100 - data.Length; i++ )
    {
      newData += '0';
    }
    newData += data;
    
  • Hector Virgen (unregistered)

    This could be improved by using "else if".

  • bluebearr (unregistered)

    Here's what I think happened: Someone had this done in a nice For loop, but then they (or someone else) heard that For loops affect performance, so they wrote it out. Actually, the fact that the zero strings are different lengths argues, I think, for some pissed off programmer being told that they had to do this despite their objections.

  • Jay (unregistered) in reply to Wade
    Wade:
    Wow, and amazingly it wasn't written in VB. I guess you can code poorly in other languages as well.

    With a little effort, you can write a VB program in any language.

  • Patrick (unregistered)

    Good God, it's hideous!

  • refoveo (unregistered)

    This should fix it:

    function ZeroFill(Data) {
           var length = Data.length;
           var NewData;
           var Zero = "0"
           if (length >= 100) {
              NewData = Data.substring(0,length);
           }
           if (length == 99) {
              NewData = Zero + Data.substring(length-99,length);
           }
           if (length == 98) {
              NewData = Zero + Zero + Data.substring(length-98,length);
           }
           if (length == 97) {
              NewData = Zero + Zero + Zero + Data.substring(length-97,length);
           }
           /*
            ...
            SNIPPED FOR REALLY LONG AND REPETITIVE CODE
            ...
           */
           if (length == 2) {
              NewData = 
              Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + 
              Zero + Zero + Zero + Zero + Zero + Zero + Zero + 
              Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + 
              Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + 
              Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + 
              Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + 
              Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + 
              Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + 
              Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + 
              Zero + Zero + Data.substring(length-2,length);
           }
           if (length == 1) {
              NewData = 
              Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + 
              Zero + Zero + Zero + Zero + Zero + Zero + Zero + 
              Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + 
              Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + 
              Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + 
              Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + 
              Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + 
              Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + 
              Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + Zero + 
              Zero + Zero + Zero + Data.substring(length-1,length);
           }
           return NewData;
    }
  • Jay (unregistered) in reply to Indrora
    Indrora:
    Obviously, this person either a) gets paid by LOC or b) works for BP or c) Gets paid by LOC AND works for BP. Because writing 400 lines of code is much more profitable than writing 1:
    NewData = String.format("{0,-100:s0}",Data);

    Or even the more esoteric:

    string newData = "";
    for(int i=0; i<( 100 - data.Length; i++ )
    {
      newData += '0';
    }
    newData += data;
    

    I once made fun of a programmer for using a loop like that to pad his strings one character at a time rather than use a substring. Now I feel that I wronged the poor guy. Apparently there are much worse alternatives.

  • odio (unregistered) in reply to Wade
    Wade:
    Wow, and amazingly it wasn't written in VB. I guess you can code poorly in other languages as well.
    I don't care much for VB, but JavaScript is definitely worse.
  • Jay (unregistered) in reply to Anon
    Anon:
    Buggz:
    I like how the first parameter always calculates to zero.

    That's called true zero. Clearly you can't trust the computer to provide true zero itself, you have to calculate it each time. Just in case. If you put a constant 0 in your code, who knows what the compiler will do with it?

    You should never hard-code numbers. What if the value of 0 changes in a future release?

  • opto (unregistered) in reply to Jaime
    Jaime:
    akatherder:
    Rusty:
    Heh...we have to do this all the time in our app.

    RIGHT('0000000...(one hundred zeros)' + userInput, 100)

    Under the circumstances, that's a clever way to deal with it.

    Nah, the clever way is ...

    REPLICATE('0', 99 - CAST((LOG(userInput)/LOG(10)) AS int)) + CONVERT(varchar(100), userInput)

    However, clever is a derogatory term in programming.

    Your clever method looks good to me. Now do it in JavaScript.

  • fjf (unregistered) in reply to jes
    jes:
    what about using the library functions? Perl, but works simialrly in C or Python

    [code]

    sub pad { my ($n, $w) = @_; my $format = sprintf("%%%d.%dd", $w, $w); return sprintf ($format, $n); } [code]

    Doesn't Perl support "sprintf ("%0*d", $w, $n)"? (Not that a variable width is required in the original problem in the first place ...)
  • Andy Brice (unregistered)

    Maybe he wrote a program to generated that code. ;0)

  • Philipp (unregistered)

    I love all the calls to Data.substring that do nothing.

  • Larry++ (unregistered)

    No worries, the compiler will optimize it all down to a simple, elegant algorithm. Basically we've reached the point where computers can program themselves and developers need merely give a vague shrug in the general direction of the goal.

  • fjf (unregistered) in reply to jrh
    jrh:
    Wouldn't it be easier to maintain if he had written it like this?
       var length = Data.length;  
       var NewData;  
       if (length = 1) {  
          NewData = "0" = Data;  
       }  
       if (length == 2) {  
          NewData = "0" = Data;  
       }  
       if (length == 3) {  
          NewData = "0" = Data;  
       }  
       /* 
        ... 
        SNIPPED FOR REALLY LONG AND REPETITIVE CODE 
        ... 
       */  
       if (length == 98) {  
          NewData = "0" = Data;  
       }  
       if (length == 99) {  
          NewData = "0" = Data;  
       }  
       return NewData; 
    

    That's how I would have done it...

    At least the original code works (module NoAstronomer's comment). In your code I count 4 different errors at first glance, and if it worked as I suppose it's meant to, it would be vastly less efficient than the original code (copying the string up to 99 times).

  • Gary (unregistered) in reply to Robyrt
    Robyrt:
    David Robinson:
    I'd say the real WTF is using a googol (10^100) as a tracking number. What are they tracking? All the subatomic particles in the visible universe?
    The 100-digit routing number is probably a concatenation of several smaller numbers, say ten 10-digit identifiers with only the last one being mandatory.

    Depends on how many neutrinos there are, I suppose.

    Here's a solution, javascripty but you get the picture:

    
    function pad(input) {
       var inp=input+"";
       var out=[];
       for (i=0;i<100;i++) out.push((i<inp.length) ? inp.charAt(inp.length-1-i) : "0");
       return out.reverse().join("");
    }
    </pre>
    
  • Anon (unregistered) in reply to jrh
    jrh:
    Wouldn't it be easier to maintain if he had written it like this?
       var length = Data.length;  
       var NewData;  
       if (length = 1) {  
          NewData = "0" = Data;  
       }  
       if (length == 2) {  
          NewData = "0" = Data;  
       }  
       if (length == 3) {  
          NewData = "0" = Data;  
       }  
       /* 
        ... 
        SNIPPED FOR REALLY LONG AND REPETITIVE CODE 
        ... 
       */  
       if (length == 98) {  
          NewData = "0" = Data;  
       }  
       if (length == 99) {  
          NewData = "0" = Data;  
       }  
       return NewData; 
    

    That's how I would have done it...

    Ignoring what I assume is a typo (should be NewData = "0" + Data;). This is brillant. It keeps most of the WTF from the original and yet manages to make it less efficient.

  • JohnFx (unregistered) in reply to Hector Virgen
    Hector Virgen:
    This could be improved by using "else if".

    Sure. If the programmer wanted to achieve the world record for indenting depth and didn't have a code obfuscator handy.

  • (cs)

    this is by far my favorite part:

    if (length >= 100) {   
              NewData = Data.substring(0,length);   
           }
    

    The rest, as egregiously bad as it is, at least does something.

    Edit: Except, of course, for all the substring calls that do nothing

  • JohnFx (unregistered)

    Ironically, "SNIPPED FOR REALLY LONG AND REPETITIVE CODE" was also the final entry his this developer's employee file, also.

  • Jeff (unregistered)

    Don't be so judgmental. It works, and that's what matters. Although a forward thinking programmer would have passed in a parameter for the target string length, in case it ever changes, then added a few "if" statements to handle those possibilities as well.

    You could probably even use some kind of recursion here, if for no other reason than recursion is cool when you get it right. For example, if the desired output length is 120, you parse off the "1", realize that means "100", call the existing code to pad up to 100, then use a case statement to "goto" the part in the code that will pad another 20 digits. Simple, really.

  • Anon (unregistered)

    Obvious solution:

    "SELECT FROM paddedStringTable where length = " + data.Length

    Then concatenate the result with data.

  • OldCoder (unregistered) in reply to @Deprecated
    @Deprecated:
    Mark Bowytz:
    Zecc:
    I like the syntax highlighter. That's new, isn't it?.

    Yes - big kudos go out to Remy for suggesting and implementing.

    Awesome! It has a print feature, so I can print out TDWTF code! Now all I need is a wooden table...

    Fixed that for you...

  • re:me (unregistered) in reply to Robyrt
    Robyrt:
    David Robinson:
    I'd say the real WTF is using a googol (10^100) as a tracking number. What are they tracking? All the subatomic particles in the visible universe?
    The 100-digit routing number is probably a concatenation of several smaller numbers, say ten 10-digit identifiers with only the last one being mandatory.

    I was thinking the same thing, but perhaps 5 numbers of different lengths, given the sections that add the different length strings of 0s as filler.

    apaq11:
    I feel like this would have been better if he had gotten the Database involved somehow....

    Where do you think the resulting 100 character string is headed? On the DB side he parses it into sections but he simplified it by passing only one parameter.

  • Brian White (unregistered) in reply to Jay

    I don't know what you are talking about. The DWTF is not how you would right this in VB. The very first one line example answer works perfectly well in VB/VBA/VBScript. There is a Right function in VB languages.

    Right("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" & "12345", 100) works fine.

Leave a comment on “Maximum Pad”

Log In or post as a guest

Replying to comment #:

« Return to Article