<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>RubyLearning Blog &#187; Evan Light</title>
	<atom:link href="http://rubylearning.com/blog/author/evanlight/feed/" rel="self" type="application/rss+xml" />
	<link>http://rubylearning.com/blog</link>
	<description>Helping Ruby Programmers become Awesome</description>
	<lastBuildDate>Tue, 24 Apr 2012 22:15:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Let&#8217;s Talk About Conditional Expressions</title>
		<link>http://rubylearning.com/blog/2011/12/21/lets-talk-about-conditional-expressions/</link>
		<comments>http://rubylearning.com/blog/2011/12/21/lets-talk-about-conditional-expressions/#comments</comments>
		<pubDate>Wed, 21 Dec 2011 03:00:07 +0000</pubDate>
		<dc:creator>Evan Light</dc:creator>
				<category><![CDATA[Beginners]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Conditional Expressions]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby programming]]></category>

		<guid isPermaLink="false">http://rubylearning.com/blog/?p=6442</guid>
		<description><![CDATA[This guest post is by Evan Light a test-obsessed developer, the author of several rarely used gems, and the curator of Ruby DCamp. When he&#8217;s not a talking head at conferences, he&#8217;s usually working at home as a freelance developer remotely mentoring a developer, working for one or more startups, playing with open source, keeping [...]<p><a href="http://www.launchbit.com/az/113-209/"><img width="468" height="60" src="http://www.launchbit.com/az-images/113-209/" /></a><br />
<small>(Powered by <a href="http://www.launchbit.com/lb/113-209/">LaunchBit</a>)</small></p>
]]></description>
			<content:encoded><![CDATA[<p></p>
<div class="topsy_widget_data topsy_theme_brick-red" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Frubylearning.com%252Fblog%252F2011%252F12%252F21%252Flets-talk-about-conditional-expressions%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Let%27s%20Talk%20About%20Conditional%20Expressions%22%20%7D);"></div>
<div>
<p class="update">This guest post is by <strong><a href="https://twitter.com/#!/elight">Evan Light</a></strong> a test-obsessed developer, the author of several rarely used gems, and the curator of Ruby DCamp. When he&#8217;s not a talking head at conferences, he&#8217;s usually working at home as a freelance developer remotely mentoring a developer, working for one or more startups, playing with open source, keeping his wife and four cats company, hacking nonsensically, talking at people on the internet, and/or attempting to lose weight (or any combination of the above). What else do you do when you live 3 hours from civilization?</p>
<p class="block"><img style=' float: right; padding: 4px; margin: 0 0 2px 7px;'  class="alignright" src="http://www.gravatar.com/avatar/3c51f636715fb42bc82141702aa92b09?s=125" alt="Evan Light" /> You know what bugs me?  People who don&rsquo;t write idiomatic conditionals in Ruby.</p>
<p>Many folks, especially those who came out of Java, .NET, or C/C++, use and abuse the ternary.  The ternary is a humble little production rule that works like so:</p>
<pre><code>&lt;boolean expression&gt; ? &lt;eval this expression if truthy&gt; : &lt;eval if falsey&gt;
</code></pre>
<p>The ternary can be useful if the truthy and falsey cases are terse and well-named.  However, it&rsquo;s common to see multi-line ternaries or, even worse, nested ternaries.  For example:</p>
<p><script type="text/javascript" src="https://gist.github.com/1448377.js?file=delegation_1.rb"></script></p>
<p>By the way, the above example comes out of Rails.</p>
<p>Accepting that the above is just painful, how could we make it hurt less?  Specifically, let&rsquo;s focus on the nested ternary on Line 7 (accepting that there are other ways that the code can be made clearer).</p>
<p>For starters, let&rsquo;s DRY up these Hash lookups:</p>
<p><script type="text/javascript" src="https://gist.github.com/1448397.js?file=delegation_2.rb"></script></p>
<p>Now let&rsquo;s unravel this into more idiomatic Ruby.  We&rsquo;ll extract the outer ternary into a functional style if-else.  Remember that every expression in Ruby returns the value of its last executed expression.  The if-else returns either the value of the computed String in the <strong><em>if</em></strong> or the empty String <strong><em>else</em></strong>.  It has the advantage of containing more English words and fewer characters that resemble modem line noise or PERL.</p>
<p><script type="text/javascript" src="https://gist.github.com/1448450.js?file=delegation_3.rb"></script></p>
<p>We can get rid of this <strong><em>else</em></strong>.  After all, it doesn&rsquo;t contain any logic.  Let&rsquo;s just make it the default value for <strong><em>method_prefix</em></strong> and then assign a new value only when <strong><em>prefix</em></strong> is truthy.</p>
<p><script type="text/javascript" src="https://gist.github.com/1448492.js?file=delegation_4.rb"></script></p>
<p>Ah so what this code really cares about, first and foremost, is whether <strong><em>prefix</em></strong> is falsey.  When it&rsquo;s falsey, it just returns an empty String!  Otherwise, if it&rsquo;s not, it generates a String possibly using the value of <strong><em>prefix</em></strong> if and only if <strong><em>prefix</em></strong> is not true &ndash; not a boolean.</p>
<p>So why should we try to avoid ternaries?  Because, when used within a complex expression, they tend to lack the clarity of intent of an if-else.  The lack of clarity likely resulted in the less DRY previous implementation.  Once we introduced more clarity, it became obvious how this code could be DRY&rsquo;d up, resulting in better readability.</p>
<p><em>I hope you found this article valuable. Feel free to ask questions and give feedback in the comments section of this post.</em> Thanks!</p>
<p class="update">Subscribe to the waiting list of the free, online &#8220;<a href="http://satishtalim.github.com/webruby/">Intermediate Ruby Course</a>&#8220;. As a bonus, get a free copy of the &#8220;Introduction to Rack&#8221; eBook (.pdf format).</p>
</div>
<p>Technorati Tags: <a href="http://technorati.com/tag/Programming" rel="tag">Programming</a>, <a href="http://technorati.com/tag/Ruby+programming" rel="tag">Ruby programming</a>, <a href="http://technorati.com/tag/Conditional+Expressions" rel="tag">Conditional Expressions</a></p>
Posted by <b>Evan Light</b><p><a href="http://www.launchbit.com/az/113-209/"><img width="468" height="60" src="http://www.launchbit.com/az-images/113-209/" /></a><br />
<small>(Powered by <a href="http://www.launchbit.com/lb/113-209/">LaunchBit</a>)</small></p>

]]></content:encoded>
			<wfw:commentRss>http://rubylearning.com/blog/2011/12/21/lets-talk-about-conditional-expressions/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Do you ponder what to name things in your code?</title>
		<link>http://rubylearning.com/blog/2011/11/30/do-you-ponder-what-to-name-things-in-your-code/</link>
		<comments>http://rubylearning.com/blog/2011/11/30/do-you-ponder-what-to-name-things-in-your-code/#comments</comments>
		<pubDate>Wed, 30 Nov 2011 05:38:29 +0000</pubDate>
		<dc:creator>Evan Light</dc:creator>
				<category><![CDATA[Beginners]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby programming]]></category>

		<guid isPermaLink="false">http://rubylearning.com/blog/?p=6401</guid>
		<description><![CDATA[This guest post is by Evan Light a test-obsessed developer, the author of several rarely used gems, and the curator of Ruby DCamp. When he&#8217;s not a talking head at conferences, he&#8217;s usually working at home as a freelance developer remotely mentoring a developer, working for one or more startups, playing with open source, keeping [...]<p><a href="http://www.launchbit.com/az/113-209/"><img width="468" height="60" src="http://www.launchbit.com/az-images/113-209/" /></a><br />
<small>(Powered by <a href="http://www.launchbit.com/lb/113-209/">LaunchBit</a>)</small></p>
]]></description>
			<content:encoded><![CDATA[<p></p>
<div class="topsy_widget_data topsy_theme_brick-red" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Frubylearning.com%252Fblog%252F2011%252F11%252F30%252Fdo-you-ponder-what-to-name-things-in-your-code%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2Fvy1E0m%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Do%20you%20ponder%20what%20to%20name%20things%20in%20your%20code%3F%22%20%7D);"></div>
<div>
<p class="update">This guest post is by <strong><a href="https://twitter.com/#!/elight">Evan Light</a></strong> a test-obsessed developer, the author of several rarely used gems, and the curator of Ruby DCamp. When he&#8217;s not a talking head at conferences, he&#8217;s usually working at home as a freelance developer remotely mentoring a developer, working for one or more startups, playing with open source, keeping his wife and four cats company, hacking nonsensically, talking at people on the internet, and/or attempting to lose weight (or any combination of the above). What else do you do when you live 3 hours from civilization?</p>
<h2 id="why-should-i-care">Why should I care?</h2>
<p class="block"><img style=' float: right; padding: 4px; margin: 0 0 2px 7px;'  class="alignright" src="http://www.gravatar.com/avatar/3c51f636715fb42bc82141702aa92b09?s=125" alt="Evan Light" /> &#8220;The hell with clean code!<sup class='footnote'><a href='#fn-6401-1' id='fnref-6401-1'>1</a></sup>, you say. &#8220;I&#8217;ve got stuff to get done!&#8221; You beat on keys for a while. Characters appear. The characters don’t make a lot of sense but, hey, the compiler compiles it or the virtual machine interprets it. Things happen. Eventually, an application emerges.</p>
<p>Success? Hell no!</p>
<p>Most of the time, someone has to maintain that pile of crap you just birthed! It may be someone else. It may be you! But it&#8217;s always wise to pretend that the person who will own your code next is an axe-wielding lunatic who knows where you live.</p>
<p>The worst sin that I&#8217;ve seen people make in their code: choosing poor names&#8230; for their classes, methods, literals, you name it.</p>
<p>&#8220;I don&#8217;t have time to sit around thinking about the perfect name for a class! Besides, I know how the code works! I can fix it.&#8221;</p>
<p>Oh, yeah? You may now. But how about in a month? Or three? Or a year? Wait&#8230; will you even be working for this company or on this project in a year? Who inherits this code?</p>
<h2 id="prototypes-spikes-and-other-rationalizations">Prototypes, spikes, and other rationalizations</h2>
<p>Occasionally, developers invoke the &#8220;p&#8221; word: prototype. Prototypes are ugly little creatures that live only a short while.</p>
<p>At least that&#8217;s the myth.</p>
<p>The truth of the matter is that prototypes don&#8217;t exist. Prototypes don&#8217;t get thrown away. What happens when you show a manager working code? He says, &#8220;Great! Here&#8217;s your next feature!&#8221;</p>
<p>&#8220;But&#8230;&#8221;, you start to say.</p>
<p>&#8220;But what? It&#8217;s working! Marvelous! Right, here you are. Next feature!&#8221;</p>
<p>Heard of a &#8220;spike&#8221;? That&#8217;s basically a small prototype that you may not tell management about. You spend maybe 30 minutes working on one. The pain of throwing away the code is lessened. But even then it may offend your sensibilities. And that code is going to be ugly too.</p>
<p>We tell ourselves that the code will be temporary. Most of the time, this just isn&#8217;t so. These techniques are rationalizations for writing ugly code that will likely outlive its intended lifespan.</p>
<h2 id="think-about-the-future">Think about the future!</h2>
<p>So don&#8217;t throw that code away! Don&#8217;t prototype. Don&#8217;t spike<sup class='footnote'><a href='#fn-6401-2' id='fnref-6401-2'>2</a></sup>.</p>
<p>Dave Thomas sometimes describes writing an application as writing a Domain Specific Language. That is, creating an application is akin to developing a semantically meaningful API to describe an application&#8217;s behavior.</p>
<p>Therefore, the smartest thing you can do, when starting a new application, is to cultivate an understanding of the overall problem the application is intended to solve.</p>
<p>How can we do this? Personally, I favor contemporary Test Driven Development techniques:</p>
<p>For a given feature:</p>
<ol>
<li>Describe, in English, what the feature is trying to accomplish</li>
<li>Exercise the API that I wish I&#8217;d write as a test</li>
<li>Make the test pass</li>
<li><strong>Compare 1 &amp; 2</strong></li>
<li>Refactor</li>
</ol>
<p>For instance, consider this simplistic example:</p>
<p><script type="text/javascript" src="https://gist.github.com/1408200.js?file=subscribing_user_spec.rb"></script></p>
<h2 id="names-should-match-intent">Names should match intent</h2>
<p>The English uses the word &#8220;subscribe&#8221;. That&#8217;s the action that I want the user to perform. So I make it the method name because that&#8217;s what I want to tell the user to do. That&#8217;s the message that I want to send the user.</p>
<p>Now what if I need an entity to representation that relationship between a User and a Plan?</p>
<p>I&#8217;ve seen (and written) some egregiously bad code in situations like this. Once upon a time, my initial reaction would be to call such a thing a &#8220;UserPlan&#8221;.</p>
<p>That&#8217;s just vile and disgusting. Sure, it conjoins the two entities but it does so like Siamese twins!</p>
<p>The better answer should be in plain sight. I used the word &#8220;subscribe&#8221; in the description! Call that entity a &#8220;Subscription&#8221;!</p>
<p>But maybe your description was</p>
<p><script type="text/javascript" src="https://gist.github.com/1408202.js?file=alternate_spec_wording.rb"></script></p>
<p>That would probably cause me to write sample code</p>
<p><script type="text/javascript" src="https://gist.github.com/1408203.js?file=alternate_impl.rb"></script></p>
<p>It&#8217;s still valid though I did less to model the relationship between the User and the Plan because words matter and <strong>naming matters</strong>.</p>
<h2 id="counter-example">Counter example</h2>
<p>So what happens when we name things badly?</p>
<p>Is &#8220;UserPlan&#8221; so bad? Perhaps not. But UserPlan only represents a relationship between two entities. Perhaps I need a greater monstrosity to make the point plain.</p>
<p>So what is a SelfpropelledFourWheelGroundVehicle? It&#8217;s pretty plainly a Car/Automobile. But it&#8217;s a disgusting name. Yet these are the kinds of names that people frequently use in their code due to lack of effort!</p>
<p>Or there&#8217;s the other direction: Haskell developers often like to abstract functions such that they have no clear applicability to a domain. That is, their functions will have semantically meaningless variables such as &#8220;x&#8221; and &#8220;xs&#8221;. I&#8217;m told this is because Haskell has its roots in Lambda Calculus. Be that as it may, mathematical variables have only place in code: in implementing a mathematical calculation!</p>
<p>How would you feel maintaining code where the classes, literals, and methods read like that?!</p>
<p>How productive would you be working in such code? If you cannot trust the names in a code base to accurately represent the ideas at work then you need to understand vast swaths of the code base to be the least bit productive!</p>
<h2 id="conclusion">Conclusion</h2>
<p>If you did not before, I hope that you now have a better appreciation why it is worth your time to get the names right in your code. It will make yours, your colleagues&#8217;, and any who later touch your code lives better and more productive.</p>
<p>And if you don&#8217;t use good names, I have an axe and I know where you live.</p>
<p><em>I hope you found this article valuable. Feel free to ask questions and give feedback in the comments section of this post.</em> Thanks!</p>
<p class="update">Subscribe to the waiting list of the free, online &#8220;<a href="http://satishtalim.github.com/webruby/">Intermediate Ruby Course</a>&#8220;.</p>
</div>
<p>Technorati Tags: <a href="http://technorati.com/tag/Programming" rel="tag">Programming</a>, <a href="http://technorati.com/tag/Ruby+programming" rel="tag">Ruby programming</a>, <a href="http://technorati.com/tag/code" rel="tag">code</a></p>
<div class='footnotes'>
<div class='footnotedivider'></div>
<ol>
<li id='fn-6401-1'>Robert Martin also wrote about this topic, and, in part, inspired this diatribe with the first chapter of his book, Clean Code. <span class='footnotereverse'><a href='#fnref-6401-1'>&#8617;</a></span></li>
<li id='fn-6401-2'>Yes, this is a sweeping generalization. Yes, I occasionally spike on code. However, I only do this for technically challenging pieces which is to say very infrequently. And I throw my few spikes away. <span class='footnotereverse'><a href='#fnref-6401-2'>&#8617;</a></span></li>
</ol>
</div>
Posted by <b>Evan Light</b><p><a href="http://www.launchbit.com/az/113-209/"><img width="468" height="60" src="http://www.launchbit.com/az-images/113-209/" /></a><br />
<small>(Powered by <a href="http://www.launchbit.com/lb/113-209/">LaunchBit</a>)</small></p>

]]></content:encoded>
			<wfw:commentRss>http://rubylearning.com/blog/2011/11/30/do-you-ponder-what-to-name-things-in-your-code/feed/</wfw:commentRss>
		<slash:comments>52</slash:comments>
		</item>
	</channel>
</rss>

