One of the Best Bits of Programming Advice I ever Got
Don’t make objects that end with ‘er’.
Some er’s that I’ve learned to avoid over the years:
- Managers - Every time I see one of these, I cringe. People will usually tell me what it does, long before they can tell me what it is. Is it a registry? Fine call it a registry. Is it a history or a log? Call it that. Is it a factory? Call it that.
- Controllers - Only good controller object I’ve made in the last 20 years was an interface to a BallastVoltageController that represented a real world object. The fact that every single MVC implementation in the world has had a different role for Controller ought to tell us something about how well that idea fit.
- Organizer (and many like them) - Focus is on what it does. This is a great example of how easy it is to turn many of these ‘ers’ into nouns. Call it an Organization. Now we’re focusing on what it is.
Analyzer/Renderer/etc - Definitely examples of “worker” objects. What if they had been Analysis/Rendering/etc.- Builder/Loader/Reader/Writer/etc - Remove the focus from the objects being manipulated, and tend assume to much responsibility themselves.
Data, Context and Interaction - DCI
Learned something new just now - http://en.wikipedia.org/wiki/Data,_Context,_and_Interaction
Basically with DCI we have
- Data - the stuffs you have, what your system is made of i.e Bank accounts
- Context - representation of use cases, scenario involving your data i.e Wire transfers
- Interaction - you can think of it as roles.
DCI on Ruby/Ruby on Rails
- This article covers some DCI with Rails.
- Ruby source on Object-Compositions (Google groups)
p/s: Will come out with my own example soon.
Bug for private method in Ruby < 1.8
Source. This is a bug apparently :).
Delegate module for Ruby
Another way to do delegation (continue from previous post) is by using the Delegate module. Refer to the Rails Magazine article in previous post
Forwardable module for Ruby
Was reading Rails Magazine first edition (get it here) when I found about this module. http://railsmagazine.com/articles/4
Below is an example of SingleForwardable module extended in an object
Checkout the Forwardable module which acts on a class.