• (cs)

    I reply sooner than you!

  • boohiss (unregistered)

    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.

  • (cs)

    I guess the WTF is that they are not using Collections.EMPTY_LIST ?

  • unknown (unregistered)

    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?

  • Dave (unregistered)

    Apparently they decided to re-invent Collections.EMPTY_LIST (which has been around since 1.2)

  • William T. Franklin (unregistered)

    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.

  • Katabrok (unregistered)

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

  • (cs)

    The contract for List.get actually states:

    Throws:
    IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).
    so it should be:
    <font size="2">public Object get(int index){
        throw new IndexOutOfBoundsException("This is the EmptyList you silly you!");
    }

    </font>
  • Dave (unregistered)

    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....

  • Dave (unregistered) in reply to Sindri

    damn i'm slow today....

  • (cs)

    Yet it's kind enough to not throw an exception if you try to access an element that is out of bounds.  Genius!

  • (cs) in reply to GalacticCowboy

    Dang it!  Cow-orker interrupted me mid-post...

  • (cs)

    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.

  • Jon Erickson (unregistered)

    <font><snip>
    package</snip></font> initech.foundation.util;


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

    "I too am not a ..."

    - Jon

  • (cs) in reply to Jon Erickson
    Anonymous:
    <font><snip>
    package</snip></font> 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.
  • (cs) in reply to William T. Franklin
    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:

    <font>package</font> initech.foundation.util;
    <font></font>
    <font>public class</font> NotSoEmptyList <font>extends</font> EmptyList
    {
    <font>public</font> NotSoEmptyList()
    {

    }

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

    <font>public int</font> size()
    {
    <font>return</font> 1;
    }

    }
    Imagine the possibilites.  Brilliant, simply brilliant

    NOT!
  • (cs) in reply to Jon Erickson
    Jon:
    <FONT size=+0><SNIP>
    package</SNIP></FONT> 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.

  • (cs) in reply to richleick

    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...

  • (cs) in reply to Sindri

    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!

  • mike_d (unregistered) in reply to GalacticCowboy
    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
  • (cs) in reply to Katabrok
    Anonymous:

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



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

       }

    }

  • (cs)

    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.

  • (cs) in reply to Katabrok

    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.

  • (cs) in reply to Katabrok
    Anonymous:

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


    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!
  • (cs)

    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

  • a0a (unregistered) in reply to dmitriy
    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.


  • (cs) in reply to Dave

    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?

  • Nate (unregistered)

    This one is weird, but not WTF-worthy.

  • David (unregistered)

    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.

  • Anonymous (unregistered)

    I'm just glad they didn't sprinkle a few calls to IsTrue() in there.

  • (cs) in reply to Rick

    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]

  • Kreiger (unregistered)

    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.

  • Hans Hammer (unregistered)

    Could this be an undocumented implementation of the Introduce Null Object pattern (http://www.refactoring.com/catalog/introduceNullObject.html) instead of a WTF?

  • Joe H. (unregistered)

    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!




  • (cs) in reply to Joe H.
    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.


  • Beyonce Knowles (unregistered) in reply to Rick
    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.

  • (cs)

    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.

  • (cs)

    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.

  • (cs) in reply to GalacticCowboy
    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.
  • (cs) in reply to Sean
    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.
  • (cs) in reply to Beyonce Knowles
    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.

  • (cs) in reply to mjonhanson
    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, ...

  • dasmb (unregistered) in reply to Dave

    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...

  • moobar (unregistered) in reply to dmitriy

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

  • (cs) in reply to trollable
    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.

  • somebody (unregistered) in reply to masklinn

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

  • (cs)

    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.

  • (cs) in reply to Sean

    Write-only memory is as useful as a function that returns void...

  • (cs) in reply to masklinn
    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.
  • vhawk (unregistered)

    Cool code !!

Leave a comment on “My List Is Emptier Than Your List”

Log In or post as a guest

Replying to comment #:

« Return to Article