- 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
[code]
// Set Route string to 5 bytes first Redim ArrayElement[5]; StrLength := Len(Route); if StrLength > 0 then ArrayElement[1] := Route[1] else ArrayElement[1] := 'f'; if StrLength > 1 then ArrayElement[2] := Route[2] else ArrayElement[2] := 'r'; if StrLength > 2 then ArrayElement[3] := Route[3] else ArrayElement[3] := 'i'; if StrLength > 3 then ArrayElement[4] := Route[4] else ArrayElement[4] := 's'; if StrLength > 4 then ArrayElement[5] := Route[5] else ArrayElement[5] := 't'; [code]
Admin
Admin
In my first (no frist) programming class I was taught the concept of a loop. That was back in the 60's. I don't think the basic concepts have changed much, but the quality of the instruction seems to have changed quite a bit, as witnessed by code examples like this.
The sad part is that this "programmer" was probably a graduate of a "top tier" school (or his resume said so). Sad, very sad!
Admin
The truly brain-burning part is that they use a loop at the end to set each array element, but can't conceive of using a loop to fill the first "30 - Len(Route)" elements with spaces...
Admin
This is one of those situations where the code smells so bad, I'm not even going to inspect it further.
Admin
-1 for unwitting loop unrolling but +1 for orderly columnar goodness.
Admin
Oh my, his hurts.....
Why did the developer not stop after the first 5 lines or so and thought 'I wonder if there is a better way of doing this?'... I mean he could have used a loop, or gasp sprintf() or whatever that language has of a similar functionality....
I have heard people talking about code smell, but this ranks a code stink instead!, just think about want other 'gems' might lurk beneath the surface...
Yours Yazeran
Plan: To go to Mars one day with a hammer.
Admin
... which would have been okay, but you were drinking chamomile tea?
Admin
No I'm sorry I can't see anything wrong with this. It's formatted nice and neatly on the page and all the parameters are carefully checked for null (or whatever its equivalent is in this language which I can't place - it's not Java or Fortran or Cobol or Coral 66).
Aha! I get it! I know what the real WTF is! It's a zero based array! Am I right?
Admin
Okay then, we have a new word that we all learned on yesterday's WTF.
Code which has this quality of stink can be referred to as a Cleveland Steamer.
Admin
Due to the assigments with colon, it seems Pascal, but it uses Length(), not Len() and no Redim. Redim and Len exist in BASIC and Visual Basic, but they don't use colon for assignments... What is it?
Admin
Looks like someone's getting paid by the SLOC.
Admin
It appears to be Crystal Reports formulas (VB6, only worse)
Admin
Do you need a loop for string padding? I typically set a variable to a very long series of empty spaces, like 100 or so, then just do: Right(padVar & realVar, 30)
Admin
Everything except for the redim seems like Pascal / Delphi...
Honestly, this is really juvenile coding. I'm thinking 13 or younger.
I find it hard to believe that this is used in a production system. That's probably precisely why it is in one...
They most likely graduated a reporting person from report layout to basic coding responsibilities... After all, if the report works (eventually) then they do contribute.
My eyes are still hurting though, and I'm probably the one regular on this site that doesn't know it all.
Admin
Yeah, after 5 lines I would have started to use the clipboard.
Redim ArrayElement[30]; StrLength := Len(Route); if StrLength > 0 then ArrayElement[1] := Route[1] else ArrayElement[1] := ' '; if StrLength > 1 then ArrayElement[2] := Route[2] else ArrayElement[2] := ' '; if StrLength > 2 then ArrayElement[3] := Route[3] else ArrayElement[3] := ' '; if StrLength > 3 then ArrayElement[4] := Route[4] else ArrayElement[4] := ' '; if StrLength > 4 then ArrayElement[5] := Route[5] else ArrayElement[5] := ' ';
//well let's use the clipboard to optimize this Clipboard.SetText(" ");
if StrLength > 5 then ArrayElement[6] := Route[6] else ArrayElement[6] := Clipboard.GetText(); if StrLength > 6 then ArrayElement[7] := Route[7] else ArrayElement[7] := Clipboard.GetText(); if StrLength > 7 then ArrayElement[8] := Route[8] else ArrayElement[8] := Clipboard.GetText(); if StrLength > 8 then ArrayElement[9] := Route[9] else ArrayElement[9] := Clipboard.GetText(); if StrLength > 9 then ArrayElement[10] := Route[10] else ArrayElement[10] := Clipboard.GetText(); if StrLength > 10 then ArrayElement[11] := Route[11] else ArrayElement[11] := Clipboard.GetText(); if StrLength > 11 then ArrayElement[12] := Route[12] else ArrayElement[12] := Clipboard.GetText(); if StrLength > 12 then ArrayElement[13] := Route[13] else ArrayElement[13] := Clipboard.GetText(); if StrLength > 13 then ArrayElement[14] := Route[14] else ArrayElement[14] := Clipboard.GetText(); if StrLength > 14 then ArrayElement[15] := Route[15] else ArrayElement[15] := Clipboard.GetText(); if StrLength > 15 then ArrayElement[16] := Route[16] else ArrayElement[16] := Clipboard.GetText(); if StrLength > 16 then ArrayElement[17] := Route[17] else ArrayElement[17] := Clipboard.GetText(); if StrLength > 17 then ArrayElement[18] := Route[18] else ArrayElement[18] := Clipboard.GetText(); if StrLength > 18 then ArrayElement[19] := Route[19] else ArrayElement[19] := Clipboard.GetText(); if StrLength > 19 then ArrayElement[20] := Route[20] else ArrayElement[20] := Clipboard.GetText(); if StrLength > 20 then ArrayElement[21] := Route[21] else ArrayElement[21] := Clipboard.GetText(); if StrLength > 21 then ArrayElement[22] := Route[22] else ArrayElement[22] := Clipboard.GetText(); if StrLength > 22 then ArrayElement[23] := Route[23] else ArrayElement[23] := Clipboard.GetText(); if StrLength > 23 then ArrayElement[24] := Route[24] else ArrayElement[24] := Clipboard.GetText(); if StrLength > 24 then ArrayElement[25] := Route[25] else ArrayElement[25] := Clipboard.GetText(); if StrLength > 25 then ArrayElement[26] := Route[26] else ArrayElement[26] := Clipboard.GetText(); if StrLength > 26 then ArrayElement[27] := Route[27] else ArrayElement[27] := Clipboard.GetText(); if StrLength > 27 then ArrayElement[28] := Route[28] else ArrayElement[28] := Clipboard.GetText(); if StrLength > 28 then ArrayElement[29] := Route[29] else ArrayElement[29] := Clipboard.GetText(); if StrLength > 29 then ArrayElement[30] := Route[30] else ArrayElement[30] := Clipboard.GetText();
InsertString := ''; for i := 1 to 30 do InsertString := InsertString + ArrayElement[i];
I am not sure if there would be a cast problem, but if so, must be possible to inherit from Clipboard...
Admin
Shouldn't that be -30 (or 35?) for loop unrolling? The columns only count for +1 in total.
Admin
TRWTF is that a "real" programmer would have used recursion
(notice how he falls into the trap of "premature optimization" by storing the result of Len(Route) into a variable) __ /hg
Admin
So do it. Or you could build a string with 30-len(Route) spaces and concatenate this to Route. Though this appears to be right-padded, not left padded, so you might want to use Left instead.
Admin
Admin
Admin
Admin
And they should - if the above code causes a delay noticeable to anything but a finely tuned machine or a profiler, something is seriously wrong.
Admin
Ironically, depending on what we ate earlier that day, we might accidentally give you the Cleveland Steamer! Lovely!
Admin
Alternatively:
or,
I have to admit that I found this snippet in my own files while looking at this, which has to be a miniWTF in its own right.
Admin
Either I'm reading it wrong, or the loop at the end does nothing.
Admin
I don't see anything wrong with the code, that's how you do it on embedded systems without filesystems.
Admin
"Hey, why does this pad function lock up and allocate a bunch of memory when I give it a 40-character string?"
Admin
That's better. This one just stack faults when I call padMe("ABC",1).
Admin
Admin
Admin
padMe on the bottom? Come on Zooney, get the led out ...
Admin
Seriously, such copy/paste/modify coding techniques are not all that difficult if you have the right editor and know how to use it. For lots of people it's easier than thinking, "I wonder if there is a better way of doing this?"
I prefer Vim myself. Type the first line, then qwyyp3w^k4w^k5w^k5w^kq
Then @w as many times as you need. Or in this case, just 28@w
BTW, I've mapped the Ctrl-K key to the increment function.
Not that I would ever code that way.
Admin
THG said TRWTF is that a "real" programmer would have used recursion
A real programmer would have one-lined it to
(Real programmers also use .NET, because it lets them do that in one line, in this case.)
But what you said is true of a "real" programmer, certainly, since they'd rather write and test a recursive space-adder than just use something built in that pads a string...
Admin
Admin
This is quite a pattern: We could use it all sorts of ways:
//Add 5 to number a[0] = in; a[1] = 1; a[2] = 1; a[3] = 1; a[4] = 1; a[5] = 1; int out = 0; for (int i = 0; i < 6; i++) { out = out + a[i]; }
Think of the efficiency improvements!
Admin
This is almost a winner, but you forgot all the conditionals.
It should be more like "if result is less than 5, add one" then "if result is still less than 5, add another one"...
Admin
I'm a WTF generator, guys.
Admin
um, isnt this just
create all ' ' string (35 chars) copy existing string into beginning of all' 'string
or dim route as string dim maxchar as integer = 35 dim paddedroute as string [maxchar] ' do something if route > maxchar chars paddedroute = route for padme := len(route) to maxchar paddedroute[padme] = ' ' next padme
dunno, might contain an of-by-one-error or missing pragmas for that
Admin
Admin
and the missing: in ' do something if route > maxchar chars like 'missing: do something if route > maxchar chars
Admin
Admin
Admin
Str += " [35]spaces " // only one concat used. Str = Str.Left(35); //
Admin
Admin
"So, compilers sometimes do this neat trick called 'loop unwinding' to increase performance..."
"Only sometimes? Pff. I'll do it manually ALL the time!"
Admin
Maybe if the original poster check the code 2 months later, he would see the loop at the lower part got unwinded as well.
Admin
How can you be sure that your loop doesn't contain off-by-one error
Now back to formatting next 1000 lines for business report.
Admin
You are worse that the retard that wrote that code. It's sprintf() that you are looking for.
Admin
I don't know about that - I would never have done something like that when I was 13 (or younger) and I don't think anyone else my age would've, either. Then again, when I was a kid, we had books and magazines full of BASIC (and assembly, LOGO, and Pascal, to some degree) programming tutorials.