<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Talk Like A Duck: Tag chickens</title>
    <link>http://talklikeaduck.denhaven2.com/articles/tag/chickens</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In Ruby, it's not the dog, it's the tricks!</description>
    <item>
      <title>On Ceremony and Training Wheels</title>
      <description>&lt;p&gt;The perennial topic of &lt;a href="http://www.nabble.com/Not-quite-getting-it.-td17281703.html"&gt;&lt;strong&gt;getting&lt;/strong&gt; duck-typing in ruby&lt;/a&gt; has come up again.  A ruby nuby wonders about the dangers of passing a &amp;#8220;giraffe&amp;#8221; object to a method expecting an &amp;#8220;order.&amp;#8221; Or asking an object to &amp;#8220;draw&amp;#8221;, expecting it to be a &amp;#8220;brush&amp;#8221; when it was really a &amp;#8220;cowboy.&amp;#8221;&lt;/p&gt;
&lt;p&gt;These are reasonable questions, coming from someone who is used to a high-ceremony language which gives at least the illusion of protection from such &amp;#8220;errors.&amp;#8221;  Note that method signatures in languages like Java can&amp;#8217;t really protect from a draw method doing different things in different classes.&lt;/p&gt;
&lt;p&gt;J. Cooper offered the following good advice:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Instead, look at it like this: how likely is a Giraffe able to do what
an Order does? If the Giraffe doesn&amp;#8217;t quack like an Order, than an
exception is going to be thrown automatically anyway.&lt;/p&gt;
&lt;p&gt;Just program in it, and see how often it ever actually bites you. Start
with small things, work your way up.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;The advice here is to tackle the problem of learning a language like Ruby with a certain amount of fearlessness.  This reminds me of learning to ride a bike.&lt;/p&gt;
&lt;p&gt;The traditional way of learning to ride a bicycle is to use training wheels, sometimes even progressively.  Your dad might have installed the training wheels so that they touched the ground, turning the bike into a kind of tricycle with an extra, overly large back wheel between the normal ones.  You think you&amp;#8217;re riding a bike, but you&amp;#8217;re really still on a trike.&lt;/p&gt;
&lt;p&gt;The next stage, is for dad to raise the training wheels a bit so that you need to start learning to balance properly, but you can&amp;#8217;t really tip too far. Now you&amp;#8217;re riding something that kind of acts like a bicycle, but which &amp;#8220;flops over&amp;#8221; into tricycle mode.&lt;/p&gt;
&lt;p&gt;If you never get past this stage, you never really learn to ride a bike.  In an analogy with programming in Ruby, training wheels are &lt;a href="http://talklikeaduck.denhaven2.com/articles/2007/10/22/chicken-typing-isnt-duck-typing"&gt;chicken typing&lt;/a&gt; devices.&lt;/p&gt;
&lt;p&gt;The truth is that bicycles and motorcycles operate quite differently than wheeled vehicles which keep three or more wheels on the ground, for one thing you steer by leaning, not with the handlebars or steering wheel.  Learning to fly an airplane gives even stronger examples of having to learn that your instincts are wrong, and that you have to train yourself to &amp;#8220;instinctively&amp;#8221; know not only that you turn by banking rather than with the rudder, but that you control altitude primarily with the throttle, not the elevators, speed primarily with the elevators not the throttle, and so forth.&lt;/p&gt;
&lt;p&gt;Now the use of training wheels, or flight simulators, might be considered a ceremony, although a necessary rite of passage for many learners.  Given the high cost of mistakes in learning, it&amp;#8217;s no doubt a good thing that student pilots get time in simulators and in dual control planes accompanied by a flight instructor before soloing.  Although the way most young birds learn to fly is to be throw out of the nest to &amp;#8220;sink&amp;#8221; or &amp;#8220;swim&amp;#8221; so to speak.&lt;/p&gt;
&lt;p&gt;In learning to program, it&amp;#8217;s hard to see a difference between a simulator and reality, so it&amp;#8217;s perfectly feasible to &amp;#8220;fly the plane&amp;#8221; from the start. The consequences of a mistake are no more dire than making a mistake in a flight simulator.  It&amp;#8217;s really just a matter of conquering fear, not to rely on chicken-typing, and the more mistakes you make and correct, the more quickly you come to grips with the differences in really using a dynamically typed language from earlier experiences.&lt;/p&gt;
&lt;p&gt;Some can do this easier than others.  For example, Alex, my 6-year old next-door neighbor recently jumped off his tricycle, and onto a bicycle (sans training wheels) and is now zooming around our cul-de-sac faster than ever before.&lt;/p&gt;
&lt;p&gt;So if you&amp;#8217;re struggling with how to program Ruby or a similar language because lacking ceremonial training wheels to &amp;#8220;bridge the gap&amp;#8221; from Java or a similar language.  Might I suggest that you just jump in like Alex and give it a whirl.&lt;/p&gt;
&lt;p&gt;Of course, it&amp;#8217;s rarely easy to be as fearless as a 6-year old!&lt;/p&gt;</description>
      <pubDate>Sun, 18 May 2008 10:15:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:d174206d-505d-465b-8cbd-435bd179a143</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2008/05/18/on-ceremony-and-training-wheels</link>
      <category>ruby</category>
      <category>ducks</category>
      <category>chickens</category>
      <category>ceremony</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/499</trackback:ping>
    </item>
    <item>
      <title>Chicken Typing Isn't Duck Typing</title>
      <description>&lt;div style='width:173; float:left; text-align:right; font-size:xx-small; border-width:1px; border-color:#444444; border-style:solid;margin-bottom:30px; margin-right:30px;' class='tease-image'&gt;
