| « Prev | Page 1 | Page 2 | Next » |
|
I reply sooner than you!
|
|
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. |
|
I guess the WTF is that they are not using Collections.EMPTY_LIST ?
|
|
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?
|
|
Apparently they decided to re-invent Collections.EMPTY_LIST (which has been around since 1.2)
|
|
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. |
|
Let's start the flame wars!!! The only WTF that I see is that they are using JAVA!!!! [um]Come, rain of fire!!! Thanks, Leo |
|
The contract for List.get actually states:
so it should be: public Object get(int index){ throw new IndexOutOfBoundsException("This is the EmptyList you silly you!"); } |
|
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.... |
Re: My List Is Emptier Than Your List
2005-12-01 11:45
•
by
Dave
|
|
damn i'm slow today....
|
|
Yet it's kind enough to not throw an exception if you try to access an element that is out of bounds. Genius! |
Re: My List Is Emptier Than Your List
2005-12-01 11:48
•
by
GalacticCowboy
|
|
Dang it! Cow-orker interrupted me mid-post...
|
|
I can't think of any reason you'd need an object that was completely empty. Couldn't you just do this: String 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. |
|
Re: My List Is Emptier Than Your List
2005-12-01 12:03
•
by
sinistral
|
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. |
Re: My List Is Emptier Than Your List
2005-12-01 12:08
•
by
richleick
|
My thoughts exactly. Because you are now able to code the following: package initech.foundation.util;Imagine the possibilites. Brilliant, simply brilliant NOT! |
Re: My List Is Emptier Than Your List
2005-12-01 12:13
•
by
Manni
|
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. |
Re: My List Is Emptier Than Your List
2005-12-01 12:13
•
by
GalacticCowboy
|
|
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...
|
Re: My List Is Emptier Than Your List
2005-12-01 12:14
•
by
richleick
|
|
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! |
Re: My List Is Emptier Than Your List
2005-12-01 12:21
•
by
mike_d
|
ah..just like the good old write-only memory: http://ganssle.com/misc/wom1.jpg |
Re: My List Is Emptier Than Your List
2005-12-01 12:23
•
by
richleick
|
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() } } |
|
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. |
Re: My List Is Emptier Than Your List
2005-12-01 12:48
•
by
mjonhanson
|
|
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. |
Re: My List Is Emptier Than Your List
2005-12-01 12:49
•
by
mjonhanson
|
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! |
|
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 |
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. |
Re: My List Is Emptier Than Your List
2005-12-01 13:12
•
by
Rick
|
|
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? |
|
This one is weird, but not WTF-worthy.
|
|
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.
|
|
I'm just glad they didn't sprinkle a few calls to IsTrue() in there. |
Re: My List Is Emptier Than Your List
2005-12-01 13:34
•
by
GalacticCowboy
|
Ahh... see, I'm not a Java programmer, unlike the guy who told me that... [A] |
|
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. |
|
Could this be an undocumented implementation of the Introduce Null
Object pattern (http://www.refactoring.com/catalog/introduceNullObject.html) instead of a WTF? |
|
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! |
Re: My List Is Emptier Than Your List
2005-12-01 14:18
•
by
Steve
|
Sounds like it's usage was more of a WTF than that actual class. |
Re: My List Is Emptier Than Your List
2005-12-01 14:23
•
by
Beyonce Knowles
|
The extended for loop uses a java.util.Iterator, not size info. |
|
I've done something extremely similar to this, only with an Enumeration:
/** * 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; } }; 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. |
|
The sign of a great application architect is one who writes code that:
|
Re: My List Is Emptier Than Your List
2005-12-01 14:46
•
by
Sean
|
Write-Only Memory. Awesome. |
Re: My List Is Emptier Than Your List
2005-12-01 14:50
•
by
kipthegreat
|
As someone else has already pointed out, add() will throw an UnsupportedOperationException unless its behavior is defined. |
Re: My List Is Emptier Than Your List
2005-12-01 15:51
•
by
Rick
|
That is what I had thought. Apparently the hidden Iterator uses the size() method. |
Re: My List Is Emptier Than Your List
2005-12-01 17:01
•
by
trollable
|
Empty list in Lisp: ( ) Sorry, I can not make it unreadable. Lots of advantage: constant, small, shareable, not exceptions, ... |
Re: My List Is Emptier Than Your List
2005-12-01 17:06
•
by
dasmb
|
|
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... |
Re: My List Is Emptier Than Your List
2005-12-01 17:59
•
by
moobar
|
|
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). |
Re: My List Is Emptier Than Your List
2005-12-01 18:04
•
by
masklinn
|
Empty list in python: Ditto for the thoughness of making that unreadable. |
Re: My List Is Emptier Than Your List
2005-12-01 19:07
•
by
somebody
|
|
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()) |
|
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. |
Re: My List Is Emptier Than Your List
2005-12-01 22:53
•
by
ennuija
|
|
Write-only memory is as useful as a function that returns void...
|
Re: My List Is Emptier Than Your List
2005-12-01 23:37
•
by
mjonhanson
|
You're right. So is pair((),()). And that makes a better dirty joke. |
| « Prev | Page 1 | Page 2 | Next » |