Comment On My List Is Emptier Than Your List

Joe H. was reading through the documentation of his company's custom-built Java framework and came across today's bizarre example. It would seem that the framework builders (who have since moved on) preferred to use the EmptyList in a number of places in lieu of an Empty List. [expand full text]
« PrevPage 1 | Page 2Next »

Re: My List Is Emptier Than Your List

2005-12-01 11:27 • by Sindri
I reply sooner than you!

Re: My List Is Emptier Than Your List

2005-12-01 11:31 • by boohiss
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.

Re: My List Is Emptier Than Your List

2005-12-01 11:34 • by NetForce1
I guess the WTF is that they are not using Collections.EMPTY_LIST ?

Re: My List Is Emptier Than Your List

2005-12-01 11:36 • by unknown
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?

Re: My List Is Emptier Than Your List

2005-12-01 11:36 • by Dave
Apparently they decided to re-invent Collections.EMPTY_LIST (which has been around since 1.2)

Re: My List Is Emptier Than Your List

2005-12-01 11:38 • by William T. Franklin

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.

Re: My List Is Emptier Than Your List

2005-12-01 11:42 • by Katabrok

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

Re: My List Is Emptier Than Your List

2005-12-01 11:44 • by Sindri
The contract for List.get actually states:


Throws:
IndexOutOfBoundsException - if the index is out of range (index
< 0 || index >= size()).


so it should be:

public Object get(int index){

    throw new IndexOutOfBoundsException("This is the EmptyList you silly you!");

}





Re: My List Is Emptier Than Your List

2005-12-01 11:45 • by Dave
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
52314 in reply to 52312
damn i'm slow today....

Re: My List Is Emptier Than Your List

2005-12-01 11:47 • by GalacticCowboy

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
52316 in reply to 52315
Dang it!  Cow-orker interrupted me mid-post...

Re: My List Is Emptier Than Your List

2005-12-01 11:52 • by Manni

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.

Re: My List Is Emptier Than Your List

2005-12-01 11:56 • by Jon Erickson

package
initech.foundation.util;


Anybody else notice the name "Initech" (of Office Space fame).

"I too am not a ..."

- Jon

Re: My List Is Emptier Than Your List

2005-12-01 12:03 • by sinistral
52319 in reply to 52318
Anonymous:

package
initech.foundation.util;


Anybody else notice the name "Initech" (of Office Space fame).

"I too am not a ..."

- Jon




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
52321 in reply to 52308
Anonymous:

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.





My thoughts exactly.  Because you are now able to code the following:



package initech.foundation.util;

public class NotSoEmptyList extends EmptyList
{
public NotSoEmptyList()
{

}

public Object get(int index)
{
return new String("TA DA");
}

public int size()
{
return 1;
}

}
Imagine the possibilites.  Brilliant, simply brilliant



NOT!

Re: My List Is Emptier Than Your List

2005-12-01 12:13 • by Manni
52322 in reply to 52318
Jon:

package
initech.foundation.util;


Anybody else notice the name "Initech" (of Office Space fame).

"I too am not a ..."

- Jon


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
52323 in reply to 52321
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
52324 in reply to 52312
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
52325 in reply to 52323
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...




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
52326 in reply to 52310
Anonymous:

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





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()


   }


}



Re: My List Is Emptier Than Your List

2005-12-01 12:46 • by dmitriy
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
52330 in reply to 52310
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
52332 in reply to 52310
Anonymous:

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




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!


I won't even try to stay on topic

2005-12-01 12:52 • by bit
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

Re: My List Is Emptier Than Your List

2005-12-01 12:55 • by a0a
52334 in reply to 52329
dmitriy:
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.




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
52335 in reply to 52313
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?

Re: My List Is Emptier Than Your List

2005-12-01 13:12 • by Nate
This one is weird, but not WTF-worthy.

Re: My List Is Emptier Than Your List

2005-12-01 13:24 • by David
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.

It could have been worse.

2005-12-01 13:29 • by Anonymous

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
52341 in reply to 52335

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.


Ahh...  see, I'm not a Java programmer, unlike the guy who told me that...  [A]

Re: My List Is Emptier Than Your List

2005-12-01 13:45 • by Kreiger
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.



Re: My List Is Emptier Than Your List

2005-12-01 13:46 • by Hans Hammer
Could this be an undocumented implementation of the Introduce Null
Object pattern
(http://www.refactoring.com/catalog/introduceNullObject.html) instead
of a WTF?

Re: My List Is Emptier Than Your List

2005-12-01 13:59 • by Joe H.
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
52346 in reply to 52345
Anonymous:
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!










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
52347 in reply to 52335
Rick:
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?


The extended for loop uses a java.util.Iterator, not size info.

Re: My List Is Emptier Than Your List

2005-12-01 14:32 • by kipthegreat
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.

Re: My List Is Emptier Than Your List

2005-12-01 14:42 • by Sean
The sign of a great application architect is one who writes code that:




  1. Takes three times the code necessary to perform simple tasks.

  2. Mimics functionality that is readily available in the development framework, but with a fraction of the features.





Re: My List Is Emptier Than Your List

2005-12-01 14:46 • by Sean
52350 in reply to 52323
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...




Write-Only Memory.



Awesome.

Re: My List Is Emptier Than Your List

2005-12-01 14:50 • by kipthegreat
52352 in reply to 52350
Sean:
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...




Write-Only Memory.



Awesome.


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
52357 in reply to 52347
Anonymous:
Rick:
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?


The extended for loop uses a java.util.Iterator, not size info.


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
52363 in reply to 52332
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.


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
52364 in reply to 52306
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
52367 in reply to 52329
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
52368 in reply to 52363
trollable:
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.


Empty list in Lisp:    ( ) 

Sorry, I can not make it unreadable. Lots of advantage: constant, small, shareable, not exceptions, ...




Empty list in python: list() (alternate syntax: []).


Ditto for the thoughness of making that unreadable.

Re: My List Is Emptier Than Your List

2005-12-01 19:07 • by somebody
52373 in reply to 52368
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())

Re: My List Is Emptier Than Your List

2005-12-01 20:08 • by Xepol

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
52382 in reply to 52350
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
52384 in reply to 52368
masklinn:
trollable:
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.


Empty list in Lisp:    ( ) 

Sorry, I can not make it unreadable. Lots of advantage: constant, small, shareable, not exceptions, ...




Empty list in python: list() (alternate syntax: []).


Ditto for the thoughness of making that unreadable.





You're right.  So is pair((),()).  And that makes a better dirty joke.

Re: My List Is Emptier Than Your List

2005-12-02 00:26 • by vhawk
Cool code !!
« PrevPage 1 | Page 2Next »

Add Comment