- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Rainbows just appeared on the page. NICE! Leave the page up for a while... you should get rainbows too.
Only thing missing are unicorns!
Admin
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.
Admin
Perfect, this was just what I was looking for.
Admin
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.
Admin
Where's the joke about embedded systems?
Admin
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?
Admin
HAHAHAHAHAHAH
You win sarcasm
Admin
Admin
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.
Admin
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?
Admin
That's the least of the problems with this code.
Admin
Nah, the rwtf is using a language which requires more than one line to do the padding.... whatever happened to sprintf?
Admin
How is this not the featured quote?
Admin
Wouldn't it be easier to maintain if he had written it like this?
That's how I would have done it...
Admin
Comment win.
CAPTCHA: dolor, dull colours
Admin
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]
Admin
I must agree, the function works flawlessly, with no leaks, let's admit this!
Admin
Admin
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.
Admin
Cool. Dynamically creating a format string. Have to be careful in C with buffer overflow or handling negative widths.
Admin
I would have done this in COBOL. Perfect for that!
Admin
Here, I fixed it (except number of zeroes). For an extra bonus, it now takes the length at least seven times (instead of once).
Admin
new_data = (100-length)*' ' + data
Admin
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!
Admin
Admin
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:
Or even the more esoteric:
Admin
This could be improved by using "else if".
Admin
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.
Admin
With a little effort, you can write a VB program in any language.
Admin
Good God, it's hideous!
Admin
This should fix it:
Admin
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.
Admin
Admin
You should never hard-code numbers. What if the value of 0 changes in a future release?
Admin
Admin
Admin
Maybe he wrote a program to generated that code. ;0)
Admin
I love all the calls to Data.substring that do nothing.
Admin
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.
Admin
Admin
Depends on how many neutrinos there are, I suppose.
Here's a solution, javascripty but you get the picture:
Admin
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.
Admin
Sure. If the programmer wanted to achieve the world record for indenting depth and didn't have a code obfuscator handy.
Admin
this is by far my favorite part:
The rest, as egregiously bad as it is, at least does something.
Edit: Except, of course, for all the substring calls that do nothing
Admin
Ironically, "SNIPPED FOR REALLY LONG AND REPETITIVE CODE" was also the final entry his this developer's employee file, also.
Admin
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.
Admin
Obvious solution:
"SELECT FROM paddedStringTable where length = " + data.Length
Then concatenate the result with data.
Admin
Admin
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.
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.
Admin
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.