{ |one, step, back| } 2 of 2 articles Syndicate: full/short

The Open Class Mindset   09 Dec 05
[ print link all ]

Does the open class nature of Ruby effect how we think of classes? Perhaps it does.

More on the Array API Controversy

I commented earlier on the Array API controversy (see Array API). I had some more thoughts on that topic.

As both a Java programmer and a Ruby programmer, I enjoy comparing the two languages, noting their differences and more importantly, recognizing the different ways they effect my thinking.

In general, I would tend to agree with Elliotte on creating classes with small APIs. They do seem to be easier.

On the other hand, I’m not really bothered by the large Array API provided by Ruby. There is a clash between what I believe and what I find to be true.

Perhaps some of the answer lies in the open nature of Ruby classes. A Ruby class can be extened at any time by any user. Although the potential for abuse exists, it is also a very handy thing to be able to do.

As a result, methods tend to migrate to the classes they belong to, rather than put in a utility method somewhere. Although I might never use assoc, if I were to look for it, I would start in the Array class1, or one of its included modules.

Since rubyists accept the fact that we can’t ever completely know a class, the fact it has extra methods on it that we aren’t using doesn’t seem to be a big deal.

Java, on the other hand, has closed classes. The methods available on List are all that are there, and all that ever will be there (well, until someone modifies and recompiles the class). This gives the impression that I can fully understand the class and exactly what it is doing. This impression is further strengthened by the use of interfaces, which further narrows down the user’s view of a class to just the methods available in an interface.

I don’t know, maybe this has nothing to do with it. And there are certainly other factors involved. For example, Array is such a commonly used class2 that perhaps this is the exception to the rule of small interfaces.

Whatever the reason, I don’t find using Array objects to be a burden at all, despite what the theory of small interfaces might indicate.


1 In fact, I did need assoc recently. Guess where I found it.

2 I find that Array is used much more in Ruby than any variety of List is used in Java. I don’t know if that a good thing or not.


comments

The Ruby Array API   09 Dec 05
[ print link all ]

Minimalists VS Humanists—A tempest in a teapot is brewing over the proper design of a class API.

Martin Fowler VS Elliotte Rusty Harold on API Design

There’s a hot debate going on about humane API design. Martin Fowler points out that Ruby’s Array API is very rich, providing the user with lots of options when using it. Elliotte Rusty Harold responded with “A 78 method List class is about three times as bad as a 25 method List class, not three times as good.”

And so the debate rages on. Read the links on the bottom of Martin’s page for other opinions on the topic.

I was going to write a long post pointing out that java.util.List is an interface, and we like to keep interfaces short. Ruby’s Array is not an interface (Ruby doesn’t have interfaces as a language construct) but a class, and the forces that keep interfaces small don’t apply so strongly.

But … I decided not to.

Instead I’m going to point out just a small irony. Elliotte writes:

Another example: Fowler likes the first and last methods in Ruby, but list.first() is not significantly simpler than list.get(0). list.last() is perhaps a little simpler than list.get(list.size() – 1) but only because Java stupidly indexes everything from 0 rather than 1. And how often do you actually need to get the first item in the list?

But java.util.LinkedList provides:

   Object getFirst()
            Returns the first element in this list.
   Object getLast()
            Returns the last element in this list.

Interesting.

Why does Elliotte not use getFirst()? ... Because it is not available in the java.util.List interface. Java programmers typically write:

   List myList = new LinkedList();

And then use myList in their code. Since myList is declared to be a list, the getFirst() method never pops up in their completion menus.

As they say: “Out of sight, out of mind.”


comments

 

Formatted: 19-Mar-10 08:53
Feedback: jim@weirichhouse.org