- 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
This is close to how one would do this in more lower level languages such as C. So whoever made this probably recently started using Java and used his experiences from previous jobs . Sure it could have been done more efficient, but it works. So no real WTF here I say.
Admin
Even if the library didn't offer methods for splitting strings, copying array contents, or building a growing list, there's still no good reason to initialise ret to a 0-element array and have an extra array-extend operation outside the loop. That's the WTF cherry on top.
Admin
The inability of some people (I refuse to use the phrase 'developers') to read a simple man page baffles me.
Another thing is not moving with times. If they used a Vector in the early 2000's, they will continue using it even though Sun/Oracle have been telling us for a decade that we really should be using List instead.
Yes, the Java standard library is quite large, but just reading the JavaDocs once in a while won't kill anybody.
Admin
That's the WTF whipped cream and chocolate crumbles on top.
Admin
If this is what happened, then we can say that the guy didn't read the f...ing manual, which is quite a WTF, IMHO.
Admin
What's wrong with using StringTokenizer (since v1.0)?
Admin
And which class would you instantiate to create this List?
Admin
I am no Java guy but maybe ArrayList.
If I remember correctly then the main difference is that Vector is synchronized, so whichever you want to use depends on the situation.
Admin
Given that the programmer creates a new ret and copies temp back into it, rather than just doing "ret=temp;", makes it clear that the programmer also has no concept of objects or pointers
Whoa, get your high-water pants on: It's an array flood!
Admin
Five minutes on Google saves five hours of analyzing the "custom" solution and the differences before refactoring to use the built in way (sometimes with additional changes to make it work how it did before).
Admin
Haha, Java guys complaining that it's C guys who have no understanding of pointers.
I would say that this guy has been working with unmanaged memory in C-like environments for far too long. Combine that with some naivety regarding Java object handling and it's clear what kind of intentions spawned this mess.
Admin
java;dr
Admin
Admin
Why do that when Vector is already synchronized? Isn't it the simplest class that implements List? I wouldn't be surprised if the internal Vector code does use ArrayList.
Admin
See also: StringTokenizer
Admin
Nothing, although the article author is unaware that StringTokenizer existed since JDK1.0 (according to the javadoc).
Admin
This is C code literally translated to Java. Nothing to see here... Move along...
Admin
When it takes more than 20 lines to do something fairly simple, you're probably doing it wrong.
Admin
Of course, there are the "up to date" Java programmers who try to split a pipe-delimited String like this:
String[] notTheResultYouWant = pipeDelimitedString.split("|");
Good times, good times.
Admin
To answer the first question: there are at least two reasons. Firstly, Vector is synchronised on itself: this is a bad practice; Collections.synchronizedList returns a list which is synchronised on a privately stored object, preventing idiots from synchronising on the list and creating lock conflicts.
Secondly, if you decide that you want to use a different underlying implementation (e.g. LinkedList) it's the difference between changing one contructor call (s/ArrayList/LinkedList/) and having to ask "Was this using Vector because it needed to be synchronised, in which case I need to add a call to Collections.synchronizedList, or was it for some other reason?"
(Back when people believed that Sun might one day actually remove anything from the API - which is what "Deprecated" was originally intended to signal - there may also have been a belief that Vector would be deprecated and removed and you were better off using ArrayList to ensure forward compatibility).
Admin
One does on occassion forget that they are regular expressions.
Admin
Its clear that a lot of people claiming to be java developer needs the right mentoring to create the right code.
I have seen MANY SUCH WTF coding practices in our code review, but I have gone above and beyond to fix them.
Admin
Java guy here.
Crappy overcomplicated code by some dumbass that do not RTFM. Not going to waste brainpower trying to understand that.
Admin
There is no reason whatsoever to keep copying the temp array into a new one of size +1. Instead, loop over the string twice. Once to determine the amount of tokens. Then initialize the array. Then loop over the string a second time to put the tokens in the array. That changes an O(N^2) algorithm to O(N).
So yes, even if this person came from a C-like background, this is very much a WTF.
Admin
Admin
If this discussion had occurred on the Discourse forum, I would have <3'ed this.
Admin
If this discussion had occurred on the Discourse forum, I would have !<3 'ed the Discourse page layout.
Admin
The chocolate syrup drizzled on top of the whipped cream and chocolate crumbles is that the final copy of temp to ret also adds one last element and puts the full original string into it.
Admin
Bad Java code....you don't say...
Admin
Some people don't have access to Google. You should know this.
On a complete unrelated note, The Queen's Horse is a a user.
Admin
Answer: Poppy Seeds.
Admin
Admin
Admin
Wrong Answer, Gungadin!
Admin
Total red herring. The code is wrong even in C with unmanaged memory. In that environment, temp and ret would both have to be pointers for this code to work at all, and it is completely idiotic to allocate a new array for ret and copy temp to ret, when a simple = would do. After all, during the next loop, he is generating a new temp, so there will be no conflict.
I'd suggest this was COBOL programmer stuff, but this is below even sensible COBOL code.
Admin
No big deal. The first time you run your test data through it you realise you've made a silly mistake and so you read the API a little more closely. Oh yes, you think, I forgot the argument has to be a regexp. And you correct your code and carry on. If, of course, you just put this line into your program and don't check it, then I'm afraid you're a bit of a cunt.
Admin
Admin
I wrote code in Fortran that had some of these WTFs. Arcane constructions were unavoidable because Fortran IV grudgingly provided just enough support for strings to let them exist, but I made these kinds of logic WTFs and overly complex loops because I was 16 years old, in first year of university, and prior experience had not included analysis of algorithms.
Admin
If you are like me, you are the only one in the code review who actually read the code.
Admin
My bet is on this coming from someone with VB background and a very very small hint of C.
Admin
Someone remind me to save a USB floppy drive. I'll need it to install VB 1 for testing.
Admin
Code like this should be a firing offence. It impressively presents a level of incompetence that is not easily remedied even by training; so the company should just let the employee go, and soon. Just sayin'.
Admin
Yeah, because the VB to Java transition is common.
Admin
Admin
Admin
Admin
String Tokenizer might not cut it (no pun intended).....I notice the delimiter is a String (I think Java's StringTokenizer Tokenizes on ANY element in the delimiter string, this appears to try to tokenize on the WHOLE string....but I'm not Javanese) That is, Java's ST takes delimiters whereas this one takes a string delimiter....
http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html#StringTokenizer(java.lang.String,%20java.lang.String)
Loads of other WTF there, but not using the strtok is not one of them.
Admin
If this discussion had occurred on the Discourse forum, I wouldn't have seen it.
Admin
If this is C code, the coder sucks in C, too. First, there would be the obvious use of strtok(). And in case you don't want to modify your "toSplit" string, you'd do something like this:
Admin