Plasticx Blog

Capable of being shaped or formed

query method pattern, abridged version

Posted by Mike 07/30/2008 at 10:50AM

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.

Posted in , , |

Sasquatch, Inc.

Posted by Mike 06/21/2007 at 01:09AM

I’m now a company!!!

Sasquatch, Inc.

Yet another programmer becomes a consultant.

Linux, Ruby, and a little Java to make some latte money.

Posted in , , , , |

Spring in Action (ISBN 1932394354)

Posted by Mike 02/12/2007 at 10:33PM
I've started reading Spring in Action at work for a new project (I get paid to write Java). I think I might like Java again, thanks Spring. Our group is also thinking about using JBoss SEAM ... which I fondly refer to JBoss on Rails JBoss Seam is written by Michael Yuan and Thomas Heute from the Seam Team at JBoss

Posted in , |

Head First Design Patterns (ISBN: 0596007124)

Posted by Mike 01/21/2007 at 06:14PM

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

Posted in , |


Web Statistics