Comment On Maximum Pad

Brian's company needs to track financial information indexed by 100 digit routing numbers. Now, obviously, not all of those digits are significant, so if a user enters "123", the application needs to be smart enough to pad out the other 97 digits with leading zeros. Sane people might think this should be implemented as a one-line call to a built-in method. The more DIY among us might waste time building up a for loop. And, of course, a LISP fan would simply torture future coders with recursion and parentheses. [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4 | Page 5Next »

Re: Maximum Pad

2010-06-16 09:02 • by glyph (unregistered)
I like how he divided the long string of zeroes into several smaller ones for readability.

Re: Maximum Pad

2010-06-16 09:02 • by Alargule (unregistered)
Onehundredth!

Re: Maximum Pad

2010-06-16 09:02 • by SPiT (unregistered)
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001th

Re: Maximum Pad

2010-06-16 09:02 • by James O'Meara (unregistered)
0

Re: Maximum Pad

2010-06-16 09:03 • by Jayman
311824 in reply to 311820
glyph:
I like how he divided the long string of zeroes into several smaller ones for readability.


Yeah, and he even politely added in differing length constant strings...

Re: Maximum Pad

2010-06-16 09:06 • by Brian (unregistered)
Oddly enough, we do the same thing at our company. At least Routing and Transit numbers are only 9 digits long...

Re: Maximum Pad

2010-06-16 09:07 • by Rusty (unregistered)
Heh...we have to do this all the time in our app.

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

Captcha: Valetudo
Someone so smart they were Valedictorian and Salutatorian?

Re: Maximum Pad

2010-06-16 09:08 • by Buggz
I like how the first parameter always calculates to zero.

Re: Maximum Pad

2010-06-16 09:09 • by трпаьтонр (unregistered)
It's performance optimisation

Re: Maximum Pad

2010-06-16 09:11 • by Snarf (unregistered)
I hope this was computer-generated code.

Re: Maximum Pad

2010-06-16 09:11 • by frits
I don't know which is the more entertaining wtf: allowing strings longer than 100 characters, or calling substring with an index of 0 and length equal to the string's total length.

Re: Maximum Pad

2010-06-16 09:13 • by ParkinT
Now that's a Maxi-Pad!
And it appears to contain NO LEAKS.

Re: Maximum Pad

2010-06-16 09:14 • by akatherder
311832 in reply to 311826
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.

Re: Maximum Pad

2010-06-16 09:15 • by Mateus (unregistered)
311833 in reply to 311820
glyph:
I like how he divided the long string of zeroes into several smaller ones for readability.


really clever indeed!

Re: Maximum Pad

2010-06-16 09:15 • by Some guy (unregistered)
311834 in reply to 311831
ParkinT:
Now that's a Maxi-Pad!
And it appears to contain NO LEAKS.


The rest of us were hoping no one would make that joke. Thanks, you've ruined the Internet for the rest of us, buddy.

Re: Maximum Pad

2010-06-16 09:16 • by monkeyPushButton (unregistered)
311835 in reply to 311827
He doesn't even validate that the data is a valid routing number.

Assuming the routing number is only supposed to have digits should be:

if (length == 99) && (Data == "1") {
NewData = "0" + Data.substring(length-99,length);
}
if (length == 99) && (Data == "2") {
NewData = "0" + Data.substring(length-99,length);
}
if (length == 99) && (Data == "3") {
NewData = "0" + Data.substring(length-99,length);
}

/*snip*/

if (length == 98) && (Data =="10") {
NewData = "00" + Data.substring(length-98,length);
}
if (length == 98) && (Data =="11") {
NewData = "00" + Data.substring(length-98,length);
}

Re: Maximum Pad

2010-06-16 09:22 • by akatherder
311836 in reply to 311828
трпаьтонр:
It's performance optimisation


Especially since it has to run through all of the if-statements every single time. Why stop when you have your answer? Let's keep checking!

Re: Maximum Pad

2010-06-16 09:22 • by fragilist (unregistered)
He obviously used TDD, and just made the tests pass, one by one.

Re: Maximum Pad

2010-06-16 09:23 • by snoofle
311838 in reply to 311829
Snarf:
I hope this was computer-generated code.
What self-respecting computer would generate code like this?

Re: Maximum Pad

2010-06-16 09:23 • by Matt (unregistered)
Somebody's never heard of the switch statement.

CAPTCHA: "secundum" (not firstum)

Re: Maximum Pad

2010-06-16 09:24 • by Zecc
311840 in reply to 311826
I like the syntax highlighter. That's new, isn't it?
Rusty:
Captcha: Valetudo
Someone so smart they were Valedictorian and Salutatorian?
"Vale tudo" means something like "everything goes"/"everything's fair" in Portuguese.

Re: Maximum Pad

2010-06-16 09:24 • by mornak
Why did he even bother to do the substring? It's always from 0 to length!

Re: Maximum Pad

2010-06-16 09:26 • by Pigeon
Pay per line FTW!

Re: Maximum Pad

2010-06-16 09:26 • by Jaime
311843 in reply to 311832
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.

Re: Maximum Pad

2010-06-16 09:26 • by Drew (unregistered)
Clearly it should've been this...

var oneHundredZeroes = "0000....0000";
var length = Data.length;

return oneHundredZeroes.substring(100-length) + data;

/sarcasm

Re: Maximum Pad

2010-06-16 09:27 • by Anon (unregistered)
public String rateThisCode() {
List<String> comment = new ArrayList<String>();
int length = 3;

while (comment.size()< length) {
if (comment.size() == length-1) {
comment.add("Fail");
}
if (comment.size() == length-2) {
comment.add("Than");
}
if (comment.size() == length-3) {
comment.add("Worse");
}
}
return comment.toString();
}

