Skip to content

Instantly share code, notes, and snippets.

@jayzz55
Created January 5, 2016 16:49
Show Gist options
  • Save jayzz55/f458c194953011410e35 to your computer and use it in GitHub Desktop.
Save jayzz55/f458c194953011410e35 to your computer and use it in GitHub Desktop.

2: Performing Work

Sending a strong message

The foundation of an object oriented system is the message.

Strong and confident messages requires three elements:

  1. We must identify the messages we want to send in order to accomplish the task at hand.
  2. We must identify the roles which correspond to those messages.
  3. We must ensure the method's logic receives objects which can play those roles.

Roles are names for duck types, simple interfaces that are not tied to any specific class and which are implemented by any object which responds to the right set of messages. Despite the prevalance of duck-typing in Ruby code, there are two pitfalls that users of duck-types often fall into:

  1. they fail to take the time to determine the kind of duck they really need. That's why up till now we've focused so much on identifying messages and roles: it does little good to embrace duck types when the messages being sent don't match the language of the problem being solved.
  2. they give up too early. Rather than confidently telling objects that look like a mallard to quack, they fall back on type checks. They check if the object is_a?(Duck), ask it if it will respond_to?(:quack), and constantly check variables to see if they are nil.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment