query method pattern, abridged version
Kids that smoke pot grow up to be short adults
Like kids that start smoking pot before the age of 13 stunting their growth, almost every Java programmer has an aspect of their OO programming skills stunted. Namely the OO principle of “Favor ‘object composition’ over ‘class inheritance’.” from the GofF book about design patterns . Because you can’t do that naturally in Java one becomes heavily dependent on class inheritance and your ability to solve problems elegantly in Ruby is extremely disabled when first using the language.
Example
What’s up Java Guy?:
arr = [1, 2, 3,]
if arr.size > 0
#do something
end
How’s it hang’n OO dude?:
arr = [1, 2, 3,]
if !arr.empty?
#do something
# fyi, in ActiveRecord there is arr.any?
end
The Fundamental Idea
Start looking at querying objects for their state instead of pulling their state out of them. When you start doing this you’ll come up on situations where the object is not providing a query method that you need. That is your entry point into the power of Ruby because you’ll start to learn how to open up classes and objects using various Ruby techniques to create the query method needed for the situation.
In the above code there is a comment that ActiveRecord defines #any? for its collections. See the definition of any? in association_collection.rb . Essentially #any? is just !arr.empty? The point being that Ruby didn’t provide an #any? method for collections so it was defined in ActiveRecord. There is no reason that you shouldn’t be doing the same in your code when you need a query method for your situation.
ReihH describes the query method pattern at depth in Ruby Patterns: Query Method and was the inspiration for this post.
Resources
Once you start applying the query pattern in your code you’ll want to follow up by understanding the following posts.
- Ruby Patterns: Query Method http://reinh.com/blog/2008/07/16/ruby-patterns-query-method.html
- Ruby: Underuse of Modules: http://blog.jayfields.com/2008/07/ruby-underuse-of-modules.html
- Class Reopening Hints: http://blog.jayfields.com/2007/01/class-reopening-hints.html
- Seeing Metaclasses Clearly: http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html
Sasquatch, Inc.
I’m now a company!!!
Yet another programmer becomes a consultant.
Linux, Ruby, and a little Java to make some latte money.
Spring in Action (ISBN 1932394354)
Head First Design Patterns (ISBN: 0596007124)
A couple of years ago Slashdot reviewed of “Head First Design Patterns” by Elisabeth Freeman, Eric Freeman, Bert Bates, and Kathy Sierra.
I bought the book but at the time wasn’t programming Java and didn’t take any further action. A weird twist of circumstance was at the time Elisabeth Freeman and Eric Freeman lived in the same town as me and I would see them on the ferry that we commute on. I knew that they were Java programmers by the JavaOne backpacks that they had but I didn’t know they were the authors of the book. Anyway, they don’t live here now, which is too bad, because I would let them know that I learned a lot from their work. |
The way that I self teach is to read a book and either take notes while reading or take summary notes at the end of the chapter. Then when finished with the book recompile my notes. These are my notes from the book. They will not replace your notes when you read the book but do provide a nice reference in and of themselves (and make for some blog content).