<?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>Object Mentor Blog: Now 'and' for something completely different.</title>
    <link>http://blog.objectmentor.com/articles/2008/06/26/now-and-for-something-completely-different</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Now 'and' for something completely different.</title>
      <description>&lt;p&gt;My son Justin is working as a Ruby apprentice for my son Micah at &lt;a href="http://www.8thlight.com"&gt;8th light&lt;/a&gt;. We were sitting at the kitchen table, and he showed me a function he was writing.  In the midst of the function I saw this:&lt;/p&gt;


&lt;code&gt;handle_batch(item) and display_batch(item) while items_remaining?&lt;/code&gt;
&lt;br&gt;&lt;br&gt;
I looked hard at this and then I said: &amp;#8220;Justin, I don&amp;#8217;t think you understand what &lt;code&gt;and&lt;/code&gt; does.   

	&lt;p&gt;He said: &amp;#8220;I think I do.&amp;#8221; and he pointed me to a &lt;a href="http://www.rubyinside.com/21-ruby-tricks-902.html"&gt;website&lt;/a&gt; which showed 21 Ruby tricks &amp;#8220;you &lt;em&gt;should&lt;/em&gt; be using in your own code.&amp;#8221;&lt;/p&gt;


	&lt;p&gt;Trick #9 was the use of the &lt;code&gt;and&lt;/code&gt; keyword to couple statements together to make &amp;#8220;one liners&amp;#8221;.  It was billed as a trick that &lt;em&gt;[cough]&lt;/em&gt; &amp;#8220;more confident&amp;#8221; Ruby programmers use.&lt;/p&gt;


	&lt;p&gt;So I got the pickaxe book out and looked up the &lt;code&gt;and&lt;/code&gt; keyword.  I showed Justin where it said that the second clause won&amp;#8217;t be executed if the first clause is &lt;code&gt;false&lt;/code&gt; &lt;em&gt;(or &lt;code&gt;nil&lt;/code&gt;!)&lt;/em&gt;  So if &lt;code&gt;handle_batch&lt;/code&gt; ever returned &lt;code&gt;nil&lt;/code&gt;, then &lt;code&gt;display_batch&lt;/code&gt; would never be called.&lt;/p&gt;


	&lt;p&gt;Warning bells were going off in my head.  This was clearly an unintended use of the &lt;code&gt;and&lt;/code&gt; keyword that could have rather nasty repercussions.  I thought it was somewhat irresponsible for a website that boasted expertise in programming to tell people they should be using a dangerous stunt like this.&lt;/p&gt;


	&lt;p&gt;However, &lt;code&gt;handle_batch&lt;/code&gt; did &lt;em&gt;not&lt;/em&gt; return &lt;code&gt;nil&lt;/code&gt;, so the &lt;code&gt;and&lt;/code&gt; worked well enough in this case; and we had more to do.  So I made my point to Justin and then we kept working, leaving the &lt;code&gt;and&lt;/code&gt; in place.&lt;/p&gt;


	&lt;p&gt;An hour later we were making changes to a function.  Suddenly a whole bunch of our tests broke. (You know what I&amp;#8217;m going to say, don&amp;#8217;t you?)  The failure mode didn&amp;#8217;t make any sense.  Suddenly a whole bunch of processing simply wasn&amp;#8217;t getting done.&lt;/p&gt;


	&lt;p&gt;It was Justin who said: &amp;#8220;Wow, I&amp;#8217;ll bet it&amp;#8217;s that &lt;code&gt;and&lt;/code&gt;.  And it was.  The change we had made had indirectly caused &lt;code&gt;handle_batch&lt;/code&gt; to return a &lt;code&gt;nil&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;OK, this was a fun little story.  You might think the moral is &amp;#8220;don&amp;#8217;t use and for one-liners&amp;#8221; or &amp;#8220;don&amp;#8217;t trust websites that claim expertise&amp;#8221;.  Yes, those would be good conclusions to draw.  But I&amp;#8217;m concerned about something else.&lt;/p&gt;


	&lt;p&gt;The &amp;#8220;and trick&amp;#8221; is &lt;em&gt;clever&lt;/em&gt;.  In programming, &lt;em&gt;clever&lt;/em&gt; != &lt;em&gt;smart&lt;/em&gt;.  (OK, sorry, that was clever&amp;#8230;)  Using a keyword like &lt;code&gt;and&lt;/code&gt; in a manner for which it was not intended, and in a way that is not guaranteed to work in all cases, is risky. It bothers me.&lt;/p&gt;


	&lt;p&gt;By the same token it bothers me that so many Ruby programmers use the &lt;code&gt;||=&lt;/code&gt; trick for lazy initialization.  I know it works.  I know it&amp;#8217;s a standard idiom.  I&amp;#8217;m not trying to stop people from doing it.  Hell, I use it too because it&amp;#8217;s become a standard idiom. But it bothers me nonetheless because it entered our vocabulary of idioms because it was clever trick; and clever little tricks have a way of turning into nasty little surprises.&lt;/p&gt;


	&lt;p&gt;Ruby is a fun and powerful language.  But that doesn&amp;#8217;t mean we should go out of our way to be clever.  We shouldn&amp;#8217;t be eager to adopt quirky little idioms and erudite little stunts just because they are cute, or neat, or nifty.   Code is hard enough to understand without having to think sideways through the next novel application of the ?: operator.&lt;/p&gt;


	&lt;p&gt;Professionals write clear and clean code.  They use their language well.  They use their language efficiently.  But they don&amp;#8217;t aspire to be master tricksters.  Rather, they prove their professionalism by writing code that &lt;em&gt;needs no explanation&lt;/em&gt;.&lt;/p&gt;</description>
      <pubDate>Thu, 26 Jun 2008 13:16:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:44ccdb8f-f0b0-43dc-9695-d7bcd97e6b4f</guid>
      <author>Uncle Bob</author>
      <link>http://blog.objectmentor.com/articles/2008/06/26/now-and-for-something-completely-different</link>
      <category>Uncle Bob's Blatherings</category>
      <category>Dynamic Languages</category>
      <category>Clean Code</category>
    </item>
    <item>
      <title>"Now 'and' for something completely different." by Christina Bacon</title>
      <description>&lt;p&gt;What, using an operator without understanding what it does can cause problems?! To the juniors defense, the [&amp;#8220;tips&amp;#8221;](&lt;a href="http://www.rubyinside.com/21-ruby-tricks-902.html" rel="nofollow"&gt;http://www.rubyinside.com/21-ruby-tricks-902.html&lt;/a&gt;) site didn&amp;#8217;t bother to explain what the operator does.&lt;/p&gt;</description>
      <pubDate>Thu, 24 Jul 2008 09:43:07 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:5dcd5781-e2a5-4fe1-9f9a-60ce38bf0ac7</guid>
      <link>http://blog.objectmentor.com/articles/2008/06/26/now-and-for-something-completely-different#comment-1912</link>
    </item>
    <item>
      <title>"Now 'and' for something completely different." by human i tarian</title>
      <description>&lt;p&gt;(Off topic) Hey Bob, now that the Iraqi premier has called for a timetable for the withdrawal of US troops from Iraq, how do you feel about your cheer-leading for the Iraq war of aggression? Changed your mind yet? Or are you still an obstinate old man refusing to let facts affect your arguments?&lt;/p&gt;</description>
      <pubDate>Mon, 14 Jul 2008 15:44:38 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:1973f420-5b8e-453f-8f20-4451506fd5b4</guid>
      <link>http://blog.objectmentor.com/articles/2008/06/26/now-and-for-something-completely-different#comment-1893</link>
    </item>
    <item>
      <title>"Now 'and' for something completely different." by Michael</title>
      <description>&lt;p&gt;I&amp;#8217;m quite fond of the &amp;#8216;and&amp;#8217; trick.  I don&amp;#8217;t think it&amp;#8217;s clever at all.  The short circuiting of &amp;#8216;and&amp;#8217; is well known in most languages, and if that&amp;#8217;s the behavior you want, it&amp;#8217;s a concise way to get it.&lt;/p&gt;


	&lt;p&gt;In shell scripting and Perl, it&amp;#8217;s a basic idiom used pretty much everywhere.&lt;/p&gt;</description>
      <pubDate>Fri, 11 Jul 2008 15:44:53 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:c30bbabb-c282-4c15-bf10-7b2a37d8b97f</guid>
      <link>http://blog.objectmentor.com/articles/2008/06/26/now-and-for-something-completely-different#comment-1890</link>
    </item>
    <item>
      <title>"Now 'and' for something completely different." by Charles</title>
      <description>&lt;p&gt;Well ruby doesn&amp;#8217;t have the comma operator, but thankfully (unlike eg python) there is no statement / expression dichotomy. Ie:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
a = [1, 2, 3, 4]
until a.empty?
p a.shift
p '-'
end
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;vs&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
a = [1, 2, 3, 4]
(p a.shift; p a.length) until a.empty?
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;Ie we just use statement separators inside a sub expression rather than a comma operator.&lt;/p&gt;</description>
      <pubDate>Fri, 11 Jul 2008 15:43:14 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:f9561975-ec22-4307-a5cb-76e5cb6e6546</guid>
      <link>http://blog.objectmentor.com/articles/2008/06/26/now-and-for-something-completely-different#comment-1889</link>
    </item>
    <item>
      <title>"Now 'and' for something completely different." by Matt S Trout</title>
      <description>&lt;p&gt;And this is why perl provides the comma operator (ruby may, I didn&amp;#8217;t check). Consider, for example&lt;/p&gt;


	&lt;p&gt;(warn(&amp;#8220;foo\n&amp;#8221;), warn(&amp;#8220;bar\n&amp;#8221;)) for 1..2;&lt;/p&gt;


	&lt;p&gt;at my Devel::REPL prompt this gives&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;foo
bar
foo
bar&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;and the comma shows every indication of clearly being a separator; &amp;#38; is a bitwise operator with a return value you might care about, and as such doesn&amp;#8217;t really express the intent of the code.&lt;/p&gt;</description>
      <pubDate>Fri, 11 Jul 2008 01:56:11 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:dd29566b-38f2-4b0a-b72b-3a479972c7af</guid>
      <link>http://blog.objectmentor.com/articles/2008/06/26/now-and-for-something-completely-different#comment-1887</link>
    </item>
    <item>
      <title>"Now 'and' for something completely different." by James Justin Harrell</title>
      <description>&lt;p&gt;It is a well established and perfectly acceptable practice to use &amp;#8216;and&amp;#8217; operators in this manner when the short-circuiting behavior is desired. This is often used in shell scripts where you don&amp;#8217;t want to execute certain commands if a previous command failed. This practice is far too old to be considered clever anymore.&lt;/p&gt;</description>
      <pubDate>Fri, 11 Jul 2008 00:52:59 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:800e11f5-0af9-4980-a231-65dea3a5f497</guid>
      <link>http://blog.objectmentor.com/articles/2008/06/26/now-and-for-something-completely-different#comment-1886</link>
    </item>
    <item>
      <title>"Now 'and' for something completely different." by agnoster</title>
      <description>&lt;p&gt;We shouldn&amp;#8217;t eschew the use of idioms because some people may use them incorrectly.  As was pointed out, that would pretty much rule out pointers.  I use &amp;#8220;A and B&amp;#8221; (or &amp;#8220;A &amp;#38;&amp;#38; B&amp;#8221;, depending on language) specifically because the short-circuit is the logical thing.  If you&amp;#8217;re not intending that behaviour, it&amp;#8217;s not because you&amp;#8217;re using a dangerous idiom &amp;#8211; it&amp;#8217;s because you&amp;#8217;re using a perfectly natural idiom incorrectly.  The fault, dear Brutus, lies not in the stars&amp;#8230;&lt;/p&gt;</description>
      <pubDate>Fri, 11 Jul 2008 00:00:18 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:1885dc94-0f6a-4976-ae76-bf6cffa8267f</guid>
      <link>http://blog.objectmentor.com/articles/2008/06/26/now-and-for-something-completely-different#comment-1885</link>
    </item>
    <item>
      <title>"Now 'and' for something completely different." by Andrew Walker</title>
      <description>&lt;p&gt;Great example. I have been bitten badly by &amp;#8216;clever&amp;#8217; code over the years, some much worse than others. Each time it bites, it hurts because its you who looks bad when it takes a little while to fix something that should be &amp;#8216;simple&amp;#8217;/a &amp;#8216;quick fix&amp;#8217;.&lt;/p&gt;


	&lt;p&gt;Undoubtedly the &amp;#8216;clever&amp;#8217; person who put it in place could fix it quite quickly, but how many times has that person conveniently moved on.&lt;/p&gt;


	&lt;p&gt;Many people in this business like being clever, and that is one of the toughest challenges. Its almost a prerequisite that this kind of lesson be learned the hard way &amp;#8211; as in your example. Leave the offending code in place and teach/coach about the perils of employing clever techniques. Having said that, there is so much literature out there that advocates this kind of thing, you can&amp;#8217;t necessarily blame people. Guess thats the reason I like to have experienced folk around.&lt;/p&gt;


	&lt;p&gt;Looking forward to your book.&lt;/p&gt;</description>
      <pubDate>Thu, 10 Jul 2008 16:28:07 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:5917a414-53c3-4c93-a05d-8be03f281cef</guid>
      <link>http://blog.objectmentor.com/articles/2008/06/26/now-and-for-something-completely-different#comment-1883</link>
    </item>
    <item>
      <title>"Now 'and' for something completely different." by unclebob</title>
      <description>&lt;p&gt;.bq Dumbing everything down to the lowest common denominator is not smart nor clever.&lt;/p&gt;


	&lt;p&gt;Agreed.  However, using language features for unintended purposes is also suspect.&lt;/p&gt;


	&lt;p&gt;As I said, ||= has become a standard idiom, so I have no aversion to it, and use it myself.  I just don&amp;#8217;t like the &lt;em&gt;way&lt;/em&gt; it became a standard idiom&amp;#8230;&lt;/p&gt;</description>
      <pubDate>Sat, 28 Jun 2008 23:49:18 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:09787574-a32f-4ea0-a701-d8f296ac796e</guid>
      <link>http://blog.objectmentor.com/articles/2008/06/26/now-and-for-something-completely-different#comment-1867</link>
    </item>
    <item>
      <title>"Now 'and' for something completely different." by Rimantas</title>
      <description>&lt;p&gt;What if the second action should be performed only if the first action returns not nil? And is very apropriate in this case.
Like &amp;#8220;find something and then display it&amp;#8221;. If you find nothing you cannot display it, right?&lt;/p&gt;


	&lt;p&gt;I do not get your complain about ||= at all.
I&amp;#8217;d arugue that this is not tricky, no quirky and makes code easier
to read once you are familiar with the idiom.
With logic like this we should dismiss C altogether because it has pointers. Imagine, what you can do with that, oh my!.
Dumbing everything down to the lowest common denominator is not smart nor clever.&lt;/p&gt;</description>
      <pubDate>Sat, 28 Jun 2008 16:25:02 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:916490c7-4488-4e8a-8fd7-24efe45d9928</guid>
      <link>http://blog.objectmentor.com/articles/2008/06/26/now-and-for-something-completely-different#comment-1866</link>
    </item>
    <item>
      <title>"Now 'and' for something completely different." by Myles Byrne</title>
      <description>&lt;p&gt;Thanks for writing this up. I had been guilty of using the &lt;code&gt;and&lt;/code&gt; keyword to create one-liners as I always assumed &lt;code&gt;&amp;#38;&amp;#38;&lt;/code&gt; was the short-circuit form of &lt;code&gt;and&lt;/code&gt;. As you&amp;#8217;ve pointed out, they&amp;#8217;re both short-circuit, and besides a subtle precedence difference are essentially aliases to each other.&lt;/p&gt;


	&lt;p&gt;The &lt;code&gt;&amp;#38;&lt;/code&gt; operator (single ampersand) would execute both statements:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;handle_batch(item) &amp;#38; display_batch(item) while items_remaining?&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;While the above is arguably just as tricky it does have the advantage of being familiar to anyone who has done some bash scripting.&lt;/p&gt;</description>
      <pubDate>Sat, 28 Jun 2008 05:30:07 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:f28dad12-a550-46b5-8f5b-228d91aa8b61</guid>
      <link>http://blog.objectmentor.com/articles/2008/06/26/now-and-for-something-completely-different#comment-1864</link>
    </item>
    <item>
      <title>"Now 'and' for something completely different." by yachris</title>
      <description>&lt;p&gt;Three things:&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;You&amp;#8217;re absolutely right about &amp;#8216;clever&amp;#8217; code.  I once read a Randall Schwartz article &lt;em&gt;ostensibly&lt;/em&gt; introducing beginners to Perl&amp;#8230; and having programmed Perl for several years at that point, I &lt;em&gt;could not&lt;/em&gt; figure out what his code was doing without reading the article twice.  It was that obtuse.&lt;/li&gt;
		&lt;li&gt;Your link to 8thlight.com is broken.&lt;/li&gt;
		&lt;li&gt;Do you think it &lt;em&gt;entirely&lt;/em&gt; ethical to have a blurb on your son&amp;#8217;s website with no disclaimer?  Unless it&amp;#8217;s some other Bob Martin&amp;#8230;&lt;/li&gt;
	&lt;/ol&gt;</description>
      <pubDate>Wed, 25 Jun 2008 12:01:27 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:d610bdf0-e595-47e2-ab55-ae53c9282a61</guid>
      <link>http://blog.objectmentor.com/articles/2008/06/26/now-and-for-something-completely-different#comment-1854</link>
    </item>
  </channel>
</rss>