&lt;img width='173' height='240' alt='Stress' src='http://farm1.static.flickr.com/36/95509221_cc5e4aa8f5_m.jpg'&gt;
&lt;br/&gt;
&lt;a href='http://flickr.com/photos/frield/95509221/'&gt;Stress&lt;/a&gt;
&lt;br/&gt;&amp;copy;
&lt;a href='http://flickr.com/people/frield'&gt;David Friel&lt;/a&gt;
&lt;br/&gt;&lt;a href='http://creativecommons.org/licenses/by/2.0/'&gt;&lt;img src='http://i.creativecommons.org/l/by/2.0/80x15.png' title='used under a Creative Commons Attribution License' width='80' height='15' border='0'/&gt;&lt;/a&gt;
&lt;/div&gt;
Trans just posted a &lt;a href="http://thread.gmane.org/gmane.comp.lang.ruby.general/232460"&gt;question on ruby-talk&lt;/a&gt;.
&lt;p&gt;It seems that he uncovered a conflict between the facet's library and the Ruby standard open_uri library.  Open-uri defines a module called Meta which it uses to extend certain StringIo instances. The Meta module adds several methods including one called meta, which returns a hash of meta information.  In two places in open_uri, the message "respond_to? :meta" is used to test whether a particular StringIO instance has been extended.&lt;/p&gt;
&lt;p&gt;The problem is that Facets also defines a method in either Object or Kernel called meta which provides a clever way to access the singleton class of an object.  So the respond_to? in open_uri was getting false positives.&lt;/p&gt;
&lt;p&gt;Trans asked "So where lies the fault in this conflict? Are extensions the bad guy,
or is respond_to? really not a good oop concept? Or..?"&lt;/p&gt;
&lt;p&gt;David Black replied that he thinks "this is just the age-old issue about adding methods to
existing classes and hitting conflicts. #respond_to? is relatively
dumb; it can't tell you what the method actually does. I think of it
as an important tool for "soft" duck typing, in contrast with "hard"
duck typing where you just send the object the message.
"&lt;/p&gt;
&lt;p&gt;Reading this made me realize that a much better name than "soft" duck typing, for using kind_of?/is_a?, respond_to?, or other such "sniff first" techniques, is &lt;strong&gt;Chicken Typing&lt;/strong&gt; &lt;/p&gt;



