Another Rails and dynamic language fallacy concerning missing methods
Russell writes in the post The Dynamic Language Advantage: A Concrete Example:
“All you have to do is make the call to the non-existent method that contains your column names and Rails will dynamically generate the method for you.”
Though he got quite some flak for his opinion.
@cards = Card.find_all_by_cardType_and_expirationData(cardTypeId, expirationDate)
Which looks magic to most people and most impressive. But in the end it’s not more safe or magic than
Card.find("all_by_cardType_and_expirationData", cardTypeId, expirationDate);
This code can easily be written in any static language, becasuse the method missing property is no more safe or expressive than a String. The idea that parsing a method name from method missing is different than parsing a String in this case is a fallacy. I wonder why not more people use Strings like this to create finders? Perhaps because it’s not a good idea, as some comments in the mentioned post claim?
Thanks for listening.
If you liked this post, subscribe to my free full RSS feed.
Filed under: Java, Rails, Ruby
I’d like to point out that the Parancoe framework (http://www.parancoe.org, go to http://www.parancoe.org/articles/2007/08/28/how-to-add-finder-methods-to-parancoe-daos/ for a specific example) allows you to define an interface with a statically declared method findAllByCardTypeAndExpirationData() and it gets implemented by dynamic generation of code. To me it’s the best approach if you like expressive method names, compiler safety and would like to avoid boilerplate code.
June 13th, 2008