- 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
I reply sooner than you!
Admin
Ah, that's not so bad. Given the chance, I would give into urges even more shocking.
I've seen worse, and I've done worse.
Admin
I guess the WTF is that they are not using Collections.EMPTY_LIST ?
Admin
The only reason I can see for this object is if you want a read-only empty list. What's the use of it, I don't know. Any ideas?
Admin
Apparently they decided to re-invent Collections.EMPTY_LIST (which has been around since 1.2)
Admin
Well, duh, how else would you do this? Isn't this the whole point of OOP - you can take advantage of class hierarchies and subclassing polymorphism to more easily represent complex concepts, like empty lists, that would be simply impossible to create in non-OO languages?
The only WTF here is that they appear to be writing their empty list classes by hand, instead of generating them automatically from the UML.
Admin
<FONT face=Tahoma>Let's start the flame wars!!!</FONT>
<FONT face=Tahoma>The only WTF that I see is that they are using JAVA!!!!</FONT>
<FONT face=Tahoma>[um]Come, rain of fire!!!</FONT>
<FONT face=Tahoma>Thanks, Leo</FONT>
Admin
The contract for List.get actually states:
IndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size()).<font size="2">public Object get(int index){
throw new IndexOutOfBoundsException("This is the EmptyList you silly you!");
}
</font>
Admin
Perhaps they didn't like how the 'get' method was implemented on Collections.EMPTY_LIST:
public Object get(int index) {
throw new IndexOutOfBoundsException("Index: "+index);
}
Guess people over at that place don't like to see if a List is null or empty first....
Admin
damn i'm slow today....
Admin
Yet it's kind enough to not throw an exception if you try to access an element that is out of bounds. Genius!
Admin
Dang it! Cow-orker interrupted me mid-post...
Admin
I can't think of any reason you'd need an object that was completely empty. Couldn't you just do this:
String EmptyList;
EmptyList = "";
And there you have it, a simple no-nonsense way of having an object that is devoid of information. My Java experience is one college class with a Middle Eastern professor that I took a year ago and haven't used since, so sorry if the syntax isn't right.
Admin
Admin
That's because Alex (our gracious host and moderator) uses Initech to protect the company names of the (not so) innocent. It's part of The Daily WTFs community identity, if you will.
Admin
My thoughts exactly. Because you are now able to code the following:
Imagine the possibilites. Brilliant, simply brilliant
NOT!
Admin
Jon, quick tip for posting to messageboards: Read through a few of the topics before you conjure up your first post. That way you'll catch up on inside jokes and the posting habits of the regulars. I think most people here are understanding enough to explain what's going on to the new folks, but other boards I've been on are not so kind.
Admin
One of my coworkers pointed out that, as they have not overridden the "Add" functions, you can merrily add items to the list all day - you can just never retrieve them or know how many there are...
Admin
I just liked the home simpson quote for the signature.
Here is one along those same lines:
Homer, after being asked a question by his wife:
"Marge, I'm not going to lie to you"
[silence follows and then Homer exits stage left]
That's where it ends. He doesn't lie because he doesn't answer the question.
YOINK!
Admin
ah..just like the good old write-only memory: http://ganssle.com/misc/wom1.jpg
Admin
Good one. But at least they coded the brackets so you can actually read the code. I mean honestly, isn't this:
public function myFunction()
{
if(something)
{
doSomething()
}
else
{
doSomethingElse()
}
}
A LOT better than
public function myFunction() {
if(something) {
doSomething()
} else {
doSomethingElse()
}
}
Admin
This is not too bad, except for two problems. One, pointed out already, si that you can add elements to this list. The other is the get() method. If the caller tries to call any methods on the "Object" returned, their application will crash because of dereferencing a null.
Admin
If you want to start a flame war, you need to do it properly by giving an example of why Perl or VB or Lisp or Smalltalk or Brainf**k, etc. is better than Java, preferrably with completely unreadable code on a single line.
Admin
If you want to start a flame war, you need to do it properly by giving an example of why Perl or VB or Lisp or Smalltalk or Brainf**k, etc. is better than Java, preferrably with completely unreadable code on a single line. Now please flame me for f**king up the last post!
Admin
When you visit sites like these, you're forced to say (and type) WTF! a lot.
Wouldn't it be nice to have a special character to represent it?
Somebody's making it happen (not really):
http://typophile.com/node/16343
Admin
Remember, this is Java, they get a NullPointerException, which is, in fact, an error handling message.
I wrote a little something on exceptions and errors..
cheers.
Admin
You cannot add any items to an AbstractList without overriding at least one of the add methods. So EmptyList is not a NULL SINK, which actually might be useful. /dev/null in Unix certainly is useful.
Trivia question:
What does Java 1.5 do with the new extended for loop if the size() method doesn't return the correct size of the collection?
Admin
This one is weird, but not WTF-worthy.
Admin
This looks like an attempt at the NullObject pattern applied to a list to me. Maybe it wasn't done correctly, but it's not a WTF.
Admin
I'm just glad they didn't sprinkle a few calls to IsTrue() in there.
Admin
Ahh... see, I'm not a Java programmer, unlike the guy who told me that... [A]
Admin
The only WTF here is that the get(int) method does not honor the List contract by not throwing IndexOutOfBoundsException.
This may not be a problem if it is only used in code which checks the size first. Otherwise it's just a bug.
Collections.EMPTY_LIST should have been used instead.
An empty list can be quite useful as is evidenced by the existence of Collections.EMPTY_LIST.
This is a non-WTF if there ever was one.
Admin
Could this be an undocumented implementation of the Introduce Null Object pattern (http://www.refactoring.com/catalog/introduceNullObject.html) instead of a WTF?
Admin
As the poster of this code I'm happy to say that this code was actually used to represent a special brand of no data.
It made me say WTF when I saw it. Then I searched for where it was used and got even more mystified.... they kept creating new instances of EmptyList all over the place, storing it and then converting them back to null before exiting the method it was used in!
Admin
Sounds like it's usage was more of a WTF than that actual class.
Admin
The extended for loop uses a java.util.Iterator, not size info.
Admin
I've done something extremely similar to this, only with an Enumeration:
<font size="2"> /**
* An Enumeration of nothing (as opposed to returning null).
*/
private final static Enumeration EMPTY_ENUMERATION = new Enumeration() {
public Object nextElement() throws NoSuchElementException { throw new NoSuchElementException(); }
public boolean hasMoreElements() { return false; }
};</font>
I don't see what's wrong with that. In this case my class handled a HashMap internally, and there were a few methods to get Enumerations of the objects in the map (depending on what kind of objects you wanted). But the internal HashMap could be null. In that case, I didn't want to return a null Enumeration, so I returned EMPTY_ENUMERATION instead.
Admin
The sign of a great application architect is one who writes code that:
Admin
Write-Only Memory.
Awesome.
Admin
As someone else has already pointed out, add() will throw an UnsupportedOperationException unless its behavior is defined.
Admin
That is what I had thought. Apparently the hidden Iterator uses the size() method.
Admin
Empty list in Lisp: ( )
Sorry, I can not make it unreadable. Lots of advantage: constant, small, shareable, not exceptions, ...
Admin
There is one problem with using Collections.EMPTY_LIST, and that's reference equality. Maybe you don't want emptyList1 == emptyList2 (even though you do want emptyList.equals(emptyList2))
There's also the possibility that they wanted to make it abundantly clear that there's a difference between a list that just happens to be empty, and a list INTENDED to be empty. Sort of a NullList. So you can say, "Hey, did that customer send us data files that were empty, or haven't they sent them yet?" by testing list instanceof EmptyList. This is (to some people's minds) better than simply setting list = null, because then you don't have to worry about testing for null in every location that uses null. Instead, you build your null handling behavior into the list itself. The writers of "Head First Design Patterns" love this idiomatic paradigm. I tend to think they should take the "ma" out of the word...
Admin
I don't see any WTF with this class. Having an EmptyList class that can be instantiated, as opposed to using the static EMPTY_LIST, has several useful purposes. The person who posted this WTF clearly doesn't understand this.
Perhaps the only really incorrect part of this code is that you can add items to the list but not retrieve or remove them (ie, memory leak until the object is destroyed). But clearly only an idiot would actually try doing that (in which case the WTF would be the person using the class, not the class itself).
Admin
Empty list in python:
list()
(alternate syntax:[]
).Ditto for the thoughness of making that unreadable.
Admin
I think that '() is a constant in lisp.
In python I can do
x = []
x.append(1)
print x # prints [1]
So I suppose that [] is equivalent with new Arraylist())
Admin
I'd like to say that I've done similar things, but frankly I can't stretch my oddities this far. I am not sure why you would want to create a list that is ALWAYS empty but can always return data. Perhaps so that you always at least have a valid list object (and there is where the overlap occurs with my past projects, but in my case it was a list that always had a zero'th 'empty' element and it was returned when the internal searchs failed to find a match, so I didn't have to handle null pointers - still when I get the chance, I'll end up rewriting that code cause it causes nothing but grief as the project evolves).
For all it does, it might as well be a null pointer. I am somewhat suprised to find that java has a system object that clones this one. Why exactly would you want to create a list object that basically acts like a huge black hole??? For just keeping a pointer active, an actual list with no elements would seem to make about as much sense, except that you could actually add data to it instead of getting an abstraction exception popup.
A real head scratcher to be sure.
Admin
Write-only memory is as useful as a function that returns void...
Admin
You're right. So is pair((),()). And that makes a better dirty joke.
Admin
Cool code !!