&lt;h2&gt;Chicken Typing Isn't The Dynamic Equivalent of Static Typing&lt;/h2&gt;
&lt;p&gt;From what I've observed chicken typing is a "safety blanket" technique used by programmers experienced in statically typed languages, who seem uncomfortable with making the paradigm shift needed to really make dynamically typed languages work.&lt;/p&gt;
&lt;p&gt;But because Ruby is dynamically typed, such techniques work imperfectly at best.  Here are &lt;strong&gt;some&lt;/strong&gt; of the problems:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Checking with respond_to? can give false positives like this because just because an object has a method with the name you are looking for, doesn't mean that the method has the semantics you expect&lt;/li&gt;
	&lt;li&gt;Similarly respond_to? can give false negatives when methods are implemented dynamically via method_missing.  Chicken typers try to enforce conventions which require overriding respond_to? to get around this, but in general this is difficult, expensive, or impossible to do perfectly.&lt;/li&gt;
	&lt;li&gt;Checking with kind_of?/is_a? is unnecessarily constraining. In Ruby you don't need the the equivalent to Java Interfaces or Abstract Classes, and once you've really made the paradigm shift from chicken typing to duck typing, you &lt;a href="http://blog.objectmentor.com/articles/2007/08/08/craftsman-51-brown-bag-viii-ruby-visitor"&gt;realize that you don't want them&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;What's OpenURI Doing?&lt;/h2&gt;
&lt;p&gt;Because of Trans' question, I decided to look at how OpenURI was using respond_to? in this case. Here's the code in question:&lt;/p&gt;
First the beginnings of the OpenURI::Meta module
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;notextile&gt;&lt;span class="keyword"&gt;module &lt;/span&gt;&lt;span class="module"&gt;OpenURI&lt;/span&gt;
 &lt;span class="keyword"&gt;module &lt;/span&gt;&lt;span class="module"&gt;Meta&lt;/span&gt;
   &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;Meta.init&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;obj&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;src&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="constant"&gt;nil&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="comment"&gt;# :nodoc:&lt;/span&gt;
     &lt;span class="ident"&gt;obj&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;extend&lt;/span&gt; &lt;span class="constant"&gt;Meta&lt;/span&gt;
     &lt;span class="ident"&gt;obj&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;instance_eval&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt;
       &lt;span class="attribute"&gt;@base_uri&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;nil&lt;/span&gt;
       &lt;span class="attribute"&gt;@meta&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;{}&lt;/span&gt;
     &lt;span class="punct"&gt;}&lt;/span&gt;
     &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="ident"&gt;src&lt;/span&gt;
       &lt;span class="ident"&gt;obj&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;status&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;src&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;status&lt;/span&gt;
       &lt;span class="ident"&gt;obj&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;base_uri&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;src&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;base_uri&lt;/span&gt;
       &lt;span class="ident"&gt;src&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;meta&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;each&lt;/span&gt; &lt;span class="punct"&gt;{|&lt;/span&gt;&lt;span class="ident"&gt;name&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;value&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
         &lt;span class="ident"&gt;obj&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;meta_add_field&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;name&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;value&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
       &lt;span class="punct"&gt;}&lt;/span&gt;
     &lt;span class="keyword"&gt;end&lt;/span&gt;
   &lt;span class="keyword"&gt;end&lt;/span&gt;

   &lt;span class="ident"&gt;attr_accessor&lt;/span&gt; &lt;span class="symbol"&gt;:status&lt;/span&gt;
   &lt;span class="ident"&gt;attr_accessor&lt;/span&gt; &lt;span class="symbol"&gt;:base_uri&lt;/span&gt;

   &lt;span class="comment"&gt;# returns a Hash which represents header fields.&lt;/span&gt;
   &lt;span class="comment"&gt;# The Hash keys are downcased for canonicalization.&lt;/span&gt;
   &lt;span class="ident"&gt;attr_reader&lt;/span&gt; &lt;span class="symbol"&gt;:meta&lt;/span&gt;

   &lt;span class="comment"&gt;# MORE STUFF....&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt; &lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Meta.init causes the object passed as the first parameter to extend Meta, if a second parameter is passed, it's presumably also a Meta, and it's meta attributes are copied to obj.&lt;/p&gt;
