Object behavior, was RE: Split Capabilities: Making Capabilities Scale

Bill Frantz frantz@communities.com
Wed, 26 Jul 2000 17:55:09 -0700


Rather than try to reply, I'm going to change the subject. :-)

<JAVA GEEKS START SKIPPING>
For a bald statement, there are no interesting objects without side
effects.  The square root function is hereby declared as uninteresting,
although useful.  Let me give a quick example.

Consider the behavior of the java.util.Vector object.  The basic contract
of Vector is, "The Vector class implements a growable array of objects.
Like an array, it contains components that can be accessed using an integer
index. However, the size of a Vector can grow or shrink as needed to
accommodate adding and removing items after the Vector has been created."
I won't go into all the details, but there is one method which is
interesting from this discussion.

The public final synchronized Enumeration elements() method returns an
enumeration of the components of this vector.  An Enumeration is defined as:

An object that implements the Enumeration interface generates a series of
elements, one at a time. Successive calls to the nextElement method return
successive elements of the series.
 
For example, to print all elements of a vector v: 

     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {
         System.out.println(e.nextElement());
     }

The two methods on an Enumeration are defines as:

public abstract boolean hasMoreElements()

Tests if this enumeration contains more elements. 

Returns: 
true if this enumeration contains more elements; false otherwise. 

 public abstract Object nextElement()

Returns the next element of this enumeration. 

Returns: 
the next element of this enumeration. 
Throws: NoSuchElementException 
if no more elements exist. 
</JAVA GEEKS START SKIPPING>

 
The code fragment above is all very fine, and simple to understand,
unfortunately it is broken.  It is broken because of the side effects of
having another thread remove elements from the Vector.  If another thread
removes the last element of a Vector between the call to hasMoreElements()
and nextElement(), then the code fragment will be terminated by the
NoSuchElementException.

The problems of side effects are not confined to object systems with facets.

Cheers - Bill