Re: Maximum Pad

2010-06-16 09:27 • by Mark Bowytz
311846 in reply to 311840
Zecc:
I like the syntax highlighter. That's new, isn't it?.


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

Re: Maximum Pad

2010-06-16 09:27 • by Pete (unregistered)
311847 in reply to 311826
Rusty:
Heh...we have to do this all the time in our app.

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

Captcha: Valetudo
Someone so smart they were Valedictorian and Salutatorian?


Take it that's from SQL I think that's ok if you need to do it in DB.

Re: Maximum Pad

2010-06-16 09:28 • by Prosthetic Lips
311848 in reply to 311820
glyph:
I like how he divided the long string of zeroes into several smaller ones for readability.


Especially since the substrings are not a consistent length. Am I the only that actually counted? In case you haven't counted and wondered, the substring lengths are:
25, 17,
16, 16
25

Now that's just wrong!

Re: Maximum Pad

2010-06-16 09:31 • by chron3
And here I thought today had started out pretty well...

No bad traffic on the way to work, the latest software update build passed through QA with nary a hitch, no nasty-grams from clients or managers waiting in the inbox, then I read today's WTF...

and now I am filled with despair...

Re: Maximum Pad

2010-06-16 09:35 • by Remy Porter
311850 in reply to 311840

Re: Maximum Pad

2010-06-16 09:37 • by Remy Porter
311851 in reply to 311849
"I have become death, destroyer of rather pleasant mornings."

Re: Maximum Pad

2010-06-16 09:37 • by baka0815 (unregistered)
why noat just add "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" as a suffix and then do a substring? :)

Re: Maximum Pad

2010-06-16 09:39 • by baka0815 (unregistered)
And using Java you would surely do something like

public String fill(String data) {
return (data.length() > 100 ? data.substring(100) : data.length() = 99 ? "0" + data : data.length() = 98 ? "00" + data ...
}

Re: Maximum Pad

2010-06-16 09:40 • by sam (unregistered)
At least use "else if" for Christ's sake.

Re: Maximum Pad

2010-06-16 09:44 • by @Deprecated
311855 in reply to 311846
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 some matches...

Re: Maximum Pad

2010-06-16 09:45 • by Ben L. (unregistered)
311856 in reply to 311850
Remy Porter:

And you didn't see the huge note at the top of the page? Look again more carefully.

Re: Maximum Pad

2010-06-16 09:46 • by Anon (unregistered)
311857 in reply to 311827
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?

Re: Maximum Pad

2010-06-16 09:47 • by kikito (unregistered)
It's easily understantable. That's the good part.

It gives me killing insticts. That's the bad part.

Re: Maximum Pad

2010-06-16 09:47 • by Wade (unregistered)
311859 in reply to 311838
Wow, and amazingly it wasn't written in VB. I guess you can code poorly in other languages as well.

Re: Maximum Pad

2010-06-16 09:47 • by Botia (unregistered)
311860 in reply to 311838
Any self-respecting computer would properly handle a length of 0.

Re: Maximum Pad

2010-06-16 09:47 • by Anonymous (unregistered)
That's a great WTF but my immediate thought on reading this article was that the real WTF is surely the fact that TDWTF has only just implemented syntax highlighting after all these years! Good job Remy, and welcome to the 21st century! Did you know they have talking picture boxes these days, too?

Re: Maximum Pad

2010-06-16 09:51 • by Uncle Al (unregistered)
Breaking the 0's down into substrings means the coder was oh-so-close to a brilliant solution. What he should have done, of course, was to compute the length of the padding and then treat the length as a bit field. If the 1's digit is set, add "0". If the 2's digit is set, add "00". And so on. Much more scalable, don't you think? :-)

Re: Maximum Pad

2010-06-16 09:51 • by toth
311863 in reply to 311854
sam:
At least use "else if" for Christ's sake.


Fuck Christ, how about subsequent maintainers?

Re: Maximum Pad

2010-06-16 09:54 • by NoAstronomer (unregistered)
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.

Re: Maximum Pad

2010-06-16 09:56 • by Remy Porter
311865 in reply to 311856
Yes, I did, but after I had already grabbed this and was getting it setup. I'll probably get it upgraded shortlyish.

All things considered, it's still a huge step up from pasting code into Pastie.org, viewing source, and copying the HTML. Which I've done. I've also got a Ruby script that generates HTML too.

This is much nicer.

Re: Maximum Pad

2010-06-16 09:59 • by madmaxpou (unregistered)
311866 in reply to 311844
Drew:
Clearly it should've been this...

var oneHundredZeroes = "0000....0000";
var length = Data.length;

return oneHundredZeroes.substring(100-length) + data;

/sarcasm

or

string oneHundredZeros = new string('0', 100);
return (oneHundredZeros + data).Substring(0, 100);

or better yet

return data.PadLeft(100, '0');

Re: Maximum Pad

2010-06-16 09:59 • by Medinoc (unregistered)
Like Buggz said, the "length-XX" as a first parameter to substring() makes it even more WTF-y.

Re: Maximum Pad

2010-06-16 10:02 • by Brent (unregistered)
311868 in reply to 311843
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--);

Re: Maximum Pad

2010-06-16 10:07 • by apaq11
I feel like this would have been better if he had gotten the Database involved somehow....
« PrevPage 1 | Page 2 | Page 3 | Page 4 | Page 5Next »

Add Comment