&lt;p&gt;Now here's the use of chicken typing:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;notextile&gt;&lt;span class="keyword"&gt;module &lt;/span&gt;&lt;span class="module"&gt;OpenURI&lt;/span&gt;
   &lt;span class="comment"&gt;#...&lt;/span&gt;
  &lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Buffer&lt;/span&gt; &lt;span class="comment"&gt;# :nodoc:&lt;/span&gt;
    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;initialize&lt;/span&gt;
      &lt;span class="attribute"&gt;@io&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;StringIO&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;
      &lt;span class="attribute"&gt;@size&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="number"&gt;0&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
    &lt;span class="ident"&gt;attr_reader&lt;/span&gt; &lt;span class="symbol"&gt;:size&lt;/span&gt;

    &lt;span class="constant"&gt;StringMax&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="number"&gt;10240&lt;/span&gt;
    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;str&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
      &lt;span class="attribute"&gt;@io&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="ident"&gt;str&lt;/span&gt;
      &lt;span class="attribute"&gt;@size&lt;/span&gt; &lt;span class="punct"&gt;+=&lt;/span&gt; &lt;span class="ident"&gt;str&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;length&lt;/span&gt;
      &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="constant"&gt;StringIO&lt;/span&gt; &lt;span class="punct"&gt;===&lt;/span&gt; &lt;span class="attribute"&gt;@io&lt;/span&gt; &lt;span class="punct"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="constant"&gt;StringMax&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="attribute"&gt;@size&lt;/span&gt;
        &lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;tempfile&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
        &lt;span class="ident"&gt;io&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;Tempfile&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;('&lt;/span&gt;&lt;span class="string"&gt;open-uri&lt;/span&gt;&lt;span class="punct"&gt;')&lt;/span&gt;
        &lt;span class="ident"&gt;io&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;binmode&lt;/span&gt;
        &lt;span class="constant"&gt;Meta&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;init&lt;/span&gt; &lt;span class="ident"&gt;io&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="attribute"&gt;@io&lt;/span&gt; &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="attribute"&gt;@io&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;respond_to?&lt;/span&gt; &lt;span class="symbol"&gt;:meta&lt;/span&gt;
        &lt;span class="ident"&gt;io&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="attribute"&gt;@io&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;string&lt;/span&gt;
        &lt;span class="attribute"&gt;@io&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;io&lt;/span&gt;
      &lt;span class="keyword"&gt;end&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;

    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;io&lt;/span&gt;
      &lt;span class="constant"&gt;Meta&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;init&lt;/span&gt; &lt;span class="attribute"&gt;@io&lt;/span&gt; &lt;span class="keyword"&gt;unless&lt;/span&gt; &lt;span class="attribute"&gt;@io&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;respond_to?&lt;/span&gt; &lt;span class="symbol"&gt;:meta&lt;/span&gt;
      &lt;span class="attribute"&gt;@io&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt; &lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;A few observations:&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;The variable which is being chicken typed, in both cases, is an instance variable which is set by the initialize method and never rebound.  So there's no concern about checking an object which was handed in, e.g. through a parameter.  We might be concerned that someone outside the class might somehow access it and change it to extend Meta without the Buffer knowing about it, but...&lt;/li&gt;
	&lt;li&gt;The instance variable will &lt;strong&gt;always&lt;/strong&gt; extend Meta after the accessor is called, so the above concern is moot.&lt;/li&gt;
	&lt;li&gt;In the io accessor method, respond_to? is used to prevent re-extending Meta.  This could be avoided by either extending the object referred to by @io in the initialize method, or, if there's a legitimate reason to defer the extension, either adding a state instance variable to indicate that the extension has been done, or just change "unless @io.respond_to? :meta" to "unless @io.kind_of?(Meta)". This is one case where kind_of? is really useful since we're really just checking if code under our control has extended an object under our control, with a particular Module.&lt;/li&gt;
	&lt;li&gt;Finally, a minor point, but I find this code unnecessarily hard to read because of the choice of name 'io for the temporary in the &amp;lt.&amp;lt. method.  Look at the line "io.binmode".  Which io are we talking about?  Is it the io we just assigned, or the result of the io accessor method. Now it happens to be the former, but knowing that requires knowing that the assignment above is what causes the Ruby parser to treat it as a temporary rather than a method send.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Better a Duck Than  a Chicken&lt;/h2&gt;
&lt;p&gt;So in my opinion, it's better to be a bold Duck than a timid Chicken.  Chicken typing is weak, and it really doesn't provide much more than the "warm fuzzies".  Duck type, and build your specs/tests as you code to provide long-term robustness in dynamically typed languages.&lt;/p&gt;</description>
      <pubDate>Mon, 22 Oct 2007 10:43:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:234b2578-fe29-4215-994e-73d9592cfd63</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/10/22/chicken-typing-isnt-duck-typing</link>
      <category>ruby</category>
      <category>best_practices</category>
      <category>ducks</category>
      <category>chickens</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/475</trackback:ping>
    </item>
  </channel>
</rss>
