{ |one, step, back| } 6 of 6 articles WikiSyndicate: full/short

Dependency Injection Gone Bad   28 Dec 05
[ print link all ]

It always bothers me to see mock objects creating and returning other mock objects.

David Chelimsky on Dependencies

David Chelimsky of Object Mentor notes that dependency injection eases the pain of dependencies so much that we sometimes don’t bother to root out the bad dependencies. Read the article and pay close attention to the dependencies in the original and final versions (I drew a couple UML diagrams to keep it clear in my head).

Plus its got a great Princess Bride reference!

Symbols Are Not Immutable Strings   27 Dec 05
[ print link all ]

Inevitably, every Ruby nuby asks the question “What are symbols?”, and the standard answer is “A Symbol is an immutable string”. I have some issues with that answer.

Every Nuby’s Question

Whenever someone asks “what are symbols” in the ruby-talk mailing list, the explaination almost always begins with “Symbols just immutable strings”, and then goes on to explain what symbols are good for and how they are used. Comparing them to something familiar (like a String) is a good pedagogical technique, but I’m left feeling that the answer leads the nuby down the wrong path to understanding.

Symbols aren’t Strings

First of all, symbols really aren’t immutable strings. Not even close. In fact, they aren’t Strings of any kind. This simple fact is easily demonstrated by the following:

  $ irb --simple-prompt
  >> sym = :symbol
  => :symbol
  >> sym.size
  NoMethodError: undefined method `size' for :symbol:Symbol
          from (irb):2
  >> sym[0,2]
  NoMethodError: undefined method `[]' for :symbol:Symbol
          from (irb):3

You can’t ask for the size of a symbol. You can’t ask for a substring of the symbol. You can’t do anything that you can do with strings on Symbols except (1) convert it to an integer (using to_i) and (2) convert it to a symbol (using to_sym). If a symbol is not a duck-type for a String, it hardly seems reasonable to call Symbols any kind of String, even immutable ones.

So if they aren’t Strings, what are they?

A Symbol is a simply an object that can be can be uniquely identified by its name. Every time you say :xyz in your code, you will be refering to an object (a symbol) that has the name “xyz”. There is only one Symbol with the name “xyz”, so all references to :xyz will refer to the same symbol1.

So you see, there is a tight relationship between Strings and Symbols. Every symbol has a string name (available via to_s). And every String can be asked for its corresponding symbol (via to_sym). Strings and Symbols are closely related, but they are not the same thing.

So, fundamentally, Symbols are just objects with names.

And when should we use Symbols?

Invariably, when presented with the “Symbols are immutable Strings” meme, the nuby will ask “So when do I use Strings and when do I use Symbols”? By pointing out that Symbols are not Strings at all, makes it clear. Use Strings whenever you need … umm … string-like behavior.

As we pointed earlier, Symbols are not even duck-typed strings. So using a symbol where you really want string-like behavior is a mistake. Rather, take a clue from the description of a symbol: an object with a name. It turns out that symbols are great for naming things. Use them whenever you need to name something in your code. For example:

  1. Naming keyword options in a method argument list
  2. Naming enumerated values (e.g. like enums in C).
  3. Naming options in an option hash table.

I’m sure other uses will come to your mind.

Some Closing Thoughts

Here’s how I like to express the difference between Strings and Symbols. Strings are about a sequence of characters. Symbols are about naming and identifying things.

There you have it.


I’m watching. Perhaps the next time this question comes up on ruby-talk, someone will say “A Symbol is an object with a name”. That would make my day.


1 For anyone familiar with Lisp, the simularities between Symbols and Atoms are obvious.

Coding Standards -- Keep It Simple   14 Dec 05
[ print link all ]

The last several clients I have worked for loved to publish huge coding standards documents. A recent one was only 20 pages long (and that was one of the shorter ones).

While browsing the new Rails 1.0 web site1, I stumbled upon the Rails coding standard. Here it is in full (link):

  • Two spaces, no tabs
  • MyClass.my_method(my_arg)—not my_method( my_arg ) or my_method my_arg
  • Follow the conventions you see used in the source already

1 You did know that Rails 1.0 was released today, right?

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.

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

Try Ruby   01 Dec 05
[ print link all ]

Why the Lucky Stiff provides us with yet another Ruby Goodness.

Try Ruby Now

For those of you who have been living under a rock and have missed this link, you need to check this out:

(courtesy of Why the Lucky Stiff)

 

Formatted: 21-Aug-08 13:36
Feedback: jim@weirichhouse.org