On Mon, Feb 20, 2006 at 01:59:40PM -0800, Joshua S. wrote:
documentation for under what circumstances find() returns a single
object vs a list, when it raises an exception, etc.
Can anyone shed some light on this design decision? Any chance of
getting those deprecated APIs supported again?
One of the lessons of API design that has emerged from the development
of
Rails is that when you need to do something in several slightly
different
ways, write a single method and parameterize it with an options hash
rather
than write several similar methods.
So rather than the positional parameters that you had to pass to
find_first,
find_all, we now have the single find with the symbol and option hash
that
you talk about above. Similarly, render_file, render_template,
render_inline,
render_partial, render_collection_of_partials, (etc…), turn into a
single
render method, which dispatches to the desired functionality depending
on the
options hash passed to it. This approach is, essentially, “multiple
dispatch”
(Multiple dispatch - Wikipedia), which is built into,
for
example, the Common Lisp Object System
(28.1.6. Generic Functions and Methods).
What’s good about this API?
-
Fewer methods to remember
Which also means, fewer method signatures to remember
-
It “scales”
You can add options thereby increasing the method’s utility without
changing the method signature, preserving backwards compatability
-
Encourages elegant usage
When you have only one method as your point of entry, you also only
have
one method signature. You can build a hash dynamically and pass it
along to
your method. You don’t have to contruct the name of and dynamically
call
the desired method while conditionally determining the appropriate
positional parameters to pass in. Sometimes the latter case is easy
enough,
but when it isn’t, it gets really messy, really fast.
-
It’s friendly
With the implementation details increasingly hidden inside the
framework, it is
the framework’s responsibility, rather than your’s, to figure out
how to
make a single method do the job of, in the case of render, 10
methods.
These multiple dispatch style methods provide a lot of functionality
with
their option hash, yet the single point of entry with just the single
method,
stricking a balance between a “Humane Interface”
(HumaneInterface) and a “Minimal
Interface” (MinimalInterface).
That is of course not the whole picture. But it’s the start of an
answer.
The old methods have indeed been officially deprecated and have been so
for
probably about over a year now. They could disappear any day now. I
wouldn’t
imagine them becoming undeprecated.
You indicated that the find_all and find_first were more natural to you
and
you said it would simplify things to have them split up. Is the relative
“naturalness” your primary beef with the single find method? It is my
experience that from both the framework implementation side and the
client
API side, the single find method is both more natural and simpler.
marcel