<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>SKORKS</title>
	
	<link>http://www.skorks.com</link>
	<description>For the betterment of the software craft...</description>
	<lastBuildDate>Mon, 07 Jun 2010 11:33:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/softwaretechandmore" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="softwaretechandmore" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">softwaretechandmore</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Learning A Software Development Lesson From A Children’s Poem</title>
		<link>http://www.skorks.com/2010/06/learning-a-software-development-lesson-from-a-childrens-poem/</link>
		<comments>http://www.skorks.com/2010/06/learning-a-software-development-lesson-from-a-childrens-poem/#comments</comments>
		<pubDate>Mon, 07 Jun 2010 11:33:12 +0000</pubDate>
		<dc:creator>Alan Skorkin</dc:creator>
				<category><![CDATA[People]]></category>
		<category><![CDATA[lessons]]></category>
		<category><![CDATA[opinions]]></category>

		<guid isPermaLink="false">http://www.skorks.com/?p=1861</guid>
		<description><![CDATA[As I was getting ready for work the other day, some children&#39;s program was on TV. Normally it&#39;s just background noise, but today something made me pay attention. Here is what I heard:

A wise old owl sat in an oak,
		The more he heard, the less he spoke,
		The less he spoke, the more he heard,
		Why aren&#8217;t [...]


<strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2009/09/does-software-development-have-a-culture-of-rewarding-failure/' rel='bookmark' title='Permanent Link: Does Software Development Have A Culture Of Rewarding Failure'>Does Software Development Have A Culture Of Rewarding Failure</a></li>
<li><a href='http://www.skorks.com/2010/04/software-development-and-the-sunk-cost-fallacy/' rel='bookmark' title='Permanent Link: Software Development And The Sunk Cost Fallacy'>Software Development And The Sunk Cost Fallacy</a></li>
<li><a href='http://www.skorks.com/2010/04/on-the-value-of-fundamentals-in-software-development/' rel='bookmark' title='Permanent Link: On The Value Of Fundamentals In Software Development'>On The Value Of Fundamentals In Software Development</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p><img align="left" alt="Shut Up" class="alignleft size-full wp-image-1865" hspace="20" src="http://www.skorks.com/wp-content/uploads/2010/06/shutup.jpg" style="width: 112px; height: 223px;" title="Shut Up" vspace="5" />As I was getting ready for work the other day, some children&#39;s program was on TV. Normally it&#39;s just background noise, but today something made me pay attention. Here is what I heard:</p>
<blockquote>
<p>A wise old owl sat in an oak,<br />
		The more he heard, the less he spoke,<br />
		The less he spoke, the more he heard,<br />
		Why aren&rsquo;t we all like that wise old bird.</p>
</blockquote>
<p>I&#39;ve recently been on a kick of extracting software development related lessons from everyday situations and events &#8211; that little poem immediately made me think of opinions. Software developers are an opinionated bunch, which is fair enough &#8211; comes with the territory. <strong>The trick with opinions though, is knowing when to shut up and listen to those of others</strong>. I could probably stand to follow that particular advice a lot more often :). If you can master that trick you&#39;re guaranteed to learn something. Even if all you learn is how to keep your opinion to yourself, once in a while, that&#39;s still a skill worth having. </p>
<p>	Having said all of that, I do believe that you need to form an opinion about everything that happens, you don&#39;t have to <a href="http://www.codinghorror.com/blog/2008/05/strong-opinions-weakly-held.html" target="_blank">defend your opinion to the death</a>, but you do need to have one. There are important decisions to be made in software projects every day, regarding technology, process, requirements etc. You&#39;re not always going to be the most qualified person to make a decision, but if you let things slide without expressing your point of view &#8211; you deserve everything that happens. In that, software is <a href="http://voices.kansascity.com/node/9164" target="_blank">like government</a>. <strong><em>Having an opinion is the first step towards taking your destiny as a developer into your own hands</em></strong> (<em>this applies equally to teams</em>). And that&#39;s all I have to say about that <a href="http://feeds.feedburner.com/softwaretechandmore">for now</a>.</p>
<p><span style="font-size: 10px; font-family: trebuchet ms;">Image by <a href="http://www.flickr.com/photos/nuagedenuit/151329148/" target="_blank">NuageDeNuit</a></span></p>


<p><strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2009/09/does-software-development-have-a-culture-of-rewarding-failure/' rel='bookmark' title='Permanent Link: Does Software Development Have A Culture Of Rewarding Failure'>Does Software Development Have A Culture Of Rewarding Failure</a></li>
<li><a href='http://www.skorks.com/2010/04/software-development-and-the-sunk-cost-fallacy/' rel='bookmark' title='Permanent Link: Software Development And The Sunk Cost Fallacy'>Software Development And The Sunk Cost Fallacy</a></li>
<li><a href='http://www.skorks.com/2010/04/on-the-value-of-fundamentals-in-software-development/' rel='bookmark' title='Permanent Link: On The Value Of Fundamentals In Software Development'>On The Value Of Fundamentals In Software Development</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=NhQr-fZOyYs:UMO2RU9uQKM:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=NhQr-fZOyYs:UMO2RU9uQKM:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=NhQr-fZOyYs:UMO2RU9uQKM:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=NhQr-fZOyYs:UMO2RU9uQKM:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=NhQr-fZOyYs:UMO2RU9uQKM:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=NhQr-fZOyYs:UMO2RU9uQKM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=NhQr-fZOyYs:UMO2RU9uQKM:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=NhQr-fZOyYs:UMO2RU9uQKM:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/softwaretechandmore/~4/NhQr-fZOyYs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.skorks.com/2010/06/learning-a-software-development-lesson-from-a-childrens-poem/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>When A ‘Mount Fuji’ Question Is Not Really A ‘Mount Fuji’ Question (Are You As Smart As You Think You Are)</title>
		<link>http://www.skorks.com/2010/06/when-a-mount-fuji-question-is-not-really-a-mount-fuji-question-are-you-as-smart-as-you-think-you-are/</link>
		<comments>http://www.skorks.com/2010/06/when-a-mount-fuji-question-is-not-really-a-mount-fuji-question-are-you-as-smart-as-you-think-you-are/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 13:45:23 +0000</pubDate>
		<dc:creator>Alan Skorkin</dc:creator>
				<category><![CDATA[People]]></category>
		<category><![CDATA[interview questions]]></category>
		<category><![CDATA[practice]]></category>

		<guid isPermaLink="false">http://www.skorks.com/?p=1853</guid>
		<description><![CDATA[Many people hate &#39;Mount Fuji&#39; style interview questions. Questions such as:

&#34;How many barbers are there in your home town?&#34;

or

&#34;How would you move Mount Fuji?&#34;

That last one being the most well known of these types of questions and the one from which the rest get their name. These questions were initially popularized by Microsoft, but many [...]


<strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2010/03/how-to-answer-a-programming-interview-question-and-look-good-doing-it/' rel='bookmark' title='Permanent Link: How To Answer A Programming Interview Question And Look Good Doing It'>How To Answer A Programming Interview Question And Look Good Doing It</a></li>
<li><a href='http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/' rel='bookmark' title='Permanent Link: An Interview Question That Prints Out Its Own Source Code (In Ruby)'>An Interview Question That Prints Out Its Own Source Code (In Ruby)</a></li>
<li><a href='http://www.skorks.com/2009/09/the-best-way-to-interview-a-developer/' rel='bookmark' title='Permanent Link: The Best Way To Interview A Developer'>The Best Way To Interview A Developer</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p><img align="left" alt="Mount Fuji" class="alignleft size-full wp-image-1857" hspace="20" src="http://www.skorks.com/wp-content/uploads/2010/06/mountfuji.jpg" style="width: 334px; height: 251px;" title="Mount Fuji" vspace="5" />Many people hate &#39;<em>Mount Fuji</em>&#39; style interview questions. Questions such as:</p>
<blockquote>
<p>&quot;<em>How many barbers are there in your home town?</em>&quot;</p>
</blockquote>
<p>or</p>
<blockquote>
<p>&quot;<a href="http://www.amazon.com/Would-Move-Mount-Microsofts-Puzzle/dp/0316919160" target="_blank"><em>How would you move Mount Fuji?</em></a>&quot;</p>
</blockquote>
<p>That last one being the most well known of these types of questions and the one from which the rest get their name. <strong>These questions were initially popularized by Microsoft</strong>, but many of the most well-known tech companies have used them since. They have recently fallen a bit out of favour, but you&#39;re still likely to come across some if you do few <a href="http://www.skorks.com/2010/03/how-to-answer-a-programming-interview-question-and-look-good-doing-it/" target="_blank">tech interviews</a> here and there.</p>
<p>There is a good reason why many people don&#39;t like these types of questions. Most of the time, they don&#39;t really have a good answer. The question is designed to put a person under pressure (<em>when they can&#39;t immediately see a way to tackle the problem</em>) and see how they handle it, as well as see if they can come up with a creative solution on the spot. Some developers tend to enjoy that kind of challenge, others seem to loath it which is why opinions about these questions are highly polarised. While <strong>I personally don&#39;t think these questions are the be-all-end-all, I do believe they have their value</strong>, but that is not what I want to talk about.</p>
<p>You see the people who hate these questions, hate them with a passion. Whenever an interview contains a &#39;<em>Mount Fuji</em>&#39; or two and people do badly, they know exactly where to cast the blame. &quot;<em>It was all because of the stupid Mount Fuji questions. I was never good at those, plus I wouldn&#39;t want to work for a company that would ask them anyway.</em>&quot; That&#39;s the excuse you give to yourself. Makes you feel better about failing &#8211; which is actually not a bad rationalisation as far as rationalisations go. But, there is an issue with this thinking because <strong>sometimes, a question that sounds like a &#39;<em>Mount Fuji</em>&#39; question isn&#39;t really one at all</strong> and with all the hate we direct towards them we might end up missing it completely. So, what excuse do we give ourselves then, hmmm :)?</p>
<p>What I want to do is present a couple of these &#39;<em>Non-Mount Fuji</em>&#39; questions here. While they might seem like one of &#39;<em>those</em>&#39; at first, they are actually very reasonable questions, relevant, with concrete solutions. In my opinion, very decent interview fodder as well, but you&#39;re likely to fail them just as badly as any &#39;Mount Fuji&#39; problem if you haven&#39;t <a href="http://www.skorks.com/2010/05/on-personal-skills-and-how-even-riding-a-bike-is-not-like-riding-a-bike/" target="_blank">practiced</a>, so let&#39;s have a look.</p>
<p>Here is the first:</p>
<blockquote>
<p>There is a village full of people. One day the priest gathers all the villagers together and declares that their God has told him that some among the villagers are sinners. All sinners will be marked with a sign on their forehead so that all other people will be able to see if a person is a sinner or not, but noone will know if there is a mark on their own forehead. Furthermore, the mark will not be visible in the mirror and one person is forbidden from telling another if they are a sinner. The villagers must work out which among them are sinners at which point all sinners are to leave the village. The village has been given one week and if at the end of the week, there are still sinners present, the whole village will be destroyed.</p>
<p>The villagers go about their own business during the day, however the whole village gathers in the square at the end of every day so that everyone can see if there are still any sinners left among them. After the third such gathering all the sinners pack up their stuff and leave the village.</p>
<p>How many sinners were there? How did you arrive at that number?</p>
</blockquote>
<p>This is a <a href="http://en.wikipedia.org/wiki/Deductive_reasoning" target="_blank">deductive reasoning</a> question. <strong>We tend to use this kind of reasoning all the time when writing software</strong>, so the question is highly relevant to the software development profession. This question has many different forms and is pretty common, so you may have heard it before. If you have you should be able to answer it easily right :)?</p>
<p>Here is the second:</p>
<blockquote>
<p>You are one of several recently arrested prisoners. The warden, a deranged computer scientist, makes the following announcement:</p>
<p>You may meet together today and plan a strategy, but after today you will be in isolated cells and have no communication with one another.</p>
<p>I have setup a &quot;switch room&quot; which contains a light switch, which is either on or off. The switch is not connected to anything.</p>
<p>Every now and then, I will select one prisoner at random to enter the &quot;switch room&quot;. This prisoner may throw the switch (from on to off, or vice-versa), or may leave the switch unchanged. Nobody else will ever enter this room.</p>
<p>Each prisoner will visit the &quot;switch room&quot; arbitrarily often. More precisely, for any N, eventually each of you will visit the &quot;switch room&quot; at least N times.</p>
<p>At any time, any of you may declare: &quot;we have all visited the &#39;switch room&#39; at least once&quot;. If the claim is correct, I will set you free. If the claim is incorrect, I will feed all of you to the crocodiles. Choose wisely!</p>
<ol>
<li>Devise a winning strategy when you know that the initial state of the switch is off.</li>
<li>Devise a winning strategy when you do not know whether the initial state of the switch is on or off.</li>
</ol>
</blockquote>
<p><strong>This is a distributed coordination problem</strong>. The reasoning used to solve this one is relevant when it comes to both distributed computing and concurrency. Infact, I &#39;<em>stole</em>&#39; this question from Chapter 1 of the &quot;<a href="http://www.amazon.com/Art-Multiprocessor-Programming-Maurice-Herlihy/dp/0123705916" target="_blank">The Art of Multiprocessor Programming</a>&quot;, which is a great book I&#39;ve been browsing recently (<em>it really is a very interesting book and I will likely be going through it in a lot more detail, don&#39;t worry though, you&#39;ll read about it right here when that happens :)</em>). In my opinion this is an even better question than the last, it feels more &#39;<em>computery</em>&#39; for whatever reason. More than that, once you&#39;ve worked it out, the solution can pretty easily be expressed in code to validate your reasoning (<em>which is exactly what I did</em>). Infact, with a little bit of creative thinking, even the first question can be validated through some code. If you can express something in code it&#39;s a winner as far as I am concerned.</p>
<h2>So What Do We Do With These?</h2>
<p style="text-align: center;"><img align="middle" alt="Brain" class="aligncenter size-full wp-image-1858" src="http://www.skorks.com/wp-content/uploads/2010/06/brain.jpg" style="width: 277px; height: 237px;" title="Brain" vspace="3" /></p>
<p><strong>What I want you to do is use this as an opportunity to practice</strong>. It doesn&#39;t really even matter if you&#39;re likely to ever get this kind of question in an interview; these are good questions to give the old programming brain a bit of a workout. Not only is this an opportunity to problem-solve, but it can also be an opportunity to write some quick, interesting code. After going through this exercise, if you ever do see these kinds of questions in an interview, you&#39;ll hopefully be able to distinguish them from the &#39;<em>hated Mount Fuji</em>&#39; ones :).</p>
<p>So, <strong>get your thinking caps on and try to solve these questions &#8211; then post your solutions in the comments below</strong>. The first person to get everything right gets a prize, actually I lie, there is no prize, but apparently <a href="http://www.codinghorror.com/blog/2010/06/the-vast-and-endless-sea.html" target="_blank">incentives don&#39;t work</a> anyway, so it doesn&#39;t matter. What you will get is a decent mental workout and the satisfaction of solving an interesting problem. Don&#39;t worry if you can&#39;t get it though, you&#39;ll get better with practice as long as you give it a go. Anyway, I won&#39;t leave you in the lurch, I&#39;ll <a href="http://feeds.feedburner.com/softwaretechandmore">post my solutions to both of those questions in a few days</a>, including the code I wrote to validate the second one (<em>I might even write some code for the first one if I get time</em>). <strong>If you&#39;re really brave, then also post how long it took you to work out the answer</strong> (<em>it doesn&#39;t really matter, getting these right at all would be a very decent effort, but if you want to show everyone how much of a genius you really are, this is your chance :)</em>). I am really looking forward to seeing what you come up with.</p>
<p><span style="font-size: 10px; font-family: trebuchet ms;">Images by <a href="http://www.flickr.com/photos/molas/113515134/" target="_blank">Molas</a> and <a href="http://www.flickr.com/photos/lapolab/2403693037/" target="_blank">&quot;lapolab&quot;</a></span></p>


<p><strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2010/03/how-to-answer-a-programming-interview-question-and-look-good-doing-it/' rel='bookmark' title='Permanent Link: How To Answer A Programming Interview Question And Look Good Doing It'>How To Answer A Programming Interview Question And Look Good Doing It</a></li>
<li><a href='http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/' rel='bookmark' title='Permanent Link: An Interview Question That Prints Out Its Own Source Code (In Ruby)'>An Interview Question That Prints Out Its Own Source Code (In Ruby)</a></li>
<li><a href='http://www.skorks.com/2009/09/the-best-way-to-interview-a-developer/' rel='bookmark' title='Permanent Link: The Best Way To Interview A Developer'>The Best Way To Interview A Developer</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=P1H6cP6-ATI:F_7WcKKvhKc:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=P1H6cP6-ATI:F_7WcKKvhKc:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=P1H6cP6-ATI:F_7WcKKvhKc:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=P1H6cP6-ATI:F_7WcKKvhKc:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=P1H6cP6-ATI:F_7WcKKvhKc:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=P1H6cP6-ATI:F_7WcKKvhKc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=P1H6cP6-ATI:F_7WcKKvhKc:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=P1H6cP6-ATI:F_7WcKKvhKc:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/softwaretechandmore/~4/P1H6cP6-ATI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.skorks.com/2010/06/when-a-mount-fuji-question-is-not-really-a-mount-fuji-question-are-you-as-smart-as-you-think-you-are/feed/</wfw:commentRss>
		<slash:comments>60</slash:comments>
		</item>
		<item>
		<title>On Personal Skills And How Even Riding A Bike Is Not Like Riding A Bike</title>
		<link>http://www.skorks.com/2010/05/on-personal-skills-and-how-even-riding-a-bike-is-not-like-riding-a-bike/</link>
		<comments>http://www.skorks.com/2010/05/on-personal-skills-and-how-even-riding-a-bike-is-not-like-riding-a-bike/#comments</comments>
		<pubDate>Mon, 31 May 2010 13:19:37 +0000</pubDate>
		<dc:creator>Alan Skorkin</dc:creator>
				<category><![CDATA[People]]></category>
		<category><![CDATA[personal growth]]></category>
		<category><![CDATA[skills]]></category>

		<guid isPermaLink="false">http://www.skorks.com/?p=1844</guid>
		<description><![CDATA[It seems that in this industry we&#39;re operating under the false assumption that once we learn (or hear about) a skill, it is with us for good. You only have to look at a typical developer&#39;s resume for an example &#8211; it is truly buzzword central. I mean c&#39;mon, just what exactly do you remember [...]


<strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2010/03/you-dont-need-math-skills-to-be-a-good-developer-but-you-do-need-them-to-be-a-great-one/' rel='bookmark' title='Permanent Link: You Don&#8217;t Need Math Skills To Be A Good Developer But You Do Need Them To Be A Great One'>You Don&#8217;t Need Math Skills To Be A Good Developer But You Do Need Them To Be A Great One</a></li>
<li><a href='http://www.skorks.com/2009/08/building-software-development-expertise-using-the-dreyfus-model/' rel='bookmark' title='Permanent Link: Building Software Development Expertise &#8211; Using The Dreyfus Model'>Building Software Development Expertise &#8211; Using The Dreyfus Model</a></li>
<li><a href='http://www.skorks.com/2010/04/on-the-mastery-of-teaching/' rel='bookmark' title='Permanent Link: On The Mastery Of Teaching'>On The Mastery Of Teaching</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p><img align="left" alt="Riding" class="alignleft size-full wp-image-1847" hspace="20" src="http://www.skorks.com/wp-content/uploads/2010/05/riding.jpg" style="width: 204px; height: 308px;" title="Riding" vspace="5" />It seems that in this industry we&#39;re operating under the false assumption that once we learn (<em>or hear about</em>) a skill, it is with us for good. You only have to look at a typical developer&#39;s resume for an example &#8211; it is truly buzzword central. I mean c&#39;mon, just what exactly do you remember about <a href="http://www.w3schools.com/xquery/default.asp" target="_blank">XQuery</a> or <a href="http://ibatis.apache.org/" target="_blank">iBATIS</a> at this point? I sometimes look at some of my own resumes I have used in the past and can&#39;t help being impressed &#8211; if only I were really that awesome :). If you&#39;re honest with yourself you can quickly classify all those skills into 3 categories:</p>
<ol>
<li><em>Stuff you have used recently and know pretty well</em></li>
<li><em>Stuff you used to know pretty well, but haven&#39;t used for ages</em></li>
<li><em>Stuff you&#39;ve had some exposure to, but don&#39;t really remember much about</em></li>
</ol>
<p>Only the skills and tech that end up in the first group truly deserve a place in that buzzword list. The problem is that <strong>we don&#39;t tend to prune skills/tech off our resumes all that often</strong>. More stuff makes us sound more impressive &#8211; who wants to mess with that. This would actually have been ok if we made a conscious decision to include neglected and forgotten skills for marketing purposes, but the sad thing is the vast majority of developers/people truly believe that all those skills deserve a place.</p>
<h2>The Power Of The Mind</h2>
<p>When we want something to be true, <a href="http://www.pointsincase.com/columns/simonne/3-12-06.htm" target="_blank">we&#39;re really good at convincing ourselves</a> that it actually is. Companies do it all the time, they want to be the market leader so they portray/see themselves as the market leader &#8211; the reality of the situation doesn&#39;t matter. The same is true for individuals. We have some minor exposure to a skill and we convince ourselves that we&#39;re better than a total novice. This may be true initially, but without some reinforcement, this quickly ceases to be the case. That however is irrelevant, we already believe we have some level of expertise and so the buzzword list grows. What&#39;s even more interesting are the tricks our mind plays on us when it comes to skills we actually did have some expertise in. You vividly remember when C, Smalltalk or XSLT was your bread and butter, it seems like only yesterday. But, it wasn&#39;t yesterday, it was years ago, sometimes many years &#8211; <strong>you would be surprised how far those skills can degrade with time</strong>. On some level we&#39;re aware of this, after all we&#39;re not stupid, but we tell ourselves that it doesn&#39;t matter, we&#39;re just a little rusty, we can pick it all up again in no time &#8211; after all, <a href="http://blog.fishsoup.net/2006/06/18/just-like-riding-a-bike/" target="_blank">it&#39;s just like riding a bike</a>. Well, let me tell you something about riding bikes.</p>
<blockquote>
<h3>Just Like Riding A Bike</h3>
<p>I learned to ride a bike when I was very young and spent quite a large proportion of my childhood on one. It was simply the thing that we did during summer. I was pretty good (<em>even if I do say so myself :)</em>), that&#39;s what constant practice will do for you. Then, my family moved to Australia and I didn&#39;t ride for many years, until, during one fateful school camp, mountain biking was on the list of activities we had to participate in. I fully expected to ace that activity, my riding memories were still vivid in my mind. <strong>The reality was I sucked rather badly</strong>. Oh, I could ride of course, you never forget the absolute basics, but that was the only thing I really felt confident about. I could no longer judge properly how fast I could take the corners without falling over. I couldn&#39;t tell how fast I could safely go downhill, so I took it easy. Riding in a pack with other people seemed scary and unnatural and after only a short while my legs were aching and I was out of breath. Sure, the basics remained, but all the skills, which are built through continued practice and reinforcement, were no longer there. I was essentially reduced to a rank novice.</p>
</blockquote>
<h2>The Harsh Truth</h2>
<p>Every skill is the same, <a href="http://en.wikipedia.org/wiki/Practice_%28learning_method%29" target="_blank">without continued usage and practice they quickly begin to degrade</a>. Your mind begins to page-out the knowledge and experience to make room for other information. Some goes into &quot;<em>long term storage</em>&quot;, some is lost completely, but the net result is you degrade towards &#39;<em>noobhood</em>&#39;. <strong>The longer you leave a skill untouched, the more you mind will page-out</strong>. It takes longer for skills you were more familiar with and some knowledge is reinforced in other ways so that you never really loose it; but the hard-earned expertise, the things that make you more than just mediocre can disappear with surprising speed. This is both sad and dangerous. It is sad because in software, we devote inordinate amounts of time and effort to acquire and master the skills we use in our work. When the pace of our industry forces us into using wholly different sets of skills/technologies all that time and effort begins to go to waste. Previously useful skills get relegated into disuse and only see the light of day in the resume buzzword section. This is especially sad considering that the &quot;<em>useless</em>&quot; old-school skills are often not quite so useless after all. Ruby is all the rage these days, but to make things go fast, what are all the native extensions written in &#8211; C, so what&#39;s old-school anymore? It is dangerous, because we absolutely refuse to acknowledge that we may not be as expert in some skills as we used to be. Sure <strong>we might loudly proclaim how we have forgotten everything about skill/technology X, but deep down we still believe that we can give these kids a run for their money</strong> when it comes to X (<em>I am not precisely sure which kids and what money, but you get the picture :)</em>). This is why we proudly retain X in the buzzword section, but if someone really needed that expertise in a hurry (<em>like your employer for example</em>), would you be able to deliver? I am certain you&#39;ll be able to get it eventually, but so could any other reasonably competent person, there is a difference between that and <a href="http://www.skorks.com/2009/08/building-software-development-expertise-using-the-dreyfus-model/" target="_blank">expert knowledge</a>.</p>
<p style="text-align: center;"><img align="middle" alt="Expert" class="aligncenter size-full wp-image-1848" src="http://www.skorks.com/wp-content/uploads/2010/05/expert.jpg" style="width: 315px; height: 216px;" title="Expert" vspace="3" /></p>
<h2>So What Are We Gonna Do About It</h2>
<p>We can simply be scrupulously honest and remove all the skills and tech we&#39;re not sure about from the resume. But that actually helps noone, despite what I said above, you with your &quot;<em>rusty</em>&quot; skills, are still better than a complete beginner when it comes to those areas. So, by removing that stuff from your resume you&#39;re actually underselling yourself &#8211; it is wasteful and an easy way out. Rather than doing that, why not make sure your hard-earned skills and knowledge never get quite as rusty again.</p>
<p>As developers, <strong>we tend to do a lot of practice and self-study anyway, but most of us do it in a very diffuse and unfocused fashion</strong>. We learn about the latest and greatest things that strike our fancy and we practice by building stuff that catches our interest. All of that is fine as far as it goes, but we can do a lot better. Here is what I suggest. Engage a in a little self-examination by taking all your skills and sorting them into four buckets:</p>
<ul>
<li><strong>Core skills</strong> &#8211; these are the skills you consider primary to the direction you want your career to go. You want to make sure you become increasingly proficient with these and so should devote the most time to practicing them. There will only be a few of these at most (<em>there is only so much time in a day</em>).</li>
<li><strong>Supporting skills</strong> &#8211; these skills support and underpin your core skills. You want to practice these to a lesser extent than the core skills, but you still want to see slow and steady growth. If chosen correctly many of your supporting skills will get a work-out as you practice your core skills, but you should still make sure you devote some time to them exclusively.</li>
<li><strong>Peripheral skills</strong> &#8211; these are the skills that you consider valuable and worth maintaining. You&#39;re not looking to grow these skills, but you don&#39;t really want to lose them either. You want to devote enough time to these so that they don&#39;t fall into disuse.</li>
<li><strong>Misc</strong> &#8211; this is the bucket for &quot;<em>the rest</em>&quot;. The truth is you can&#39;t be an expert in everything, so this should contain the skills that you believe will add the least value to your development going forward.</li>
</ul>
<p>You essentially end up devoting you practice and study time to your core, supporting and peripheral skills on a rotating basis, dividing your time between all of them based on their importance. It is difficult to sort your skills in this fashion (<em>and even to work out what constitutes a skill, is building web apps a skill, or should it be specified into building Rails apps or alternatively generalised into being an HTTP protocol expert</em>), so it might be useful to get some help, but it is an exercise that is eminently worth going through. What it gives you is a set of goals and a strategic framework for where you want to take your skills and your career. Rather than being slave to the latest trends when it comes to your practice, it will allow you to have some focus which can only make your self-study that much more productive (<em>you&#39;ll be able to ignore the fads because you know what you need to focus on and work towards</em>). The side benefit of this is that after a while, you will be a lot more confident about the buzzword section of you resume and be able to prune it without making it look sparse.</p>
<p>So, which skills should be your core and which should be supporting? This will be different for everyone, I&#39;ve already told you what I think about <a href="http://www.skorks.com/2010/04/on-the-value-of-fundamentals-in-software-development/" target="_blank">fundamentals</a>, so that is something to go on, but ultimately it is up to you to work out what knowledge and skills you want to devote the most time to depending on where you want to take your career. It is perhaps worth exploring this in a lot more detail (<em>it is fine to have a strategic view, but it doesn&#39;t actually tell you what to do :)</em>) and I might do this in a later post. Of course the other side of the coin is this, even if you do work out what skills you want to focus on, how do you actually go about practicing them, do you just write a lot of code? What if some of the skills I value are not coding related? That aspect of it is what I would call <strong>the tactical view of your growth as a developer &#8211; here it is all about deliberate practice</strong> and that is something I will <a href="http://feeds.feedburner.com/softwaretechandmore">definitely explore further in subsequent posts</a>. Stay tuned.</p>
<p><span style="font-size: 10px; font-family: trebuchet ms;">Images by <a href="http://www.flickr.com/photos/itzafineday/2622325466/" target="_blank">ItzaFineDay</a> and <a href="http://www.flickr.com/photos/raster/3380860520/" target="_blank">Pete Prodoehl</a></span></p>


<p><strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2010/03/you-dont-need-math-skills-to-be-a-good-developer-but-you-do-need-them-to-be-a-great-one/' rel='bookmark' title='Permanent Link: You Don&#8217;t Need Math Skills To Be A Good Developer But You Do Need Them To Be A Great One'>You Don&#8217;t Need Math Skills To Be A Good Developer But You Do Need Them To Be A Great One</a></li>
<li><a href='http://www.skorks.com/2009/08/building-software-development-expertise-using-the-dreyfus-model/' rel='bookmark' title='Permanent Link: Building Software Development Expertise &#8211; Using The Dreyfus Model'>Building Software Development Expertise &#8211; Using The Dreyfus Model</a></li>
<li><a href='http://www.skorks.com/2010/04/on-the-mastery-of-teaching/' rel='bookmark' title='Permanent Link: On The Mastery Of Teaching'>On The Mastery Of Teaching</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=YJJcuqPrU9E:qtNniBepT5c:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=YJJcuqPrU9E:qtNniBepT5c:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=YJJcuqPrU9E:qtNniBepT5c:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=YJJcuqPrU9E:qtNniBepT5c:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=YJJcuqPrU9E:qtNniBepT5c:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=YJJcuqPrU9E:qtNniBepT5c:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=YJJcuqPrU9E:qtNniBepT5c:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=YJJcuqPrU9E:qtNniBepT5c:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/softwaretechandmore/~4/YJJcuqPrU9E" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.skorks.com/2010/05/on-personal-skills-and-how-even-riding-a-bike-is-not-like-riding-a-bike/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>How To Be A Real Elite Programmer And Make Sure Everybody Knows It</title>
		<link>http://www.skorks.com/2010/05/how-to-be-a-real-elite-programmer-and-make-sure-everybody-knows-it/</link>
		<comments>http://www.skorks.com/2010/05/how-to-be-a-real-elite-programmer-and-make-sure-everybody-knows-it/#comments</comments>
		<pubDate>Sat, 29 May 2010 15:09:01 +0000</pubDate>
		<dc:creator>Alan Skorkin</dc:creator>
				<category><![CDATA[People]]></category>
		<category><![CDATA[joke]]></category>
		<category><![CDATA[programmers]]></category>

		<guid isPermaLink="false">http://www.skorks.com/?p=1837</guid>
		<description><![CDATA[

Real elite programmers can&#39;t have distractions like kids and spouses in their life. Cut your family out of your life to maximize coding time. While you&#39;re there cut your friends out as well to maximize coding time even more.
Real elite programmers don&#39;t have hobbies that aren&#39;t coding. If you still engage in activities that don&#39;t [...]


<strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2010/03/the-difference-between-a-developer-a-programmer-and-a-computer-scientist/' rel='bookmark' title='Permanent Link: The Difference Between A Developer, A Programmer And A Computer Scientist'>The Difference Between A Developer, A Programmer And A Computer Scientist</a></li>
<li><a href='http://www.skorks.com/2008/09/the-real-measure-of-code-quality/' rel='bookmark' title='Permanent Link: The Real Measure Of Code Quality'>The Real Measure Of Code Quality</a></li>
<li><a href='http://www.skorks.com/2010/02/how-to-become-a-spammer-as-a-programmer-regardless-of-people-following-you-on-twitter/' rel='bookmark' title='Permanent Link: How To Become A Spammer (as a programmer) Regardless Of People Following You On Twitter'>How To Become A Spammer (as a programmer) Regardless Of People Following You On Twitter</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p style="text-align: center;"><img align="middle" alt="Elite" class="aligncenter size-full wp-image-1840" src="http://www.skorks.com/wp-content/uploads/2010/05/elite.jpg" style="width: 338px; height: 282px;" title="Elite" vspace="3" /></p>
<ol>
<li>Real elite programmers can&#39;t have distractions like kids and spouses in their life. Cut your family out of your life to maximize coding time. While you&#39;re there cut your friends out as well to maximize coding time even more.</li>
<li>Real elite programmers don&#39;t have hobbies that aren&#39;t coding. If you still engage in activities that don&#39;t involve a computer, this will have to change. If you find yourself burning out, just man-up and push through it.</li>
<li>Real elite programmers do their best work at night. Sleep is for wussies.</li>
<li>Real elite programmers hate their day job. Otherwise they would have nothing to complain about to other real elite programmers.</li>
<li>Real elite programmers eat pizza and drink Dr. Pepper. No other food is allowed, if you have fruit and vegetables in the house, they will have to go &#8211; stick to the diet of champions.</li>
<li>Real elite programmers don&#39;t read about programming, they DO the programming. Don&#39;t bother with books, just keep programming. Remember, you know best, those book-writing wimps are just a bunch of noobs, they should have been coding instead of writing, you&#39;re way better than that.</li>
<li>Real elite programmers are always right. This makes sense since everyone else is just a stupid noob.</li>
<li>Real elite programmers already know everything that is worth knowing. All the other knowledge is useless and only noobs would be interested in it.</li>
<li>Real elite programmers do not acknowledge anyone who is not a real elite programmer.</li>
<li>Real elite programmers know when to say &quot;RTFM&quot; in response to a question. This is a good answer to any question &#8211; noobs should know better than to ask stupid questions in the first place.</li>
<li>Real elite programmers are on social media. This is allowed, since it involves a computer. It is infact their duty since there are so many people on social media who need to be corrected about all things programming.</li>
<li>Real elite programmers complain loudly about the lack of in-depth programming content on social media. They are however too elite to provide this content.</li>
<li>Real elite programmers know when to say &quot;TL;DR&quot; in response to any in-depth programming content on social media.</li>
<li>Real elite programmers are particularly offended by minor inconsistencies in grammar, punctuation, colour schemes as well as any other subjective stylistic choices.</li>
<li>Real elite programmers never create any code for others to use. They have already told everyone they are awesome; there is no need to prove it.</li>
<li>Real elite programmers know how to put argumentative noobs in their place. Stupid noobs must be told they are stupid and ugly if they have the audacity to question elite wisdom.</li>
<li>Real elite programmers condescend to anyone who is not interested in whatever the real elite programmer is currently interested in.</li>
<li>Real elite programmers particularly despise non-programmers, they are worse than stupid noobs.</li>
<li>Real elite programmers use the most complicated and roundabout techniques to explain simple concepts. All must know how smart the real elite programmer is.</li>
</ol>
<p>
	I do hope you realise I am being facetious here. However, there is a grain of truth in every joke. I have seen instances of this type of behaviour and have been guilty of engaging in some of it myself from time to time &#8211; you probably have as well. Just something to watch out for.</p>
<p><span style="font-size: 10px; font-family: trebuchet ms;">Image by <a href="http://www.flickr.com/photos/theamarand/" target="_blank">Amarand Agasi</a></span></p>


<p><strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2010/03/the-difference-between-a-developer-a-programmer-and-a-computer-scientist/' rel='bookmark' title='Permanent Link: The Difference Between A Developer, A Programmer And A Computer Scientist'>The Difference Between A Developer, A Programmer And A Computer Scientist</a></li>
<li><a href='http://www.skorks.com/2008/09/the-real-measure-of-code-quality/' rel='bookmark' title='Permanent Link: The Real Measure Of Code Quality'>The Real Measure Of Code Quality</a></li>
<li><a href='http://www.skorks.com/2010/02/how-to-become-a-spammer-as-a-programmer-regardless-of-people-following-you-on-twitter/' rel='bookmark' title='Permanent Link: How To Become A Spammer (as a programmer) Regardless Of People Following You On Twitter'>How To Become A Spammer (as a programmer) Regardless Of People Following You On Twitter</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=sV0B33Fzc_o:0U4QyTOBFUI:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=sV0B33Fzc_o:0U4QyTOBFUI:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=sV0B33Fzc_o:0U4QyTOBFUI:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=sV0B33Fzc_o:0U4QyTOBFUI:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=sV0B33Fzc_o:0U4QyTOBFUI:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=sV0B33Fzc_o:0U4QyTOBFUI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=sV0B33Fzc_o:0U4QyTOBFUI:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=sV0B33Fzc_o:0U4QyTOBFUI:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/softwaretechandmore/~4/sV0B33Fzc_o" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.skorks.com/2010/05/how-to-be-a-real-elite-programmer-and-make-sure-everybody-knows-it/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
		<item>
		<title>Ruby Procs And Lambdas (And The Difference Between Them)</title>
		<link>http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them/</link>
		<comments>http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them/#comments</comments>
		<pubDate>Thu, 27 May 2010 14:08:09 +0000</pubDate>
		<dc:creator>Alan Skorkin</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[procs]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.skorks.com/?p=1825</guid>
		<description><![CDATA[As you know, I am a big believer in knowing the basics. I find it is especially valuable to go back to the fundamentals of whatever skill you&#39;re trying to master periodically, as you gain more experience. Somehow you&#39;re always able to extract something valuable from the experience due to the extra insight you&#39;ve acquired [...]


<strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2010/03/timing-ruby-code-it-is-easy-with-benchmark/' rel='bookmark' title='Permanent Link: Timing Ruby Code &#8211; It Is Easy With Benchmark'>Timing Ruby Code &#8211; It Is Easy With Benchmark</a></li>
<li><a href='http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/' rel='bookmark' title='Permanent Link: Ruby Access Control &#8211; Are Private And Protected Methods Only A Guideline?'>Ruby Access Control &#8211; Are Private And Protected Methods Only A Guideline?</a></li>
<li><a href='http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/' rel='bookmark' title='Permanent Link: Using Ruby Blocks And Rolling Your Own Iterators'>Using Ruby Blocks And Rolling Your Own Iterators</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p><img align="left" alt="Lambda" class="alignleft size-full wp-image-1831" hspace="20" src="http://www.skorks.com/wp-content/uploads/2010/05/lambda.jpg" style="width: 328px; height: 246px;" title="Lambda" vspace="5" />As you know, I am a big believer in<a href="http://www.skorks.com/2010/04/on-the-value-of-fundamentals-in-software-development/" target="_blank"> knowing the basics</a>. I find it is especially valuable to go back to the fundamentals of whatever skill you&#39;re trying to master periodically, as you gain more experience. Somehow you&#39;re always able to extract something valuable from the experience due to the extra insight you&#39;ve acquired along the way. Recently I&#39;ve been looking more closely at some Ruby concepts that I may have glossed over when I was learning the language initially, so I will likely be writing about this in <a href="http://feeds.feedburner.com/softwaretechandmore">some of my upcoming posts</a>. I thought I would begin with one of the most powerful features of Ruby &#8211; <strong>procs </strong>(<em>and lambdas :)</em>).</p>
<h2>What&#39;s So Good About Procs?</h2>
<p>You know how <a href="http://www.ruby-lang.org/en/about/" target="_blank">everything in Ruby is an object</a>, well, as it turns out that&#39;s not quite true. <a href="http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/" target="_blank">Ruby blocks</a> are <a href="http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html" target="_blank">not objects</a>! We can discuss the implications of that, but let&#39;s refrain, accept it as fact and move on. So, blocks are not objects, but you can turn them into objects without too much trouble. We do this by wrapping our block in an instance of the <a href="http://ruby-doc.org/core/classes/Proc.html" target="_blank">Proc</a> class (<em>more on this shortly</em>). This is really great since it turns our block into a <a href="http://en.wikipedia.org/wiki/First-class_function" target="_blank">first class function</a>, which in turn allows Ruby to support <a href="http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/" target="_blank">closures</a> and <strong>once a language has closures, you can do all sorts of interesting things</strong> like making use of various functional concepts. That however is a story for another time, before we dive deep into those areas, let us really understand how Procs work.</p>
<h2>Procs And Lambdas In Detail</h2>
<p>There are three ways to create a Proc object in Ruby. Well, actually there are four ways, but the last one is implicit. Here they are:</p>
<ol>
<li><strong>Using Proc.new</strong>. This is the standard way to create any object, you simply need to pass in a block and you will get back a Proc object which will run the code in the block when you invoke its call method.

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">proc_object = <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;I am a proc object&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
proc_object.<span style="color:#9900CC;">call</span></pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
I am a proc object</pre>
</li>
<li><strong>Using the proc method in the Kernel module</strong>. Being in the Kernel module, this method is globally available. It is basically equivalent to <em>Proc.new</em> in Ruby 1.9, but not in Ruby 1.8. In Ruby 1.8 the <em>proc</em> method is equivalent to <em>lambda</em>. As you may have guessed there is a difference between procs and lambdas (<em>see below</em>), so you need to be aware of whether you&#39;re using Ruby 1.8 or 1.9. If you&#39;re using 1.9 there is a way to find out if you&#39;re dealing with a proc or a lambda. The Proc object <em>lambda?</em> method:

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">proc_object = <span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Hello from inside the proc&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
proc_object.<span style="color:#9900CC;">call</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Is proc_object a lambda - #{proc_object.lambda?}&quot;</span></pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ rvm use 1.9.1
Using ruby 1.9.1 p378
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
Hello from inside the proc
Is proc_object a lambda - false</pre>
<p>If you&#39;re in 1.8 this method will not exist at all. By the way, did you notice how I had to switch Rubies and was able to do so painlessly through the awesome power of <a href="http://www.skorks.com/2010/01/using-multiple-rubies-seamlessly-on-the-one-machine-with-rvm/" target="_blank">RVM</a>, hope you&#39;re already using it. As far as I am concerned, if you need to switch between different versions of Ruby, don&#39;t bother with the <em>proc</em> method, just use <em>Proc.new</em> and you will never have an issue.</p>
</li>
<li><strong>Using the Kernel lambda method</strong>. Once again, this is globally available being in the Kernel module, using this method will create a Proc object, but it will be a lambda as opposed to a proc (<em>if you really need to know what the differences are at this point you can <a href="#difference">skip to the explanation</a> :)</em>).

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">proc_object = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Hello from inside the proc&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
proc_object.<span style="color:#9900CC;">call</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Is proc_object a lambda - #{proc_object.lambda?}&quot;</span></pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
Hello from inside the proc
Is proc_object a lambda - true</pre>
</li>
<li><strong>The implicit way</strong>. When you write a method which will accept a block, there are two ways you can go about it. The first is to simply use the <em>yield</em> keyword inside your method. If there is a <em>yield</em> in a method, but there is no block you will get an error.&nbsp;

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> my_method
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;hello method&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">yield</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
my_method <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;hello block&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
my_method</pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
hello method
hello block
hello method
a.rb:7:in `my_method&#39;: no block given (yield) (LocalJumpError)
    from a.rb:11:in `&lt;main&gt;&#39;</pre>
<p>The other way is to use a special parameter in your method parameter list. This special parameter can have the same name but you will need to precede it with an ampersand. The block you pass into this method will then be associated with this parameter, you will infact no longer have a block &#8211; it will be a Proc object. You will simply need to call the Proc <em>call</em> method to execute the code in the block you passed in. The difference is, you can now return your block (<em>or rather the Proc object</em>) as the return value of the method, thereby getting your hands on a Proc object without having to use any of the above three methods explicitly.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> my_method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>my_block<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;hello method&quot;</span>
  my_block.<span style="color:#9900CC;">call</span>
  my_block
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
block_var = my_method <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;hello block&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
block_var.<span style="color:#9900CC;">call</span></pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
hello method
hello block
hello block</pre>
</li>
</ol>
<p>Those are all the different ways to get a Proc object in Ruby (<em>either a proc or a lambda</em>).</p>
<blockquote>
<h3>Fun And Interesting Trivia About Procs And Lambdas</h3>
<p>If you have a method to which you pass a block, but instead of calling <em>yield</em>, you call <em>Proc.new</em> without giving it a block of its own, it will return a proc that represents the block you passed in to the method e.g.:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> some_method
  <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
my_proc = some_method<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;I am a happy proc&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
my_proc.<span style="color:#9900CC;">call</span></pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
I am a happy proc</pre>
<p><strong>In Ruby 1.9, there is actually another way to create lambdas</strong>. It isn&#39;t really a different way, it is simply some syntactic sugar, but it is crazy looking, so worth knowing :). The normal way to create a lambda is using the <em>lambda</em> keyword, and if you want your lambda to take parameters, you simply pass them in the normal block way e.g.:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">normal_lambda = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>param1, param2<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;param1: #{param1} and param2: #{param2}&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
normal_lambda.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">10</span>, <span style="color:#006666;">20</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
param1: 10 and param2: 20</pre>
<p>With the new syntax it is slightly different. We replace the lambda method with the <strong>-&gt;</strong> method (<em>as in minus followed by greater than sign</em>). The <a href="http://www.skorks.com/2009/08/method-arguments-in-ruby/" target="_blank">parameters</a> move out of the block, and move into some brackets right after the<strong> -&gt;</strong> method (<em>so they look like method parameters, but in reality are still block parameters</em>). Everything else is the same, so our previous example would look like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">normal_lambda = <span style="color:#006600; font-weight:bold;">-&gt;</span><span style="color:#006600; font-weight:bold;">&#40;</span>param1, param2<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;param1: #{param1} and param2: #{param2}&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
normal_lambda.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">10</span>, <span style="color:#006666;">20</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
param1: 10 and param2: 20</pre>
<p>Everything still works fine (<em>in Ruby 1.9, don&#39;t try this in 1.8</em>), but if you&#39;re using Netbeans, this syntax is way too crazy for it and it will barf all over itself. This syntax is an interesting curiosity, but <strong>I recommend sticking to plain old lambda for your own sanity</strong> (<em>especially if you&#39;re using Netbeans</em>) and that of everyone else who has to read your code later.</p>
</blockquote>
<h2>Procs Are First Class (Functions That Is)</h2>
<p style="text-align: center;"><img align="middle" alt="First class" class="aligncenter size-full wp-image-1832" src="http://www.skorks.com/wp-content/uploads/2010/05/first_class.jpg" style="width: 322px; height: 214px;" title="First class" /></p>
<p>Once we have created our first class function (<em>in Proc form</em>), there are a few things we may want to do with it, such as calling it or comparing it to another Proc. As usual, Ruby makes all of this interesting. Let&#39;s look at Proc equality first. You can compare procs or lambdas to each other, using the == method (<em>just like with any other object</em>). Unlike many other objects though, defining exactly the same proc twice will not make them equal. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">string1 = <span style="color:#996600;">&quot;blah&quot;</span>
string2 = <span style="color:#996600;">&quot;blah&quot;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{string1 == string2}&quot;</span>
&nbsp;
proc1 = <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;blah&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
proc2 = <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;blah&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{proc1 == proc2}&quot;</span></pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
String are equal? - true
Procs are equal? - false</pre>
<p>There are only two ways that two procs or lambdas will be equal to each other. One way is to define two really simple procs that have the same object in them e.g.:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">proc1 = <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#123;</span>string1<span style="color:#006600; font-weight:bold;">&#125;</span>
proc2 = <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#123;</span>string1<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Procs are equal? - #{proc1 == proc2}&quot;</span></pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
Procs are equal? - true</pre>
<p>This is not really very useful. The other way that two procs will ever be equal is if they are clones of each other (<em>one was created by using the clone or dup method on the other</em>).</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">proc1 = <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span><span style="color:#996600;">&quot;blah1&quot;</span><span style="color:#006600; font-weight:bold;">*</span>x<span style="color:#006600; font-weight:bold;">&#125;</span>
proc2 = proc1.<span style="color:#9900CC;">dup</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Procs are equal? - #{proc1 == proc2}&quot;</span></pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
Procs are equal? - true</pre>
<p>Let us now look at executing procs. We have already seen the call method; this is the most common way to execute a proc or lambda.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span><span style="color:#996600;">&quot;blah1&quot;</span><span style="color:#006600; font-weight:bold;">*</span>x<span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
blah1blah1</pre>
<p>But, there are actually two other ways. The first is to use the array syntax, this works in both Ruby 1.8 and 1.9:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span><span style="color:#996600;">&quot;blah1&quot;</span><span style="color:#006600; font-weight:bold;">*</span>x<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
blah1blah1</pre>
<p>The other is the dot syntax, which only works in 1.9.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span><span style="color:#996600;">&quot;blah1&quot;</span><span style="color:#006600; font-weight:bold;">*</span>x<span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
blah1blah1</pre>
<p>This syntax being 1.9 only will once again cause Netbeans to barf all over itself :). Both of those ways are nice and succinct, but <strong>I still recommend you use <em>call</em>; it is not that much longer and is much more readable, both for yourself and for others</strong>. The array syntax could be especially confusing, consider:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">some_var<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span>,<span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>That could refer to an array, a string, a proc &#8211; without context it&#39;s a bit too ambiguous for my tastes, stick to <em>call</em>.</p>
<blockquote>
<h3>Even More Trivia About Procs And Lambdas</h3>
<p>You may not know this, but procs and lambdas have an <em>arity</em> method which allows you to find out how many arguments a Proc objects expects to receive (<em><a href="http://ruby-doc.org/core/classes/Method.html" target="_blank">Method</a> objects also have this method</em>). For example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">my_proc = <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span><span style="color:#996600;">&quot;blah1&quot;</span><span style="color:#006600; font-weight:bold;">*</span>x<span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;I need #{my_proc.arity} arguments&quot;</span></pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
I need 1 arguments</pre>
<p>Pretty simple &#8211; the fun bit starts when the block you use to create the proc expects an arbitrary number of arguments (<em>or requires some, but sponges up the rest using a * prefixed final parameter</em>) e.g.:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">my_proc = <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x, <span style="color:#006600; font-weight:bold;">*</span>rest<span style="color:#006600; font-weight:bold;">|</span><span style="color:#996600;">&quot;blah1 #{x} - #{rest}&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;I need #{my_proc.arity} arguments&quot;</span></pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
I need -2 arguments</pre>
<p>What the hell? <strong>When you get a negative number for you arity, it means there is potentially an arbitrary number of arguments involved</strong>. In this case, all you can do is find out how many arguments are required (<em>the rest will be optional</em>). You do this by using the <strong>~</strong> operator on the return value of the arity method:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">my_proc = <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x, <span style="color:#006600; font-weight:bold;">*</span>rest<span style="color:#006600; font-weight:bold;">|</span><span style="color:#996600;">&quot;blah1 #{x} - #{rest}&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;I actually require #{~my_proc.arity} arguments, the rest are optional&quot;</span></pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
I actually require 1 arguments, the rest are optional</pre>
<p>I reckon that&#39;s interesting stuff to know, it won&#39;t save your life on a daily basis, but there will be that one time when knowing this will make you look like an absolute superstar :).</p>
</blockquote>
<p><a name="difference"></a></p>
<h2>So What IS The Difference Between Procs And Lambdas?</h2>
<p>It took a while, but we finally got here. Long story short,<strong> the real difference between procs and lambdas has everything to do with control flow keywords</strong>. I am talking about return, raise, break, redo, retry etc. &#8211; those control words. Let&#39;s say you have a return statement in a proc. When you call your proc, it will not only dump you out of it, but will also return from the enclosing method e.g.:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> my_method
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;before proc&quot;</span>
  my_proc = <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;inside proc&quot;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  my_proc.<span style="color:#9900CC;">call</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;after proc&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
my_method</pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
before proc
inside proc</pre>
<p>The final <em>puts</em> in the method, was never executed, since when we called our proc, the return within it dumped us out of the method. If, however, we convert our proc to a lambda, we get the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> my_method
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;before proc&quot;</span>
  my_proc = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;inside proc&quot;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  my_proc.<span style="color:#9900CC;">call</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;after proc&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
my_method</pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
before proc
inside proc
after proc</pre>
<p>The return within the lambda only dumps us out of the lambda itself and the enclosing method continues executing. The way control flow keywords are treated within procs and lambdas is the main difference between them. Or rather, the way that the <em>return</em> keyword and the <em>break</em> keyword are treated is the difference since most of the others are treated in exactly the same way in both procs and lambdas. Let&#39;s have a look at <em>break</em>. If you have a break keyword within a lambda it is treated just like a return:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> my_method
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;before proc&quot;</span>
  my_proc = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;inside proc&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">break</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  my_proc.<span style="color:#9900CC;">call</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;after proc&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
my_method</pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
before proc
inside proc
after proc</pre>
<p>However, if we change the lambda to a proc:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> my_method
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;before proc&quot;</span>
  my_proc = <span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;inside proc&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">break</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  my_proc.<span style="color:#9900CC;">call</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;after proc&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
my_method</pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
before proc
inside proc
a.rb:64:in `block in my_method&#39;: break from proc-closure (LocalJumpError)
    from a.rb:66:in `call&#39;
    from a.rb:66:in `my_method&#39;
    from a.rb:70:in `&lt;main&gt;&#39;</pre>
<p>We get a LocalJumpError, what happened? Well, the break keyword is used to break out of iteration, and we didn&#39;t have an iterator around our proc, so we couldn&#39;t break out of anything, so Ruby tries to punish us with an error. If we put an iterator around our proc, everything would be fine:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> my_method
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;before proc&quot;</span>
  my_proc = <span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;inside proc&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">break</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span>my_proc.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;after proc&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
my_method</pre></div></div>

<pre>alan@alan-ubuntu-vm:~/tmp$ ruby a.rb
before proc
inside proc
after proc</pre>
<p>So, how come the lambda didn&#39;t have the same error, we didn&#39;t have an iterator there either. Well, this is the other difference between procs and lambdas. Having a <em>break</em> keyword within a lambda will cause us to break out of the lambda itself, but within a proc we get an error if there is no iterator around the proc and we break out of the iterator if we do have one.</p>
<p>There you go, the difference between Ruby procs and lambdas. There are other control flow keywords (<em>next, redo, retry, raise</em>), but they behave the same in both procs and lambdas, that behaviour, however, is quite interesting, I urge you to have a play with it. More interesting Ruby fundamentals soon.</p>
<p><span style="font-size: 10px; font-family: trebuchet ms;">Images by <a href="http://www.flickr.com/photos/rofi/2097239111/" target="_blank">rofi</a> and <a href="http://www.flickr.com/photos/richardmoross/2211308689/" target="_blank">Richard Moross</a></span></p>


<p><strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2010/03/timing-ruby-code-it-is-easy-with-benchmark/' rel='bookmark' title='Permanent Link: Timing Ruby Code &#8211; It Is Easy With Benchmark'>Timing Ruby Code &#8211; It Is Easy With Benchmark</a></li>
<li><a href='http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/' rel='bookmark' title='Permanent Link: Ruby Access Control &#8211; Are Private And Protected Methods Only A Guideline?'>Ruby Access Control &#8211; Are Private And Protected Methods Only A Guideline?</a></li>
<li><a href='http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/' rel='bookmark' title='Permanent Link: Using Ruby Blocks And Rolling Your Own Iterators'>Using Ruby Blocks And Rolling Your Own Iterators</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=t2lSRs7dneY:DhbzzhhvtLk:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=t2lSRs7dneY:DhbzzhhvtLk:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=t2lSRs7dneY:DhbzzhhvtLk:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=t2lSRs7dneY:DhbzzhhvtLk:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=t2lSRs7dneY:DhbzzhhvtLk:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=t2lSRs7dneY:DhbzzhhvtLk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=t2lSRs7dneY:DhbzzhhvtLk:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=t2lSRs7dneY:DhbzzhhvtLk:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/softwaretechandmore/~4/t2lSRs7dneY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Why I Love Reading Other People’s Code And You Should Too</title>
		<link>http://www.skorks.com/2010/05/why-i-love-reading-other-peoples-code-and-you-should-too/</link>
		<comments>http://www.skorks.com/2010/05/why-i-love-reading-other-peoples-code-and-you-should-too/#comments</comments>
		<pubDate>Wed, 19 May 2010 13:42:25 +0000</pubDate>
		<dc:creator>Alan Skorkin</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[reading code]]></category>

		<guid isPermaLink="false">http://www.skorks.com/?p=1815</guid>
		<description><![CDATA[It occurs to me, that many programmers hate reading code &#8211; c&#39;mon admit it. Just about everyone loves writing code &#8211; writing code is fun. Reading code, on the other hand, is hard work. Not only is it hard work, it is boring, cause let&#39;s face it, any code not written by you just sucks [...]


<strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2008/09/the-real-measure-of-code-quality/' rel='bookmark' title='Permanent Link: The Real Measure Of Code Quality'>The Real Measure Of Code Quality</a></li>
<li><a href='http://www.skorks.com/2009/08/is-your-code-wthy-of-being-improvedor/' rel='bookmark' title='Permanent Link: Is Your Code Worthy Of Being Improved'>Is Your Code Worthy Of Being Improved</a></li>
<li><a href='http://www.skorks.com/2009/09/its-not-about-the-duct-tape-its-about-the-people-and-more/' rel='bookmark' title='Permanent Link: It&#8217;s Not About The Duct Tape It&#8217;s About The People (And More)'>It&#8217;s Not About The Duct Tape It&#8217;s About The People (And More)</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p><img align="left" alt="Hate" class="alignleft size-full wp-image-1819" hspace="20" src="http://www.skorks.com/wp-content/uploads/2010/05/hate.jpg" style="width: 385px; height: 255px;" title="Hate" vspace="5" />It occurs to me, that many programmers hate reading code &#8211; c&#39;mon admit it. Just about everyone loves writing code &#8211; writing code is fun. Reading code, on the other hand, <a href="http://blogs.msdn.com/ericlippert/archive/2004/06/14/reading-code-is-hard.aspx" target="_blank">is hard work</a>. Not only is it hard work, it is boring, cause let&#39;s face it, any code not written by you just sucks (<em>oh we don&#39;t say it, but we&#39;re all thinking it</em>). <strong>Even your own code begins to <a href="http://www.codinghorror.com/blog/2009/07/nobody-hates-software-more-than-software-developers.html" target="_blank">look increasingly sucky</a> mere hours after you&#39;ve finished writing it</strong> and the longer you leave it the suckier it seems. So, why should you waste hours looking at other people&#39;s crappy code when you can be spending this time writing awesome code of your own? Weren&#39;t we just over this; give it a couple of hours and then come back and see if your code still looks awesome. You&#39;re never going to become a <a href="http://www.skorks.com/2009/07/in-defense-of-the-software-craftsmanship-concept/" target="_blank">master of your craft</a> if you don&#39;t absorb the knowledge of the masters that came before you. One way to do that is to find a master in person and get them to teach you everything they know. Is this possible &#8211; certainly, is it probable &#8211; not so much, you&#39;d have to be extremely lucky. You don&#39;t really need luck though, <strong>we&#39;re fortunate to be in a profession where the knowledge and skill of all the masters is right there for us to absorb, embedded in the code they have written</strong>. All you have to do is read it, sure it might take you a bit longer without someone sitting there explaining it to you, but it&#39;s eminently possible. To put it in perspective, try becoming a great carpenter just by looking at a bunch of well constructed furniture.</p>
<p>I love reading code, I have always intuitively felt that you get a lot from it, yes it can be annoying and boring, but the payoff is well worth the effort. Consider this, if you wanted to become a great writer, would you focus exclusively on writing? You could try it, but you&#39;re not going to get far. It is an accepted fact that most great writers are also voracious readers. Before you can hope to write anything decent you need to read other great writers, absorb different styles, see what others have tried before you and feed your creative self. Your knowledge will slowly grow and eventually, your own writing will begin to exhibit some maturity, you will develop a &#39;<em>feel</em>&#39; for it. Coding is no different, <strong>why would you expect to <a href="http://c2.com/cgi/wiki?ReadGreatPrograms" target="_blank">write anything decent</a> if you&#39;ve never read any great code</strong>? The answer is you shouldn&#39;t. Reading great code is just as important for a programmer as reading great books is for a writer (<em>I can&#39;t take credit for this thought, it belongs to Peter Norvig and he is awesome, so take heed</em>).</p>
<p>Even if all of this is unconvincing, there is one fact which is undeniable. Being good at reading code is important to your survival as a professional developer. Any non-trivial project these days will be a team effort and so there will always be large chunks of code you had no hand in which you have to work with, modify and extend. And so, code reading will likely be the most used and most useful skill you can have; better bite the bullet and get good at it &#8211; fast.</p>
<h2>How To Read Code Like &#8230; Some Kind Of Code-Reading Guy</h2>
<p>I can&#39;t tell you how many times I&#39;ve seen programmers scroll up and down through an unfamiliar piece of code, for minutes on end, with a sour expression on their face. They would later declare the code to be unreadable crap and why waste the time anyway; we can just work around the problem somehow. I am not sure what the expectation was here, absorb the meaning of the code by osmosis, or maybe intensely stare your way to enlightenment? <strong>You don&#39;t read code by just looking at it for ages, you want to understand it and make it your own</strong>. Here are some <a href="http://c2.com/cgi/wiki?TipsForReadingCode" target="_blank">techniques</a> I like to use, it is not an exhaustive list, but I have found these to be particularly helpful.</p>
<p style="text-align: center;"><img align="middle" alt="Stare" class="aligncenter size-full wp-image-1821" src="http://www.skorks.com/wp-content/uploads/2010/05/stare.jpg" style="width: 269px; height: 296px;" title="Stare" vspace="3" /></p>
<ol>
<li><strong>Try to build and run it</strong>. Often this is a simple one step process, like when you&#39;re looking at work code (<em>as opposed to random code</em>). However this is not always the case and you can learn a lot about the high level structure of the code from getting it to build and execute. And, on the subject of work code, you are intimately familiar with how to build your current project are you not? Builds are often complex, but there is a lot of understanding to be gleaned from knowing how the build does its thing to produce the executable bits.</li>
<li><strong>Don&#39;t focus on the details just yet</strong>. The first thing you want to do is get a bit of a feel for the structure and style of the code you&#39;re reading. Start having a browse and try to figure out what the various bits of the code are trying to do. This will familiarise you with the high level structure of the whole codebase as well as give you some idea of the kind of code you&#39;re dealing with (<em>well factored, spaghetti etc.</em>). This is the time where you want to find the entry point (<em>whatever it happens to be, main function, servlet, controller etc.</em>) and see how the code branches out from there. Don&#39;t spend too long on this; it&#39;s a step you can come back to at any time as you gain more familiarity with the code.</li>
<li><strong>Make sure you understand all the constructs</strong>. Unless you happen to be the premier expert on your programming language there are probably some things you didn&#39;t know it could do. As you&#39;re having a high level fly-through of the code, note down any constructs you may be unfamiliar with. If there are a lot of these, your next step is obvious. You&#39;re not going to get very far if you have no idea what the code is doing syntactically. Even if there are only a few constructs you&#39;re unfamiliar with, it is probably a good idea to go look them up. You&#39;re now discovering things about your language you didn&#39;t know before, I am happy to trade a few hours of code reading just for that.</li>
<li><strong>Now that you&#39;ve got a good idea about most of the constructs, it is time to do a couple of random deep-dives</strong>. Just like in step 2, start flying though the code, but this time, pick some random functions or classes and start looking through them line by line. This is where the hard work really begins, but also where you will start getting the major pay-offs. The idea here is to really get into the mindset (<em>the groove</em>) of the codebase you&#39;re looking at. Once again don&#39;t spend too much time on this, but do try and deeply absorb a few meaty chunks before moving on. This is another step to which you can come back again and again with a bit more context and get more out of it every time.</li>
<li><strong>There were undoubtedly things in the previous step you were confused about, so this is the perfect time to go and read some tests</strong>. You will potentially have a lot less trouble following these and gain an understating of the code under test at the same time. I am constantly surprised when developers ignore a well-written and thorough test suite while trying to read and understand some code. Of course, sometimes there are no tests.</li>
<li><strong>No tests you say, sounds like the perfect time to write some</strong>. There are many benefits here, you&#39;re aiding your own understanding, you&#39;re improving the codebase, you&#39;re writing code while reading it, which is the best of both worlds and gives you something to do with your hands. Even if there are tests already, you can always write some more for your own benefit. Testing code often requires <a href="http://www.skorks.com/2010/01/thoughts-on-tdd-a-case-study-with-ruby-and-rspec/" target="_blank">thinking about it a little differently</a> and concepts that were eluding you before can become clear.</li>
<li><strong>Extract curious bits of code into standalone programs</strong>. I find this to be a fun exercise when reading code, even if just for a change of pace. Even if you don&#39;t understand the low level details of the code, you may have some idea of what the code is trying to do at a high level. Why not extract that particular bit of functionality into a separate program. It will make it easier to debug when you can execute a small chunk by itself, which &#8211; in turn &#8211; may allow you to take that extra step towards the understanding you&#39;ve been looking for.</li>
<li><strong>The code is dirty and smelly? Why not refactor it</strong>. I am not suggesting you rewrite the whole codebase, but refactoring even small portions of the code can really take your understanding to the next level. Start pulling out the functionality you do understand into self-contained functions. Before you know it, the original monster function is looking manageable and you can fit it in your head. Refactoring allows you to make the code your own without having to completely rewrite it. It helps to have good tests for this, but even if you don&#39;t have that, just test as you go and only pull out functionality you&#39;re sure of. Even if the tests seem totally inadequate &#8211; learn to trust your own skill as a developer, sometimes you just need to go for it (<em>you can always revert if you have to</em>).</li>
<li><strong>If nothing seems to help, <a href="http://www.joelonsoftware.com/articles/fog0000000053.html" target="_blank">get yourself a code reading buddy</a></strong>. You&rsquo;re probably not the only person out there who can benefit from reading this code, so go grab someone else and try reading it together. Don&#39;t get an expert though, they&#39;ll just explain it all to you at a high level and you will miss all the nuances that you can pick up by going though the code yourself. However, if nothing works, and you just don&#39;t get it, sometimes the best thing you can do is ask. Ask your co-workers or if you&#39;re reading open source code, try and find someone on the interwebs. But remember, this is the last step, not the first one.</li>
</ol>
<p>If I was pressed for time and needed to understand some code reasonably quickly and could only pick one of the above steps, I would pick refactoring (<em>step 8</em>). You will not be able to get across quite as much, but the stuff you do get across you will know solid. Either way, the thing you need to keep in mind is this. <strong>If you&#39;re new to a significant codebase, you will never get across it instantly, or even quickly</strong>. It will take days, weeks and months of patient effort &#8211; just accept it. Even having an expert right there with you doesn&#39;t significantly cut the time down (<em>this is what the <a href="http://feeds.feedburner.com/softwaretechandmore">last post in my teaching and learning series</a> will be about</em>). If however, you&#39;re patient and methodical about your code reading (<em>and writing</em>) you can eventually become intimately familiar with all aspects of the project and become the go-to man when it comes to the codebase. Alternatively you can avoid reading code and always be the guy looking for someone to explain something to you. I know which one I would rather be.</p>
<h2>Seek Out Code Reading Opportunities &#8211; Don&#39;t Avoid Them</h2>
<p style="text-align: center;"><img align="middle" alt="Code" class="aligncenter size-full wp-image-1820" src="http://www.skorks.com/wp-content/uploads/2010/05/code2.jpg" style="width: 356px; height: 267px;" title="Code" vspace="3" /></p>
<p>We love to write new code, it&#39;s seductive cause this time we&#39;re going to get it just right. Ok, maybe not this time, but next time for sure. The truth is, <strong>you&#39;re always evolving in your craft and you&#39;re never going to get it just right</strong>. There is always value in writing new code, you get to practice and hone your skills, but there is often just as much (<em>if not more</em>) value in reading and playing with code written by others. You not only get some valuable tech knowledge from it, but often domain knowledge as well (<em>after all, the code is the ultimate form of documentation</em>) which is often more valuable still.</p>
<p>Even code that is written in an esoteric fashion, without following any kind of convention, can be valuable. You know the code I am talking about, it almost looks obfuscated, but it wasn&#39;t intended to be (<em>for some reason it is often Perl code :)</em>). Whenever I see code like that, I think of it this way. Just imagine the kinds of stuff you will learn if you can only decipher this stuff. Yeah, it&#39;s a major pain, but admit it, there is that niggling secret desire to be able to write such naturally obfuscated code yourself (<em>no use denying it, you know it&#39;s true</em>). Well, if you invest some time into reading code like that, you&#39;re much more likely to eventually be able to write it &#8211; doesn&#39;t mean you will write it, but you want to be ABLE to. Lastly, attitude is always paramount. <strong>If you view code reading as a chore then it will be a chore</strong> and you will avoid it, but if you choose to see it as an opportunity &#8211; good things will happen.&nbsp;</p>
<p><span style="font-size: 10px; font-family: trebuchet ms;">Images by <a href="http://www.flickr.com/photos/mafleen/3295163029/" target="_blank">mafleen</a>, <a href="http://www.flickr.com/photos/ableman/3405701087/" target="_blank">Scott Ableman</a> and <a href="http://www.flickr.com/photos/lisathumann/3817685511/" target="_blank">LisaThumann</a></span></p>


<p><strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2008/09/the-real-measure-of-code-quality/' rel='bookmark' title='Permanent Link: The Real Measure Of Code Quality'>The Real Measure Of Code Quality</a></li>
<li><a href='http://www.skorks.com/2009/08/is-your-code-wthy-of-being-improvedor/' rel='bookmark' title='Permanent Link: Is Your Code Worthy Of Being Improved'>Is Your Code Worthy Of Being Improved</a></li>
<li><a href='http://www.skorks.com/2009/09/its-not-about-the-duct-tape-its-about-the-people-and-more/' rel='bookmark' title='Permanent Link: It&#8217;s Not About The Duct Tape It&#8217;s About The People (And More)'>It&#8217;s Not About The Duct Tape It&#8217;s About The People (And More)</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=GMpoHO0gqSw:5DfdNtnqDAY:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=GMpoHO0gqSw:5DfdNtnqDAY:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=GMpoHO0gqSw:5DfdNtnqDAY:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=GMpoHO0gqSw:5DfdNtnqDAY:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=GMpoHO0gqSw:5DfdNtnqDAY:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=GMpoHO0gqSw:5DfdNtnqDAY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=GMpoHO0gqSw:5DfdNtnqDAY:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=GMpoHO0gqSw:5DfdNtnqDAY:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/softwaretechandmore/~4/GMpoHO0gqSw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.skorks.com/2010/05/why-i-love-reading-other-peoples-code-and-you-should-too/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
		<item>
		<title>Closures – A Simple Explanation (Using Ruby)</title>
		<link>http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/</link>
		<comments>http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/#comments</comments>
		<pubDate>Mon, 17 May 2010 14:12:30 +0000</pubDate>
		<dc:creator>Alan Skorkin</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.skorks.com/?p=1806</guid>
		<description><![CDATA[There are a great many decent developers out there who don&#39;t know what a closure is. I don&#39;t really have any concrete stats on this matter, it is simply an intuitive assessment based on experience. But, you know what &#8211; that&#39;s fair enough, considering that the most popular languages that are in use right now [...]


<strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/' rel='bookmark' title='Permanent Link: Using Ruby Blocks And Rolling Your Own Iterators'>Using Ruby Blocks And Rolling Your Own Iterators</a></li>
<li><a href='http://www.skorks.com/2010/03/writing-a-more-ruby-ish-array-intersection-function-and-sorting-structs/' rel='bookmark' title='Permanent Link: Writing A More Ruby-ish Array Intersection Function And Sorting Structs'>Writing A More Ruby-ish Array Intersection Function And Sorting Structs</a></li>
<li><a href='http://www.skorks.com/2009/07/how-to-write-a-web-crawler-in-ruby/' rel='bookmark' title='Permanent Link: How To Write A Simple Web Crawler In Ruby'>How To Write A Simple Web Crawler In Ruby</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p><img align="left" alt="Closure" class="alignleft size-full wp-image-1809" hspace="20" src="http://www.skorks.com/wp-content/uploads/2010/05/closure1.jpg" style="width: 221px; height: 331px;" title="Closure" vspace="5" />There are a great many decent developers out there who don&#39;t know what a closure is. I don&#39;t really have any concrete stats on this matter, it is simply an intuitive assessment based on experience. But, you know what &#8211; that&#39;s fair enough, considering that the most popular languages that are in use right now <a href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B" target="_blank">don&#39;t support closures (Java, C++)</a>. <strong>When the language you use day in day out doesn&#39;t support a concept, that concept is not going to be high on your agenda</strong>; infact you may not even be aware of it. And yes, I agree, good developers will know several (<em>or perhaps many</em>) different languages and there are plenty out there that do support closures &#8211; you&#39;re bound to stumble across one at some point. Which just goes to show that when most developers learn new programming languages, they go about it completely the wrong way and so when you hear someone say they know a dozen languages, they are likely overstating the situation by quite a margin. But, I&#39;ll <a href="http://feeds.feedburner.com/softwaretechandmore">leave that discussion for another time</a> &#8211; today it is all about closures.</p>
<p>You probably came across closures when you were at uni, you just don&#39;t remember. Over the last 10 years or so, the curriculum of software related degrees has evolved to the point where concepts like closures are not emphasized, it is predominantly a functional concept and <a href="http://www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html" target="_blank">functional languages are out &#8211; Java is in</a>. <strong>They were trying to make the degrees more industry-relevant and as a result there is a generation of programmers who have been professionally crippled</strong> (<em>to some extent</em>) through no fault of their own. You trust the education system to do right by you and there isn&#39;t much you can do when this trust is misplaced. I need go no farther than myself for an example. I did Java in the first year of my degree, C in the second. The first time we were exposed to functional programming, was during an <em>AI</em> subject where we had to do some Lisp along with having to learn a whole slew of other concepts. Needless to say, functional programming wasn&#39;t the emphasis in that subject; many people struggled and struggled badly. We certainly had some exposure to closures during that subject, but who had time to think about that, it was all most people could do to try and wrap their heads around a new style of programming when they had just spent two years getting used to a completely different one. By the time I found myself in industry, closures &#8211; along with most other functional concepts &#8211; were long forgotten. <strong>It took me years of self-study before I rediscovered this stuff</strong> &#8211; stuff that should have been the foundation of my education as a software developer. I <a href="http://www.skorks.com/2008/08/3-things-they-should-have-taught-in-my-computer-science-degree/" target="_blank">once had a go at my CS degree</a> for not teaching some things that would have made it more relevant. Were I to write a similar post these days, I would be a lot harsher &#8211; I still might.</p>
<p>The bitter irony is that while Java is still well and truly in, <strong>the functional style is coming back with a vengeance</strong>. Fully functional languages (<em>Clojure</em>) and functional hybrids (<em>Scala</em>) are in vogue and so the concepts that underpin them are once again relevant. But, the damage has already been done and will continue to be done &#8211; academia does not adjust itself in a hurry. You can&#39;t afford to be ignorant of functional concepts any more, even JavaScript &#8211; that Web 2.0 language that <a href="http://www.skorks.com/2010/04/software-development-and-the-sunk-cost-fallacy/" target="_blank">we all love to hate</a> &#8211; <a href="http://www.b-list.org/weblog/2006/oct/11/functional-language-s-right-under-your-nose/" target="_blank">is a functional hybrid</a>. And so developers scramble to get their heads around these concepts and as they scramble they are presented with stuff like this:</p>
<blockquote>
<p>&quot;In computer science, a closure is a first-class function with free variables that are bound in the lexical environment.&quot;</p>
</blockquote>
<p>What the hell does that even mean! Wikipedia does go on to <a href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29" target="_blank">explain it a little bit better</a>, but without some functional context, it is still tough going. Here is another one you commonly hear:</p>
<blockquote>
<p>&quot;A closure is a function that is said to be &quot;closed over&quot; it&rsquo;s free variables&quot;</p>
</blockquote>
<p>Really! A &#39;<em>closure</em>&#39; closes over, why, that makes it all much clearer. Seriously, the next question is always bound to be a plea for clarification. <strong>An explanation like that is worse than no explanation at all</strong>, although it certainly does make someone sound smart. But, when you&#39;re trying to teach someone something<a href="http://www.skorks.com/2010/04/on-the-mastery-of-teaching/" target="_blank"> it is not about self-aggrandisement</a> &#8211; we can do better. Let&#39;s give it a go.</p>
<h2>A Concept Explained Simply</h2>
<p>A closure is basically a function/method that has the following two properties:</p>
<ul>
<li>You can pass it around like an object (<em>to be called later</em>)</li>
<li>It remembers the values of all the variables that were in scope when the function was created. It is then able to access those variables when it is called even though they may no longer be in scope.</li>
</ul>
<p>Let&#39;s fill in some more details. As you may have guessed, you don&#39;t get closures for free; they must be explicitly supported by the language. In order for the language to be able to support closures, it must support <a href="http://en.wikipedia.org/wiki/First-class_function" target="_blank">first-class functions</a>. A first class function is a function that can be treated like an object in that you can store it in collections and pass it as a parameter to other functions. As I said, the ability to be passed around is the first property of a closure.</p>
<p>A normal function is defined in a particular scope (<em>i.e. in a class</em>) and can only be called within that scope. This function has access to all the variables in the scope that it is defined, like the parameters that are passed into it as well as class variables. A closure on the other hand may be defined in one scope and be called in a completely different scope (<em>since we can pass it around before calling it</em>). Because of this, when a closure is created, it retains the values of all the variables that were in scope when the closure was defined.<strong> Even if the variables are generally no longer in scope when the closure is called, within the closure they still are</strong>. In other words, the closure retains knowledge of its lexical environment at the time it was defined.</p>
<p>Hopefully that &quot;<em>lexical environment</em>&quot; sentence above is starting to make a little bit more sense now, but I am sure it would make a lot more sense if we had an example.&nbsp;</p>
<h2>An Example In Ruby</h2>
<p>No explanation is complete without some examples, that is usually what it takes to make things start to fall into place. We will use Ruby since it supports closures and I like it.</p>
<p>In Ruby, closures are supported through <em>procs </em>and <em>lambdas</em>. These constructs are very similar, but there are some subtle differences (<em>I might do a post about that soon</em>). Let&#39;s create a closure and see how it fulfils the two properties described above.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> SomeClass
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>value1<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@value1</span> = value1
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> value_printer<span style="color:#006600; font-weight:bold;">&#40;</span>value2<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Value1: #{@value1}, Value2: #{value2}&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#CC0066; font-weight:bold;">caller</span><span style="color:#006600; font-weight:bold;">&#40;</span>some_closure<span style="color:#006600; font-weight:bold;">&#41;</span>
  some_closure.<span style="color:#9900CC;">call</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
some_class = SomeClass.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span>
printer = some_class.<span style="color:#9900CC;">value_printer</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;some value&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">caller</span><span style="color:#006600; font-weight:bold;">&#40;</span>printer<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>When we execute we get the following output:</p>
<pre>alan@alan-ubuntu-vm:~/tmp$ ruby closures.rb
Value1: 5, Value2: some value</pre>
<p>As you can see, the <em>value_printer</em> function creates a closure, using the <em>lambda </em>construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure &#8211; we can pass it around. Notice also that when we called our closure, we printed out &quot;<em>5</em>&quot; and &quot;<em>some value</em>&quot;. Even though both the <em>@value1</em> and <em>value2 </em>variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. And so, our <em>lambda </em>satisfies the second property also which makes it a closure. I hope the example has made things a bit clearer.</p>
<p>Of course, we can look into closures a little bit more deeply. For example, <strong>how do they retain the values of the variables that were in scope when the closure was defined</strong>? This must be supported by the language and there are two ways to do that.</p>
<ol>
<li>The closure will create a copy of all the variables that it needs when it is defined. The copies of the variables will therefore come along for the ride as the closure gets passed around.</li>
<li>The closure will actually extend the lifetime of all the variables that it needs. It will not copy them, but will retain a reference to them and the variables themselves will not be eligible for garbage collection (<em>if the language has garbage collection</em>) while the closure is around.</li>
</ol>
<p>If the language supports the first way, then if we create two or more closures which access the same variables, each closure will have its own distinct copy of those variables when it is called. <strong>If a language supports the second way, then all closures will reference the same variables, i.e. they will in effect be dealing with exactly the same variable. This is how Ruby does things</strong>. Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> SomeClass
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>value1<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@value1</span> = value1
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> value_incrementer
    <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span>@value1 <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> value_printer
    <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;value1: #{@value1}&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
some_class = SomeClass.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
incrementer = some_class.<span style="color:#9900CC;">value_incrementer</span>
printer = some_class.<span style="color:#9900CC;">value_printer</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&#40;</span>1..3<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  incrementer.<span style="color:#9900CC;">call</span>
  printer.<span style="color:#9900CC;">call</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This produces the following output:</p>
<pre>alan@alan-ubuntu-vm:~/tmp$ ruby closures.rb
value1: 3
value1: 4
value1: 5</pre>
<p>We create two closures this time, one to increment a value and the other to print it out. When we then call both of the closures three times, we can see that both are operating on the same variable as the value is incremented on every iteration. If Ruby handled retaining variables for closures by copying them, we would have had the original value of <em>2</em> printed out on every iteration since our <em>incrementer </em>and <em>printer </em>closures would each have had a distinct copy of the <em>@value1</em> variable.</p>
<h2>Why Are Closures Useful</h2>
<p style="text-align: center;"><img align="middle" alt="Closure" class="aligncenter size-full wp-image-1810" src="http://www.skorks.com/wp-content/uploads/2010/05/closure2.jpg" style="width: 248px; height: 319px;" title="Closure" vspace="3" /></p>
<p>Now that we understand closures a bit better, what&#39;s the big deal about them anyway? Well, it depends on what kind of language you&#39;re using. In a functional language they are a very big deal. <a href="http://en.wikipedia.org/wiki/Functional_programming" target="_blank">Functional languages</a> are inherently stateless, but we can use closures to essentially store some state which will persist as long as our closure lives on (<em>i.e. if the closure changes the value of a variable it will retain the new value the next time the closure is invoked</em>). I hope it is reasonably self-evident how this can be useful.<strong> The existence of constructs such as closures along with several others, allow functional languages to be very terse in expressing logic</strong> which means you can do more with less code.</p>
<p>With non-functional languages things are a little murkier. There are better ways to represent state when it comes to <a href="http://en.wikipedia.org/wiki/Imperative_programming" target="_blank">imperative languages</a>, so the only thing that makes closures compelling in that situation is the fact that you can use them to write terser code while still taking advantage of the imperative style. <strong>The existence of closures is partly the reason why people can often do more with less code in languages like Ruby</strong> (<em>which support closures</em>) than they can in languages like Java (<em>which do not</em>). If you have other things to add about the awesomeness of closures in any context, then do leave a comment, the more information we share the better off everyone will be.</p>
<p><span style="font-size: 10px; font-family: trebuchet ms;">Images by <a href="http://www.flickr.com/photos/tattoodjay/4118860732/" target="_blank">Tattooed JJ</a> and <a href="http://www.flickr.com/photos/danisarda/3057705570/" target="_blank">~Oryctes~ (Off)</a></span></p>


<p><strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/' rel='bookmark' title='Permanent Link: Using Ruby Blocks And Rolling Your Own Iterators'>Using Ruby Blocks And Rolling Your Own Iterators</a></li>
<li><a href='http://www.skorks.com/2010/03/writing-a-more-ruby-ish-array-intersection-function-and-sorting-structs/' rel='bookmark' title='Permanent Link: Writing A More Ruby-ish Array Intersection Function And Sorting Structs'>Writing A More Ruby-ish Array Intersection Function And Sorting Structs</a></li>
<li><a href='http://www.skorks.com/2009/07/how-to-write-a-web-crawler-in-ruby/' rel='bookmark' title='Permanent Link: How To Write A Simple Web Crawler In Ruby'>How To Write A Simple Web Crawler In Ruby</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=w_cS6RWvt68:fINcLWth7-o:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=w_cS6RWvt68:fINcLWth7-o:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=w_cS6RWvt68:fINcLWth7-o:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=w_cS6RWvt68:fINcLWth7-o:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=w_cS6RWvt68:fINcLWth7-o:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=w_cS6RWvt68:fINcLWth7-o:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=w_cS6RWvt68:fINcLWth7-o:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=w_cS6RWvt68:fINcLWth7-o:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/softwaretechandmore/~4/w_cS6RWvt68" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
		<item>
		<title>Who Deserves The Credit For Software Craftsmanship and Great Design?</title>
		<link>http://www.skorks.com/2010/05/who-deserves-the-credit-for-software-craftsmanship-and-great-design/</link>
		<comments>http://www.skorks.com/2010/05/who-deserves-the-credit-for-software-craftsmanship-and-great-design/#comments</comments>
		<pubDate>Wed, 12 May 2010 14:27:13 +0000</pubDate>
		<dc:creator>Alan Skorkin</dc:creator>
				<category><![CDATA[software development]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[craftsmanship]]></category>
		<category><![CDATA[history]]></category>

		<guid isPermaLink="false">http://www.skorks.com/?p=1795</guid>
		<description><![CDATA[How did people design great software before OO and how did we ever manage to run a successful project before Agile came along? Those are the questions young programmers undoubtedly ask themselves at some point in their career (I know I did, early on). You hear about and read horror story after horror story from [...]


<strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2009/07/in-defense-of-the-software-craftsmanship-concept/' rel='bookmark' title='Permanent Link: In Defense Of The Software Craftsmanship Concept'>In Defense Of The Software Craftsmanship Concept</a></li>
<li><a href='http://www.skorks.com/2009/07/how-to-get-the-most-out-of-your-design-sessions/' rel='bookmark' title='Permanent Link: How To Get The Most Out Of Your Design Sessions'>How To Get The Most Out Of Your Design Sessions</a></li>
<li><a href='http://www.skorks.com/2010/05/software-as-a-destination-vs-software-as-a-journey/' rel='bookmark' title='Permanent Link: Software As A Destination vs Software As A Journey'>Software As A Destination vs Software As A Journey</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p><img align="left" alt="Common Sense" class="alignleft size-full wp-image-1801" hspace="20" src="http://www.skorks.com/wp-content/uploads/2010/05/commonsense.jpg" style="width: 288px; height: 365px;" title="Common Sense" vspace="5" />How did people design great software before <a href="http://en.wikipedia.org/wiki/Object-oriented_programming" target="_blank">OO</a> and how did we ever manage to run a successful project before Agile came along? Those are the questions young programmers undoubtedly ask themselves at some point in their career (<em>I know I did, early on</em>). You hear about and read <a href="http://www.cs.tau.ac.il/~nachumd/horror.html" target="_blank">horror story</a> after <a href="http://spectrum.ieee.org/computing/software/why-software-fails" target="_blank">horror story</a> from the dark ages of software development (<em>sometimes the dark ages are years ago, other times they include the project before this one</em>) and then you hear about how things like OO or Agile are transforming (<em>or have transformed</em>) the industry. The truth is, projects didn&#39;t fail due to lack of Agile or OO (<em>or any other &#39;revolutionary&#39; practice</em>) and there were plenty of successful projects even in the bad old days (<a href="http://www.skorks.com/2009/09/does-software-development-have-a-culture-of-rewarding-failure/" target="_blank"><em>it&#39;s just that you don&#39;t tend to hear about the good ones</em></a>). Don&#39;t get me wrong, I believe in what Agile stands for and I haven&#39;t even seen non-OO software in my career :), but <strong>I don&#39;t subscribe to the buzzwords, I subscribe to what these concepts stand for</strong>, the core ideals if you like. Sometimes, you need to take a step back and consider that while the moniker may be shiny and new, the concepts it tries to package are often time honoured and <a href="http://www.skorks.com/2010/04/on-the-value-of-fundamentals-in-software-development/" target="_blank">fundamental</a> (<em>or as time honoured and fundamental as you can get considering the relative youth of our industry</em>).</p>
<p>I was thinking about the <a href="http://en.wikipedia.org/wiki/Unix_philosophy" target="_blank">Unix Philosophy</a> the other day and it struck me that pretty much all of the ideas seem really familiar. So, I went and cracked open a copy of &quot;<a href="http://www.faqs.org/docs/artu/" target="_blank">The Art Of Unix Programming</a>&quot;, just to make sure I wasn&#39;t getting myself mixed up. And what do you know, it was just as I remembered, but this time, I had a bit more experience to put it all in perspective for me. Here are a couple of quotes by <a href="http://en.wikipedia.org/wiki/Douglas_McIlroy" target="_blank">Doug McIlroy</a>:</p>
<blockquote>
<p>&quot;Make each program do one thing well. To do a new job, build afresh rather than complicate old programs by adding new features.&quot;</p>
</blockquote>
<p>Why, he is simply espousing <strong>making your software modular as well as giving a nod to separation of concerns</strong>. I dunno about you, but I keep something similar in mind when designing my classes, small, self-contained, I thought it was all OO principles :).</p>
<blockquote>
<p>&quot;Design and build software, even operating systems, to be tried early, ideally within weeks. Don&#39;t hesitate to throw away the clumsy parts and rebuild them&quot;</p>
</blockquote>
<p><strong>Release early, release often</strong>. Refactor and leave the code better than you found it.</p>
<blockquote>
<p>&quot;Use tools in preference to unskilled help to lighten a programming task, even if you have to detour to build the tools and expect to throw some of them out after you&#39;ve finished using them&quot;</p>
</blockquote>
<p><strong>Automate everything</strong>. Does it smell like agile in here or is it just me.</p>
<p>Let&#39;s keep going we don&#39;t even need to turn the page. Here is what <a href="http://en.wikipedia.org/wiki/Rob_Pike" target="_blank">Rob Pike</a> had to say</p>
<blockquote>
<p>&quot;Rule 1. You can&#39;t tell where a program is going to spend its time. Bottlenecks occur in surprising places, so don&#39;t try to second guess and put in a speed hack until you&#39;ve proven that&#39;s where the bottleneck is.<br />
		&nbsp;Rule 2. Measure. Don&#39;t tune for speed until you&#39;ve measured, and even then don&#39;t unless one part of the code overwhelms the rest.&quot;</p>
</blockquote>
<p>Hmmm, <strong>avoid premature optimisation</strong>.</p>
<blockquote>
<p>&quot;Rule 4. Fancy algorithms are buggier than simple ones, and they&#39;re much harder to implement. Use simple algorithms as well as simple data structures.&quot;</p>
</blockquote>
<p><a href="http://www.skorks.com/2009/08/does-yagni-mean-you-ignore-the-obvious/" target="_blank">YAGNI</a>. Even more agile wisdom. You have to remember that this stuff is from the 70s, years before OO got any traction and decades before the agile movement was even on the horizon. And these quotes are a distillation of thought, after these people had time to mull it over; they were actually doing this stuff years before.</p>
<p>Eric S. Raymond, the author of &quot;The Art Of Unix Programming&quot;, summarised the Unix philosophy in the following 17 tenets.</p>
<blockquote>
<p>1. Rule of Modularity: Write simple parts connected by clean interfaces.</p>
<p>		2. Rule of Clarity: Clarity is better than cleverness.</p>
<p>		3. Rule of Composition: Design programs to be connected to other programs.</p>
<p>		4. Rule of Separation: Separate policy from mechanism; separate interfaces from engines.</p>
<p>		5. Rule of Simplicity: Design for simplicity; add complexity only where you must.</p>
<p>		6. Rule of Parsimony: Write a big program only when it is clear by demonstration that nothing else will do.</p>
<p>		7. Rule of Transparency: Design for visibility to make inspection and debugging easier.</p>
<p>		8. Rule of Robustness: Robustness is the child of transparency and simplicity.</p>
<p>		9. Rule of Representation: Fold knowledge into data so program logic can be stupid and robust.</p>
<p>		10. Rule of Least Surprise: In interface design, always do the least surprising thing.</p>
<p>		11. Rule of Silence: When a program has nothing surprising to say, it should say nothing.</p>
<p>		12. Rule of Repair: When you must fail, fail noisily and as soon as possible.</p>
<p>		13. Rule of Economy: Programmer time is expensive; conserve it in preference to machine time.</p>
<p>		14. Rule of Generation: Avoid hand-hacking; write programs to write programs when you can.</p>
<p>		15. Rule of Optimization: Prototype before polishing. Get it working before you optimize it.</p>
<p>		16. Rule of Diversity: Distrust all claims for &quot;one true way&quot;.</p>
<p>		17. Rule of Extensibility: Design for the future, because it will be here sooner than you think.</p>
</blockquote>
<p>Let&rsquo;s pick out some choice tidbits. How about number 4, separate interface from implementation, I think we&#39;re all well drilled in that one these days :). I am really fond of number 8, this is the stuff that most agile developers live by, robust, simple and clear code (<em>you can take robust to mean well tested if that is your preference</em>). Number 15 is a good one, anyone do spikes when they code &#8211; I know I do? Number 16 is my favourite, I interpret it as &quot;<strong><em>if something doesn&#39;t work, stop doing it and do something else</em></strong>&quot;, reminds me of Scrum (<em>or as Conan the barbarian would say it &quot;SCrom&quot; :)</em>). Much of it is about keeping it simple, i.e. the <a href="http://en.wikipedia.org/wiki/KISS_principle" target="_blank">KISS</a> principle, just like many of the concepts behind agile. But of course, just because we&#39;re keeping it simple, doesn&#39;t mean we are foolish about it (<em>it&#39;s keep it simple stupid not keep it stupid stupid</em>), which is where number 17 comes in.</p>
<p>So where am I going with all of this, did the dudes who wrote Unix invent agile (<em>or at least the underlying ideals</em>), or software craftsmanship, or great software design? It&#39;s unlikely, after all <a href="http://en.wikipedia.org/wiki/Donald_Knuth" target="_blank">Don Knuth</a> said:</p>
<blockquote>
<p>&quot;Premature optimization is the root of all evil.&quot;</p>
</blockquote>
<p><a href="http://pplab.snu.ac.kr/courses/adv_pl05/papers/p261-knuth.pdf" target="_blank">He said this in 1974</a> (<em>the link is pdf</em>) &#8211; but was likely thinking it for years and there were probably people before him as well. It all had to start somewhere, of course, as I said; software is a relatively young industry. But, it doesn&#39;t really matter who the first person was that came up with these ideas, the fact that the ideas continue to figure prominently in newer concepts tells me that they are just basic common sense. When programming was born as a career, <strong>smart people came along, did the first project or two and quickly twigged on to what seemed to make sense and what didn&#39;t</strong>. All the nonsense, confusion and failure was mostly from a bunch of other people trying to fit software development into moulds it wasn&#39;t suited for &#8211; refusing to realise that this was an industry fundamentally unlike any that has come before and must have its own &quot;<em>rules</em>&quot; (<em>rather than trying to inherit some from manufacturing or random other industries that &quot;looked&quot; similar</em>). The saddest thing was that these people had power and could impose their will and make their ideas stick, which is why the whole industry went to hell for quite a while (<em>even to this day</em>), with signs of recovery only now beginning to emerge.</p>
<h2>So Does Agile Get Credit For Anything?</h2>
<p>Christianity and Islam are some of the most popular religions in the world. This should surprise you, since they are much younger than many other religions (<a href="http://en.wikipedia.org/wiki/Islam#History" target="_blank">especially Islam</a>). Do you know why these religions are so popular? Because they were the first missionary religions, the first to actively seek out new converts for their faith. Most of the religions that came before, either did not actively welcome outsiders or were actively hostile to them. These religions are a testament to the fact that if you actively try to seek out converts and don&#39;t give up, even due to major setbacks, you will slowly gain more and more followers and will eventually spread your message far and wide.</p>
<p>The agile movement was the beginning of the &quot;<em>missionary work</em>&quot; for these common sense software development concepts that we have now all come to know and love. Up until then, there was no focus, no organisation, and even when there was organisation, there was no attempt to seek out converts (<em>other developers</em>) and actively spread the message about how software should be built. The agile movement changed all that, which just goes to show that developers are just people and are willing to buy into something that seems sensible and hits close to home. But, the message itself was never revolutionary, just like the religious messages were never revolutionary. <strong>It was simply a bunch of common sense concepts that many good developers have followed for years</strong>.</p>
<p>What I am trying to say (<em>besides some curious factoids and a history lesson :)</em>), is this. Don&#39;t faddishly follow &quot;<em>best practices</em>&quot;, remember rule 16 &#8211; there is no &quot;<em>one true way</em>&quot;. It doesn&#39;t matter what tech you&#39;re using, it doesn&#39;t matter what the configuration of your task board is. The key is to surround yourself with good people (<em>this really is key and <a href="http://feeds.feedburner.com/softwaretechandmore">deserves a separate post</a></em>) and to always remain <a href="http://www.skorks.com/2010/05/software-as-a-destination-vs-software-as-a-journey/" target="_blank">aware of the situation</a> &#8211; then figure out what makes sense and just do that (<em>even if what makes sense is to follow best practice or to do the exact opposite :)</em>). Good programmers have always known the score.</p>
<p><span style="font-size: 10px; font-family: trebuchet ms;">Image by <a href="http://www.flickr.com/photos/fallenheroescar/4119500331/" target="_blank">RobertBasil</a></span></p>


<p><strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2009/07/in-defense-of-the-software-craftsmanship-concept/' rel='bookmark' title='Permanent Link: In Defense Of The Software Craftsmanship Concept'>In Defense Of The Software Craftsmanship Concept</a></li>
<li><a href='http://www.skorks.com/2009/07/how-to-get-the-most-out-of-your-design-sessions/' rel='bookmark' title='Permanent Link: How To Get The Most Out Of Your Design Sessions'>How To Get The Most Out Of Your Design Sessions</a></li>
<li><a href='http://www.skorks.com/2010/05/software-as-a-destination-vs-software-as-a-journey/' rel='bookmark' title='Permanent Link: Software As A Destination vs Software As A Journey'>Software As A Destination vs Software As A Journey</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=XhGsY8im5tE:uL4Z96g4yq8:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=XhGsY8im5tE:uL4Z96g4yq8:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=XhGsY8im5tE:uL4Z96g4yq8:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=XhGsY8im5tE:uL4Z96g4yq8:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=XhGsY8im5tE:uL4Z96g4yq8:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=XhGsY8im5tE:uL4Z96g4yq8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=XhGsY8im5tE:uL4Z96g4yq8:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=XhGsY8im5tE:uL4Z96g4yq8:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/softwaretechandmore/~4/XhGsY8im5tE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.skorks.com/2010/05/who-deserves-the-credit-for-software-craftsmanship-and-great-design/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Software As A Destination vs Software As A Journey</title>
		<link>http://www.skorks.com/2010/05/software-as-a-destination-vs-software-as-a-journey/</link>
		<comments>http://www.skorks.com/2010/05/software-as-a-destination-vs-software-as-a-journey/#comments</comments>
		<pubDate>Mon, 10 May 2010 13:54:34 +0000</pubDate>
		<dc:creator>Alan Skorkin</dc:creator>
				<category><![CDATA[software development]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[thoughts]]></category>

		<guid isPermaLink="false">http://www.skorks.com/?p=1788</guid>
		<description><![CDATA[I am feeling a little existential and metaphorical today, so forgive me if this post has more than just a slight tinge of existential metaphor about it. I do hope it still makes some sense.&#160;

There are two fundamental ways of looking at software development. One is all about the final product and the direct benefits [...]


<strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2009/08/building-software-development-expertise-using-the-dreyfus-model/' rel='bookmark' title='Permanent Link: Building Software Development Expertise &#8211; Using The Dreyfus Model'>Building Software Development Expertise &#8211; Using The Dreyfus Model</a></li>
<li><a href='http://www.skorks.com/2010/04/software-development-and-the-sunk-cost-fallacy/' rel='bookmark' title='Permanent Link: Software Development And The Sunk Cost Fallacy'>Software Development And The Sunk Cost Fallacy</a></li>
<li><a href='http://www.skorks.com/2010/05/8-types-of-software-consulting-firms-which-one-do-you-work-for/' rel='bookmark' title='Permanent Link: 8 Types Of Software Consulting Firms &#8211; Which One Do You Work For?'>8 Types Of Software Consulting Firms &#8211; Which One Do You Work For?</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p>I am feeling a little <a href="http://en.wikipedia.org/wiki/Existentialism" target="_blank">existential</a> and metaphorical today, so forgive me if this post has more than just a slight tinge of existential metaphor about it. I do hope it still makes some sense.&nbsp;</p>
<p style="text-align: center;"><img alt="Journey" class="aligncenter size-full wp-image-1790" src="http://www.skorks.com/wp-content/uploads/2010/05/journey.jpg" style="width: 233px; height: 233px;" title="Journey" vspace="3" /></p>
<p>There are two fundamental ways of looking at software development. One is all about the final product and the direct benefits you can get from it. The other is all about the lessons you learn from the process of building the software. <strong>I call them software as a destination and software as a journey</strong>. Historically speaking and even into the present day, the majority of companies that build any kind of software are &#39;<em>software as a destination</em>&#39; companies. They want to extract some direct financial benefit from the software they build (<em>which is fair enough</em>), until this benefit is realised, the process of building the software is just a resource (<em>time, money</em>) drain. On the other hand, most individuals who build software for those companies would instinctively like to be (<em>and in many cases ARE</em>) &#39;<em>software as a journey</em>&#39; people. We want to enjoy our work, we want to learn as much as we can, we want to interact with those around us, we want to build code that we can be proud of and we want to get paid. All of this will happen throughout the project rather than just at the end, and so it is all about the journey, the final success of the product is almost icing on the cake. It seems that <strong>there is a fundamental disconnect between what a company wants to get out of building software and what the developers will try to extract from the experience</strong>.</p>
<p>This is why the software development industry is in a sort-of screwed-up state, or at least it has been for many years (<em>things are getting better</em>). Companies see the destination and want to arrive there as quickly as they can, the sooner they get there the sooner they see the benefits from all that money they spend and the sooner they can start getting their money back, either directly or indirectly. Anything that stands in the way of this goal must be swept aside and this includes the developers who want to enjoy the journey and &#39;smell the roses&#39; on their way to the final product. Of course, the companies hold all the cards (<em>they employ you, pay you &#8211; which indirectly, feeds and clothes&#39; your children</em>) and so can usually get their way if they really want to. Developers can try to make noise about the value of paying attention to the journey, but if the company is dead set on doing things their way, that&#39;s the way things will be done, developers can either fall into line or there will be other developers to replace them. However, <strong>just like a real, physical journey, if you have your eyes set firmly on the destination and are not looking where you&#39;re putting your feet, there is a good chance you will trip over some roots, or a snake will bite you, or you won&#39;t notice that cliff which is right between you and where you&#39;re going</strong>. This is how we get failed projects.</p>
<p>Once the project and the company are at the bottom of the cliff, you can kinda tell that everyone is in trouble. But it is difficult to figure out exactly how you ended up where you are, you had your eyes firmly on the destination and forgot to pay attention to what was happening around you, so it is hard to pinpoint the mistakes that landed you at the bottom of a cliff. Companies will do a post-mortem, come up with some vague reasons for why things went to hell, everyone feels better that they have an explanation (<em>leaving everyone none the wiser in reality</em>). And so next time they go out and do the same thing all over again. All the while, the answer is staring everyone right in the face.<strong> If only they had listened to the developers who told them to pay attention to the journey and not worry so much about the destination things would have been different</strong>. Even novice programmers soon learn, from personal experience, that when we slow down and pay attention to what we are doing right now, we get a lot more out of it, we learn more and the final product is improved as a result. There are only benefits, no downsides, alright, you lose some time in the short term, but gain it all back with interest in the long term. But, at the macro level where committees make decisions, this same lesson takes years and decades to permeate the hive-mind. Not to mention the fact that seeing only the destination is much easier than constantly paying attention, and so even the occasional developer gets seduced by the &#39;<em>software as a destination</em>&#39; mentality (<em>it&#39;s really more than just the occasional</em>), which sets everyone back by introducing conflicting opinion and muddying the waters unnecessarily.</p>
<p>There was a bunch of people who were all &#39;<em>software as a journey</em>&#39; people who basically got sick of all the back and forth. They recognised the fact that the journey is what really matters not just at the personal level, where each individual developer is concerned, but at the team, project and company level as well. They decided to give a united voice to the message that as long as you take care and extract the maximum benefit along the way, the destination will take care of itself &#8211; you will arrive somewhere you want to be. <strong>It may not be exactly the same place where you thought you would end up, but it will be a nice place nevertheless and best of all, you will have a very good idea of precisely how you got where you are, despite not having a narrow focus on the final objective from the start</strong>. More like-minded people joined the initial bunch and the movement got more organised. Some of these people started companies of their own; others brought their message to larger organisation. And now that the &#39;<em>software as a journey</em>&#39; message had a united voice (<em>albeit still a comparatively small one</em>), the success of looking at software development this way could be demonstrated, but more importantly, disseminated to the industry at large.</p>
<p>It is a major undertaking for a company to develop any significant piece of software. Not just from a financial perspective, but as far as airing out the dirty baggage and discovering the process inefficiencies that every significant company will have. After all, in order to put something logically into code you need to figure out exactly what that something is and how it works. <strong>This is a golden opportunity for the company to streamline and improve how their business works, figure out if some processes are unnecessary</strong>. You&#39;re building software, but you&#39;re paying attention to what you&#39;re doing not just at the technical level but at the organisational level as well. As a result, not only do you get more robust software that better fits your needs, but you also improve the day to day operations of the company; all because you were paying attention to the journey rather than focusing on the destination.</p>
<p style="text-align: center;"><img alt="Destination" class="aligncenter size-full wp-image-1791" src="http://www.skorks.com/wp-content/uploads/2010/05/destination.jpg" style="width: 285px; height: 213px;" title="Destination" vspace="3" /></p>
<p>But, it is so much easier to simply look towards the goals and on paper it <strong>seems </strong>like it SHOULD be more efficient. If you&#39;re in management you want to seem more efficient and focused rather than being wishy-washy and touchy-feely, preaching self-awareness and a less-rapid pace just isn&#39;t strategic enough. And so companies create artificial deadline pressures (<em>like end of financial cycle of some sort</em>), to make everything neater and make the people in charge look good and it feels like for every step the agile movement takes towards running software projects better, some company somewhere, takes two steps back.</p>
<p><a href="http://en.wikipedia.org/wiki/Robert_A._Heinlein" target="_blank">Robert A Heinlein</a> once said:</p>
<blockquote>
<p><em>&quot;The supreme irony of life is that hardly anyone gets out of it alive.&quot;</em></p>
</blockquote>
<p>Infact, he had many other quotes which I think are quite relevant to the software profession, maybe I&#39;ll <a href="http://feeds.feedburner.com/softwaretechandmore">do a post about that at some point</a>. To me this quote says that it&#39;s the moment that really matters, enjoy the life you have now rather than constantly looking forward to something better. I am not sure if there is any irony to be found in software, <strong>but if you direct all your focus towards your goal without paying due attention to the nitty gritty of what you&#39;re doing every day, you&#39;re likely to not get any useable software out of it</strong>. As long as you have a reasonable idea of where you want to end up, you just need to get the details right and the bigger picture will tend to sort itself out. On the other hand, you can have the clearest possible goal in mind, but if you let the details slide, bad things will almost certainly happen.</p>
<p><span style="font-size: 10px; font-family: trebuchet ms;">Images by <a href="http://www.flickr.com/photos/dramaqueennorma/535278231/" target="_blank">Norma Desmond</a> and <a href="http://www.flickr.com/photos/alicepopkorn/3377110664/" target="_blank">alicepopkorn</a></span></p>


<p><strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2009/08/building-software-development-expertise-using-the-dreyfus-model/' rel='bookmark' title='Permanent Link: Building Software Development Expertise &#8211; Using The Dreyfus Model'>Building Software Development Expertise &#8211; Using The Dreyfus Model</a></li>
<li><a href='http://www.skorks.com/2010/04/software-development-and-the-sunk-cost-fallacy/' rel='bookmark' title='Permanent Link: Software Development And The Sunk Cost Fallacy'>Software Development And The Sunk Cost Fallacy</a></li>
<li><a href='http://www.skorks.com/2010/05/8-types-of-software-consulting-firms-which-one-do-you-work-for/' rel='bookmark' title='Permanent Link: 8 Types Of Software Consulting Firms &#8211; Which One Do You Work For?'>8 Types Of Software Consulting Firms &#8211; Which One Do You Work For?</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=Dj5QDT2pPmQ:1vtgM4HCKYA:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=Dj5QDT2pPmQ:1vtgM4HCKYA:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=Dj5QDT2pPmQ:1vtgM4HCKYA:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=Dj5QDT2pPmQ:1vtgM4HCKYA:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=Dj5QDT2pPmQ:1vtgM4HCKYA:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=Dj5QDT2pPmQ:1vtgM4HCKYA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=Dj5QDT2pPmQ:1vtgM4HCKYA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=Dj5QDT2pPmQ:1vtgM4HCKYA:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/softwaretechandmore/~4/Dj5QDT2pPmQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.skorks.com/2010/05/software-as-a-destination-vs-software-as-a-journey/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Sort Files Like A Master With The Linux Sort Command (Bash)</title>
		<link>http://www.skorks.com/2010/05/sort-files-like-a-master-with-the-linux-sort-command-bash/</link>
		<comments>http://www.skorks.com/2010/05/sort-files-like-a-master-with-the-linux-sort-command-bash/#comments</comments>
		<pubDate>Sat, 08 May 2010 16:23:51 +0000</pubDate>
		<dc:creator>Alan Skorkin</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://www.skorks.com/?p=1781</guid>
		<description><![CDATA[If you do your development work in Linux, there are certain commands that you owe it to yourself to master fully. There are a number of these with the main ones being grep, find and sort. Just about everyone has at least a passing familiarity with these commands, but with most people the knowledge is [...]


<strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2010/03/how-to-quickly-generate-a-large-file-on-the-command-line-with-linux/' rel='bookmark' title='Permanent Link: How To Quickly Generate A Large File On The Command Line (With Linux)'>How To Quickly Generate A Large File On The Command Line (With Linux)</a></li>
<li><a href='http://www.skorks.com/2009/09/using-bash-to-output-to-screen-and-file-at-the-same-time/' rel='bookmark' title='Permanent Link: Using Bash To Output To Screen And File At The Same Time'>Using Bash To Output To Screen And File At The Same Time</a></li>
<li><a href='http://www.skorks.com/2010/05/executing-multiple-commands-a-bash-productivity-tip/' rel='bookmark' title='Permanent Link: Executing Multiple Commands &#8211; A Bash Productivity Tip'>Executing Multiple Commands &#8211; A Bash Productivity Tip</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p><img align="left" alt="Sort" class="alignleft size-full wp-image-1784" hspace="20" src="http://www.skorks.com/wp-content/uploads/2010/05/sort.jpg" style="width: 312px; height: 234px;" title="Sort" vspace="5" />If you do your development work in Linux, there are certain commands that you owe it to yourself to master fully. There are a number of these with the main ones being <a href="http://linux.die.net/man/1/grep" target="_blank">grep</a>, <a href="http://linux.die.net/man/1/find" target="_blank">find</a> and <a href="http://linux.die.net/man/1/sort" target="_blank">sort</a>. Just about everyone has at least a passing familiarity with these commands, but with most people the knowledge is superficial, they don&#39;t even realise how powerful those commands can be. So, if you really put in the effort to master them, not only will you make your own life much easier, but you will also be able to impress all you friends with your elite Linux skills when you <a href="http://www.skorks.com/2009/07/effective-vs-ineffective-pair-programming/" target="_blank">pair</a> with them :). I will cover grep and find (<em>as well as other valuable commands</em>) in <a href="http://feeds.feedburner.com/softwaretechandmore">subsequent posts</a> &#8211; here we will concentrate on <em>sort</em>.&nbsp;</p>
<blockquote>
<p><em><strong>Note</strong>: I am using bash, so your mileage might vary if you&#39;re using a different shell.</em></p>
</blockquote>
<p>Sorting is a <a href="http://www.skorks.com/2010/04/on-the-value-of-fundamentals-in-software-development/" target="_blank">fundamental</a> task when it comes to programming, <strong>if you have a decent knowledge of various sorting algorithms, their advantages and disadvantages, you will be a better software developer for it</strong>. However, often enough you just don&#39;t need to draw on this deeper knowledge. Whether you&#39;re answering an <a href="http://www.skorks.com/2010/03/how-to-answer-a-programming-interview-question-and-look-good-doing-it/" target="_blank">interview question</a> about sorting or simply need to quickly sort some data in you day to day work &#8211; the Linux <em>sort</em> command is your friend.</p>
<p>The extent of most people&#39;s knowledge ends with:</p>
<pre>sort some_file.txt</pre>
<p>Which is fair enough, you rarely need to dig deeper, the default behaviour will usually do what you need and when it doesn&#39;t &#8211; we have Ruby or Perl, we can hack something together. Well I hope that we&#39;re somewhat more curious than your average developer :). As much is we like hacking things together, if a tool can already do all the work for us, we want to know about it. We can look at the man page for <em>sort</em> and discover all sorts of interesting bits, but even the man page is not really clear on the more advanced aspects of sort usage. It helps to have examples to truly grok the kinds of stuff you can do with <em>sort</em>, so let&#39;s have a look.</p>
<h2>Sorting Basics</h2>
<p>To start with we have the following file:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ cat letters.txt
b
D
c
A
C
B
d
a</pre>
<p>We&#39;ll do the most basic sort first:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ sort letters.txt
a
A
b
B
c
C
d
D</pre>
<p>Looks good, how about doing it in reverse:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ sort -r letters.txt
D
d
C
c
B
b
A
a</pre>
<p>Also easy, but what if we want to be case insensitive? Hang on a sec, according to the output it&#39;s already case insensitive. But the man page has an option for this:</p>
<pre>-f, --ignore-case
              fold lower case to upper case characters</pre>
<p>If sort is case insensitive by default, what is this option for. We&#39;ll that one is a bit of a gotcha, <a href="http://www.linuxforums.org/forum/misc/159163-solved-how-make-sort-case-sensitive.html" target="_blank"><strong>it looks like GNU sort is case insensitive by default</strong></a>, but the man page also contains the following:</p>
<pre>*** WARNING *** The locale specified by the  environment  affects  sort
       order.  Set LC_ALL=C to get the traditional sort order that uses native
       byte values.</pre>
<p>What this means that we need to set the LC_ALL environment variable to get the behaviour that we would expect from sort (<em>i.e. capital letters before non-capitals</em>). Let&#39;s try that:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ export LC_ALL=C
alan@alan-ubuntu-vm:~/tmp/sort$ sort letters.txt
A
B
C
D
a
b
c
d</pre>
<p>That&#39;s better, and now our <strong><em>-f</em></strong> option is actually useful:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ sort -f letters.txt
A
a
B
b
C
c
D
d</pre>
<p>That looks ok, but something still seems a little funny, all the capitals appear before all the non-capitals every time. That&#39;s because the sort is not <a href="http://en.wikipedia.org/wiki/Sorting_algorithm#Stability" target="_blank">stable</a>, but we can make it stable:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ sort -f -s letters.txt
A
a
b
B
c
C
D
d</pre>
<p>Now that&#39;s exactly what we wanted, it&#39;s case insensitive and stable, i.e. <strong>if the small letter appeared before the capital when unsorted (<em>and the letters are the same</em>), this order will be the same in the sorted list</strong>.</p>
<p>Ok, but what if we have numbers:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ cat numbers.txt
5
4
12
1
3
56</pre>
<p>A normal sort is not what we want:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ sort numbers.txt
1
12
3
4
5
56</pre>
<p>But we can fix that:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ sort -n numbers.txt
1
3
4
5
12
56</pre>
<p>And, if our lines happen to have some leading blanks:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ cat blank_letters.txt
b
D
   c
A
C
    B
d
a</pre>
<p>We can easily ignore those and still sort correctly (<em>using the <strong>-b</strong> flag</em>):</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ sort -f -s -b blank_letters.txt
A
a
b
    B
   c
C
D
d</pre>
<p>Of course none of this actually writes the sorted output back to the file, we only get it on standard output. If we want to write it back to the file, we have to <a href="http://www.skorks.com/2009/09/output-redirection-with-bash/" target="_blank">redirect</a> the output to a new file and then replace the old file with the new file:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ sort -f -s -b blank_letters2.txt &gt; blank_letters2.sorted
alan@alan-ubuntu-vm:~/tmp/sort$ cat blank_letters2.sorted
A
a
b
    B
   c
C
D
d
alan@alan-ubuntu-vm:~/tmp/sort$ mv blank_letters2.sorted blank_letters2.txt
alan@alan-ubuntu-vm:~/tmp/sort$ cat blank_letters2.txt
A
a
b
    B
   c
C
D
d</pre>
<p>As an alternative to redirection, sort also has the <strong><em>-o</em></strong> option:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ sort -f -s -b blank_letters2.txt -o blank_letters2.sorted
alan@alan-ubuntu-vm:~/tmp/sort$ cat blank_letters2.sorted
A
a
b
    B
   c
C
D
d</pre>
<p>Alright, this is all pretty standard stuff, let&#39;s see how we can do something a little bit more fancy.</p>
<h2>Advanced Sort Usage</h2>
<p><strong>One of the most common use cases when it comes to sort is to pipe its output to <em>uniq</em></strong>, in order to remove any duplicate lines, but this is often not necessary as sort has a uniq-type option built right in (<strong><em>-u</em></strong>):</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ cat blank_letters.txt
b
D
c
A
C
B
d
a
alan@alan-ubuntu-vm:~/tmp/sort$ sort -f -s -u blank_letters.txt
A
b
c
D</pre>
<p>It even took into account the fact that we wanted to be case insensitive. Compare that to a case-sensitive sort with a <strong><em>uniq </em></strong>option set:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ sort -s -u blank_letters.txt
A
B
C
D
a
b
c
d</pre>
<p>Nothing is removed as there are no duplicate lines.</p>
<p>But what if my lines are a little bit more complicated than just a single letter, what if there are multiple columns and I want to sort by one of those columns (<em>not the first one</em>). This is also possible:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ ls -al | sort -k5
total 36
-rw-r--r-- 1 alan alan   14 May  9 00:12 numbers.txt
-rw-r--r-- 1 alan alan   16 May  9 00:00 letters.txt
-rw-r--r-- 1 alan alan   16 May  9 00:40 blank_letters.txt
-rw-r--r-- 1 alan alan   20 May  9 00:33 blank_letters2.sorted
-rw-r--r-- 1 alan alan   20 May  9 00:33 blank_letters2.txt
-rw-r--r-- 1 alan alan   20 May  9 00:37 blank_letters3.txt
-rw-r--r-- 1 alan alan   84 May  8 23:15 file1.txt
drwxr-xr-x 3 alan alan 4096 May  8 23:13 ..
drwxr-xr-x 2 alan alan 4096 May  9 00:40 .</pre>
<p>As you can see we sorted the output of <em><strong>ls</strong></em> by the size (<em>the 5th column</em>). This is what the <strong><em>-k</em></strong> option is for. Basically <strong><em>-k</em></strong> tells sort to start sorting at a particular column given a particular column separator. <strong>The column separator is, by default, any blank character</strong>. So in the above example, we told sort to sort by the 5th column given the fact that the column separator is blanks.</p>
<p>But we don&#39;t have to be restricted by the default separator, we can specify our own using the <strong><em>-t</em></strong> option. Let&#39;s sort the first 10 lines of my <strong>/etc/passwd</strong> file (<em>promise you won&#39;t hack my machine since I am giving it away like that :)</em>) by the 4th column &#8211; the group id. As you know the <strong>/etc/passwd</strong> file uses the<strong> :</strong> (<em>colon</em>) character as the separator (<em>for more info on the format see <a href="http://www.cyberciti.biz/faq/understanding-etcpasswd-file-format/" target="_blank">here</a></em>). Here is the output unsorted:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ cat /etc/passwd | head
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh</pre>
<p>There is a group id of 65534 in there which should appear last. Let&#39;s sort it:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ cat /etc/passwd | head | sort -t: -k4 -n
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
games:x:5:60:games:/usr/games:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync</pre>
<p>We had to do a numeric sort since we&#39;re dealing with numbers, and we specified <strong>: </strong>(<em>colon</em>) as the column separator. The output is sorted correctly with 65534 being on the last line. Pretty cool! But the fun doesn&#39;t stop there, we can sort by multiple columns, one after the other. Consider this list of IP addresses:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ cat ips.txt
192.168.0.25
127.0.0.12
192.168.0.1
127.0.0.3
127.0.0.6
192.168.0.5</pre>
<p>Let&#39;s sort it by the first column, so that all the addresses starting with 127 go together, and then sort it by the 4th column, to make sure that the IPs are sorted by the last column within each range.</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ cat ips.txt | sort -t. -k 2,2n -k 4,4n
127.0.0.3
127.0.0.6
127.0.0.12
192.168.0.1
192.168.0.5
192.168.0.25</pre>
<p>We specified the dot as the separator. The <strong>-k 2,2n</strong> syntax has the following meaning. Do a sort by column (<strong>-k</strong>), start at the beginning of column 2 and go to the end of column 2 (<strong>2,2</strong>). The <strong>n</strong> on the end is to indicate that we want to do a numeric sort since we are dealing with numbers. That is some powerful stuff, wouldn&#39;t you agree?</p>
<h2>Cool/Useful Stuff</h2>
<p>There is still more we can do with the sort command. Have you ever wanted to randomize the lines in a file? It is not a common use case, but does come in handy once in a while (<em>if only for testing purposes, sometimes</em>). Well, the sort command has you covered here also with the <em><strong>-R</strong></em> option (<em>that&#39;s capital R</em>):</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ cat numbers.txt
5
4
12
1
3
56
alan@alan-ubuntu-vm:~/tmp/sort$ cat numbers.txt | sort -R
5
4
1
3
12
56
alan@alan-ubuntu-vm:~/tmp/sort$ cat numbers.txt | sort -R
3
4
1
56
5
12</pre>
<p>We get a different order every time, which is what we would expect from randomizing the lines.</p>
<p>If you give sort multiple files on the command line, it will combine the contents of all the files and sort it as a whole:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ sort -n numbers.txt numbers2.txt
1
1
3
4
4
5
7
8
10
12
22
23
26
56
56
68</pre>
<p>This is really handy, but sometimes, the files you have are already sorted, you just want to merge them. Sort provides the<strong><em> -m</em></strong> options just for this purpose. The output of using sort on two files will be exactly the same whether you use <strong><em>-m</em></strong> or not, but merging should be faster:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ sort -n -m numbers1.sorted numbers2.sorted
1
1
3
4
4
5
7
8
10
12
22
23
26
56
56
68
</pre>
<p>Lastly, if you just want to check if a file is sorted or not, without actually performing the sort, you have the <strong><em>-c</em></strong> option:</p>
<pre>alan@alan-ubuntu-vm:~/tmp/sort$ sort -n -c numbers.txt
sort: numbers.txt:2: disorder: 4
alan@alan-ubuntu-vm:~/tmp/sort$ sort -n -c numbers1.sorted
alan@alan-ubuntu-vm:~/tmp/sort$</pre>
<p>There you go, the total awesomeness of the <em>sort </em>command laid bare. If you know any other handy things you can do with sort, do leave a comment. And remember &#8211; only use your new-found sort powers for good instead of evil :).</p>
<p><span style="font-size: 10px; font-family: trebuchet ms;">Image by <a href="http://www.flickr.com/photos/sewpixie/2374778051/" target="_blank">SewPixie (so far behind!)</a></span></p>


<p><strong>Related posts:</strong><ol><li><a href='http://www.skorks.com/2010/03/how-to-quickly-generate-a-large-file-on-the-command-line-with-linux/' rel='bookmark' title='Permanent Link: How To Quickly Generate A Large File On The Command Line (With Linux)'>How To Quickly Generate A Large File On The Command Line (With Linux)</a></li>
<li><a href='http://www.skorks.com/2009/09/using-bash-to-output-to-screen-and-file-at-the-same-time/' rel='bookmark' title='Permanent Link: Using Bash To Output To Screen And File At The Same Time'>Using Bash To Output To Screen And File At The Same Time</a></li>
<li><a href='http://www.skorks.com/2010/05/executing-multiple-commands-a-bash-productivity-tip/' rel='bookmark' title='Permanent Link: Executing Multiple Commands &#8211; A Bash Productivity Tip'>Executing Multiple Commands &#8211; A Bash Productivity Tip</a></li>
</ol></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=RHvZfBRqsLY:O49YohZDPV8:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=RHvZfBRqsLY:O49YohZDPV8:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=RHvZfBRqsLY:O49YohZDPV8:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=RHvZfBRqsLY:O49YohZDPV8:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=RHvZfBRqsLY:O49YohZDPV8:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=RHvZfBRqsLY:O49YohZDPV8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/softwaretechandmore?a=RHvZfBRqsLY:O49YohZDPV8:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/softwaretechandmore?i=RHvZfBRqsLY:O49YohZDPV8:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/softwaretechandmore/~4/RHvZfBRqsLY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.skorks.com/2010/05/sort-files-like-a-master-with-the-linux-sort-command-bash/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss><!-- Dynamic page generated in 2.990 seconds. --><!-- Cached page generated by WP-Super-Cache on 2010-09-09 12:42:47 -->
