- Feature Articles
-
CodeSOD
- Most Recent Articles
- Mr Number
- intint
- Empty Reasoning
- Zero Competence
- One Month
- A Little Extra Padding
- Ready Xor Not
- A Set of Mistakes
-
Error'd
- Most Recent Articles
- Monkeys
- Killing Time
- Hypersensitive
- Infallabella
- Doubled Daniel
- It Figures
- Three Little Nyms
- Tangled Up In Blue
- 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
Worse, well let's see... I looked very and very hard... At least it's easy to read and understand, and it should work, even with arrays of lengths zero and one (I wonder what would happen if some strings are shorter than 10 though). And it follows Java naming standards. And he could have accepted filesVector.size() as parameter (or it would be improvement?).
WTF's I found:
Does not use standard functions
Function name makes no sense
3. And it is not static
Does not accept String[]
Vector, not ArrayList
Converts from Vector to String[] manually
Uses bubble sort, 4 times
Of which 3 can be omitted
Does not compare strings
A. Catches exceptions better to let go
B. In the most inner loop
C. And continues
Who noticed more?
An yes, can we just stop mentioning bubble sort anywhere? I mean, like conspiracy. Start explanation with Shell sort for instance? I mean none of these people invented it themselves, they read about it somewhere...
Admin
Ya know, I think he has actually twigged onto one of the least efficient variants of the bubblesort.
Damn this sucker REALLY takes me back, it has been litterally DECADES since I've seen a bubblesort in code.
Admin
I'm guessing it went something like this:
I suspect there was either no code review or the reviewers were equally clueless. Someone would/should have looked for a built-in sorting method.
Here are some of my general ramblings if a Sort method was not available in the language/environment:
Mea Culpa: I admit that I cheated in a Windows scripting environment by shelling "DIR /B /On > dirlist.txt" and then reading the contents of the dirlist.txt file, containing sorted filenames. It worked. I am lazy.
Admin
Is it just me? Or is it that Java tend to promote ugly coding? Most of the Java codes that I have seen people write ends up as being a huge mess of logic and formatting.
Admin
This whole thing reminds me of the sorters used in the pre-computer days of punch cards. The sorter would accept a set of punch cards, and put each of those cards into a separate bin depending on the value in a particular column. Then the operator would collect the cards form each bin in order, reload them into the feeder, and sort them by the previous column. This process would continue, sorting by each column of greater significance until finally the deck was sorted.
So if you wanted to sort on a 20 character name field, you'd need 20 passes through the sorter.
Admin
I admit that I cheated in a Windows scripting environment by shelling "DIR /B /On > dirlist.txt" and then reading the contents of the dirlist.txt file, containing sorted filenames. It worked. I am lazy.
Admin
You're a moron.
Sincerely,
Richard Nixon
Admin
You mean other than the file name to sort was broken up into 4 logical sub parts(yyyy, mm, dd, and hh)? Maybe an attempt at optimization? Group everything by matching sub parts? Smoked some crack that day?
Admin
nahh, its just poor coders. and its a fairly popular language. the more popular the language the more likely you'll find people coding in it that don't know what they are doing.
Admin
Now we have some recursion that 'The Specialist' could have used.
Admin
<font style="font-family: Arial;" size="3">len = filesArray.length - 1;
</font><font size="3">[snip some lame code]
for (i = 0; i < len; i++)
[snip some lame code]</font>
Admin
Sorry got it !!!! See the +1 on valueB
Admin
Is there any worse way? Why yes! Instead of four bubble sorts, he could have used four bogo sorts ;-)
Admin
I'm guessing that in his mind, he felt like he needed to sort four times because each substring of the YYYYMMDDHH string has a different cycle. HH goes from 00 to 23 (or 01 to 24), DD goes from 01 to 31, MM goes from 01 to 12, and YYYY increments. He probably didn't think about the fact that the value representing YYYYMMDDHH increments continuously as a whole.
Now, before flaming this post, I'm not justifying what he did. I'm simply trying to recreate his thought process...
[:|]
Admin
Me, me, me, ME!
The last time I coded with Java I was only able to use language features from 1.0.2 (or was it .8?) due to having to target several VMs for pocketPCs (You know, like the Beta version Sun released that was up to 10 times faster than any commercial option, but was quietly dropped....). "Cross platform Solution". Bollocks!! I wrote so much WTF code on that project that Ive never voluntarily touched Java since. It wouldnt suprise me to find out *this* WTF was due to having to target early JVMs.....
Anyways, now I get to create WTFs with XSLT and perl....lovely.
Admin
>>"What's your vector, Victor?"
Shirley you can't be serious????
Admin
and yet you managed to acknowledge him while telling him what a fool he is [^o)]
kinda weakens your staement a little don't cha think [8-)]
[;)]
Admin
<font size="2">don't you sort everything 4 times just to make sure it's properly sorted? I always do. I swear.</font>
<font size="2"></font>
Admin
Your statement incorrectly assumes the file system was NTFS.
Kudos to my fellow dinosaur, RevMike, who worked with tab equipment back in the good old days when users didn't have computers. IT professionals had a computer. A multi-field sort and an inability to compare multiple fields' data, requires you to sort the fields in 'bottom up' order. In this case, sorting each of the 'fields' in each of the four loops, in right-to-left sequence, would produce a correctly sorted list. If there is only one year (YYYY) value, the sorting can stop after the third pass (MM).
In my earlier comment, I stated that the entire filename numeric value could be contained in a long integer variable. This statement is true until the year 2148, which would exceed the upper limit of a signed long integer. You could use an unsigned long integer if the language supported it or you could subtract the first file's year value from all (string -> int) values. Or you could just leave it to future programmers, since you would likely to have passed to that great one-way hash in the sky. :-)
Admin
"Check our Clearance, Clarence"
Admin
Think about thread safety too. If there's no danger of "c" changing size in the middle of your operation, then you're fine. Otherwise, there's a chance that toArray(Object[]) will need to return a bigger array than the "a" you passed in, and it will allocate a new one. Because you've discarded the return value, you don't know this, and you're left thinking you've got the contents of the collection in your array when in fact you haven't. Likewise, "c" could shrink, in which case there will be some nulls at the end of the array. These might surprise you if you assume that "a.length" is the number of values returned from the collection.
For the sake of re-usability and refactoring, since the difference between the two cases is pretty negligible, personally I'd tend to use the thread-safe approach even where it's not needed - you have less chance of creating trouble for yourself later. But as long as your thread exclusively owns "c", it's not necessary.
Admin
"This is getting silly, Sally!"
Admin
Which makes you wonder whether he wrote it before Java 1.2 (the birth of Collections), which would excuse him for not using Collections.sort().
The beautiful wtf is it doesn't work - it's not even bubblesort.Assume the vector is filled with:
2005122315.ext, IamGonnaBleepYourCode.ext, 2004010101.ext, getThis.ext, 2006444444.ext
Because he's compairing neighbours only and will skip these due to his "errorhandling" the 4 elements above will not be sorted - not even the numerical ones.
I assume he tested the code - well, if he tested - with few elements based on a Vector filled with filenames he got from an already sorted list .. mmm... test data input on output form.
Admin
Your approach still is not thread safe, as the size of the container still can change between the internal array allocation and the array fill inside the toArray() method. Hey, it could even change during the internal copying operation, probably making the toArray() fail with an ArrayIndexOutOfBoundsException.
Admin
When I do something stupid like that, I'm well aware that I'm not doing it the best way, and put in comment why.
I was only recently introduced to a C++ compiler that was powerful enough to handle the standard template library. (We just drop OS/2 support this year, there are a lot of WTFs in our codebase to reflect the need to compile on a platform where most compilers have not been updated in 10 years)
Just after it happened I wrote a comment in my code "This is a O(N^2) algorithm because I'm too lazy to write my own hash table when is isn't expected to exceed 16 in the real world. Still the ideal solution would be a hash table". A couple months latter I realized that the above code could use stl::vector, but by then I had debugged it.
I guess my point is that a good programmer may in fact write awful code from time to time, but the good programmer will know he did so, and write a comment to explain why. Future maintainers will then know that why it is that way, and feel free to change to something else if there is a need.
Admin
s/std::vector/std::map/
Admin
"Roger, Roger."
Admin
Well, I guess you could nest the loops somehow so that it'd run in O(n^8) time and make it move the actual file contents around..
Admin
that's an easy one, the specialist thought that if he sorted by year first, then the second round (by month) would be much faster, then the third round (by day) faster and so on
after all he was the specialist, whatever he came up with, was the greatest invention
Admin
I guess O(n^2) wasn't bad enough for this guy...
Admin
Well, no on the last part. It's a Vector. Vectors are thread safe (and all versions of the toArray() method are synchronized)...so it can't change inside the method.
Then again (and going back to the WTF), it was 'fileVector' but contained Strings...so maybe it's not even a Vector after all. :P
Admin
"Barley-compilable" code could be Perl. You know... Pearl barley...?
I'll get me coat.
Admin
O(4 * n^2)
misquoting the bard: "Double, bubble, toil and code. Index flail and coder chode."
Admin
Luckily bubble sort is stable.
Admin
Come what come may, time and the hour sorted in a drunken haze.
Admin
You make good points. However, if thread safety is an issue, It would probably be wise to get an sychronized lock on "c" before creating the array, and executing the toArray method. Not every class in the Collections api is thread safe.
Admin
The sorters that I saw needed two passes to sort a column alphabetically. Ah, the joys of the radix sort.
Sincerely,
Gene Wirchenko
Admin
I'm quite serious. And don't call me Shirley.
Admin
Oh, come on. Fess up to it. You know you're just jealous. You wanted to be first.
And the [image] goes to dshiznit!
Admin
Don't be to hard on him. His father's sperm was "first"
Admin
Visual Basic being the world's worst offender.
Admin
I like the Quinnery sort.
Admin
This will break down in the year 10000.
Admin
O(4*n^2) = O(n^2).
Admin
I never thought I'd ever see someone admit they've done that.
<too loudly>well, I have never done that in my career!</too>
Admin
I think the point of the original comment was that even O(n^2) wasn't bad enough, so they had to increase the coefficient too!
Actually, the bit that gets me the most is that in the sort (all four instances) the coder uses a while loop to implement a for loop, and then immediately inside uses a for loop!
Why not just use:
// [[Some variable declarations]]
for (len = filesArray.length - 1; len > 0; len--)
for (i = 0; i < len; i++) {
// [[Loop body]]
}
}
? It's what the for loop was designed for, after all!
Makes me wonder if the inside is a cut&paste job from a textbook sorting algorithm (and that the coder didn't understand for loops). It'd certainly explain the choice of algorithm.
Admin
Bah, the less-than sign triggered HTML issues. You get the idea.
-FM
Admin
I'm not sure that it is the fault of either the IDE or language. VB made Windows programming something that hobbyists could do. With no formal CompSci education or mentoring, it isn't any wonder that bad code was borrowed from public examples with no regard to appropriateness.
Reminds me of a great joke (circa 1999) about Bill Gates being revived in the year 9998 from his cryogenic slumber. The heads of the world body was so glad to have him on hand to help them avert the looming cyber crisis, since he'd survived the Y2K crisis. Bill asked, in wonder, what possible crisis could exist in such a mature (by this time) field as IT. The world leaders replied, the "Y10K crisis, of course!"
Sometimes, you've got to do whatever will produce the next version of the program for your client to see, with little regard for what other IT professionals will think of it. Such is the world of RAD thinking.
Is there nothing you've done for which you aren't (now) proud?
I don't think that's all the coder didn't understand :-)
Admin
its simple, he didn't understand that the whole POINT of arranging dates like that was to make them easy to sort so he did the sort by breaking the date up and dealing with each part seperately!
Admin
oops didn't notice the second page that reply was to the last post on the first page.