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

<channel>
	<title>Jorge Manrubia &#187; Projects</title>
	<atom:link href="http://jorgemanrubia.net/category/projects/feed/" rel="self" type="application/rss+xml" />
	<link>http://jorgemanrubia.net</link>
	<description>Personal Page</description>
	<lastBuildDate>Mon, 19 Sep 2011 23:39:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>solid_assert: A simple Ruby assertion utility</title>
		<link>http://jorgemanrubia.net/2011/09/19/solid_assert-a-simple-ruby-assertion-utility/</link>
		<comments>http://jorgemanrubia.net/2011/09/19/solid_assert-a-simple-ruby-assertion-utility/#comments</comments>
		<pubDate>Mon, 19 Sep 2011 20:32:08 +0000</pubDate>
		<dc:creator>Jorge Manrubia</dc:creator>
				<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://jorgemanrubia.net/?p=579</guid>
		<description><![CDATA[I have published a tiny Ruby gem implementing an assertion utility: solid_assert. When I started with Ruby I searched about an assert utility. My brother taught me about them a long time ago and I have used them since then. For some reason, assertions are not very used in Ruby. Most rubyists and most famous [...]]]></description>
			<content:encoded><![CDATA[<p>I have published a tiny Ruby gem implementing an assertion utility: <a href="https://github.com/jorgemanrubia/solid_assert">solid_assert</a>.</p>

<p>When I started with Ruby I searched about an <code>assert</code> utility. <a href="http://pablomanrubia.com/">My brother</a> taught me about them a long time ago and I have used them since then. For some reason, assertions are not very used in Ruby. Most rubyists and most famous Ruby libraries just don&#8217;t use them. I found the same thing with dependency injection frameworks. But, while after a time working with Ruby <a href="http://jorgemanrubia.net/2010/05/30/dependency-injection-and-other-java-necessary-evils/">I was convinced that I didn&#8217;t need a dependency injection framework</a>,  I still miss assertions when programming Ruby.</p>

<h2>Motivation</h2>

<p>The motivation of assertions are very well syntheitzed in the tip 33 of <a href="http://www.amazon.com/exec/obidos/ASIN/020161622X/ref=nosim/jorgmanrpersp-20">The Pragmatic Programmer</a> book:</p>

<blockquote>
  <p>If it can&#8217;t happen, use assertions to ensure that it won&#8217;t</p>
</blockquote>

<p>The premise of an assertion utility is very simple: being able to include tests for your assumptions inside your code. This way, it is the program itself who verifies its own integrity. In the same way it is a good practice to use properly named and composed methods so you don&#8217;t have to document with comments what some code is doing, it is a good practice to formally code the assumptions you make about your code, and have these assumptions tested automatically.</p>

<p>You may think that regular tests are already covering code integrity. To me, good test suites and assertions are totally complementary. I use tests but I still want my code to launch a nice <code>NoMethodError</code> for <code>NilClass</code> when I try to send a message to a nil reference. In the same way, you can write tests and also build-in integrity checking inside your code using assertions.</p>

<h2>An example</h2>

<p>Let me show you an example. Imagine you don&#8217;t know that Rails already includes an <a href="http://apidock.com/rails/ActiveSupport/OrderedHash">OrderedHash</a> class and you decide to implement your own one.</p>

<p>A simple approach (in fact, the one used by Rails&#8217; <code>OrderedHash</code>) would be extending the Ruby <code>Hash</code> class, using an array of keys for storing the keys in order and delegating hash-related behaviour to its parent class.</p>

<p>For example, we could implement the <code>[]=</code> method for setting values in the hash in the following way:</p>

<div class="codecolorer-container ruby blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> OrderedHash <span style="color:#006600; font-weight:bold;">&lt;</span> ::<span style="color:#CC00FF; font-weight:bold;">Hash</span><br />
&nbsp; &nbsp; ...<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>=<span style="color:#006600; font-weight:bold;">&#40;</span>key, value<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@keys</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> key <span style="color:#9966CC; font-weight:bold;">if</span> !has_key?<span style="color:#006600; font-weight:bold;">&#40;</span>key<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">super</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; ...<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>

<p>The method stores the key in the array and then invoke the parent&#8217;s behaviour. Under any circumstance, it should always be verified that the keys array and the hash entries have the same size. That is a class invariant. <code>assert</code> let you express that:</p>

<div class="codecolorer-container ruby blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>=<span style="color:#006600; font-weight:bold;">&#40;</span>key, value<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@keys</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> key <span style="color:#9966CC; font-weight:bold;">if</span> !has_key?<span style="color:#006600; font-weight:bold;">&#40;</span>key<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">super</span><br />
&nbsp; &nbsp; assert <span style="color:#0066ff; font-weight:bold;">@keys</span>.<span style="color:#9900CC;">size</span> == <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">size</span>, <span style="color:#996600;">&quot;#{@keys.size} elements in the list and #{self.size} entries in the hash?&quot;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>

<p><a href="http://jorgemanrubia.net/2011/09/19/solid_assert-a-simple-ruby-assertion-utility/">solid_assert</a> also includes an <code>invariant</code> method that let you make more complex calculations using a block.</p>

<div class="codecolorer-container ruby blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">invariant <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; one_variable = calculate_some_value<br />
&nbsp; &nbsp; other_variable = calculate_some_other_value<br />
&nbsp; &nbsp; one_variable <span style="color:#006600; font-weight:bold;">&gt;</span> other_variable<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>

<p>Assertions can be disabled. In fact, both <code>assert</code> and <code>invariant</code> are empty methods when you use the lib. You can enable them with <code>SolidAssert.enable_assertions</code>. This let you deactivate them in production, if you are concerned about their performance impact.</p>

<h2>References</h2>

<p>All the references to assertions you find in books refer directly or indirectly to Bertrand Meyer&#8217;s <a href="http://en.wikipedia.org/wiki/Design_by_contract">design by contract</a> proposal:</p>

<ul>
<li><p><a href="http://www.amazon.com/exec/obidos/ASIN/020161622X/ref=nosim/jorgmanrpersp-20">The Pragmatic Programmer From Journeyman to Master</a>. It contains a section dedicated to <em>Design by contract</em> and a short one dedicated to Assertive Programming. It says that you can use assertions to partially emulate <em>design by contract</em>. These sections are in the chapter <em>Pragmatic Paranoia</em>, when they defend that:</p>

<blockquote>
  <p>But Pragmatic Programmer take this a step further. They <em>don&#8217;t trust themselves</em>, either.</p>
</blockquote>

<p>By the way, they recommend you to leave assertions on in production, eloquently saying that turning them off is <em>&#8220;like crossing a high wire without a net because you once made it across in practice&#8221;</em>.</p></li>
<li><p><a href="http://www.amazon.com/exec/obidos/ASIN/0735619670/ref=nosim/jorgmanrpersp-20">Code Complete</a>. It dedicates a section in the chapter <em>Defensive programming</em> to assertions. It recommends you to use assertions to document and verify preconditions and postconditions in your software.</p></li>
<li><p><a href="http://www.amazon.com/exec/obidos/ASIN/1556155514/ref=nosim/jorgmanrpersp-20">Writing solid code</a>. This was my first reading about assertions and the one that pays more attention to them. In the chapter <em>Assert Yourself</em> it defines assertions, explains its motivation and shows when to use them using many C samples. A great book on good coding practices in general.</p></li>
<li><p><a href="http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html">Programming with assertions</a>. Although it was published for explaining the <code>assert</code> keyword introduced by Java 1.4, it explains very well the underlying concepts and shows many examples using them.</p></li>
</ul>

<h2>Conclusion</h2>

<p>In my experience assertions used reasonably are good. They make your code more solid. You don&#8217;t have to check all the invariants, or try to validate all the preconditions and postconditions of every method you write. Just use them when you find yourself saying <em>&#8220;at <strong>this</strong> point <strong>this</strong> should be verified&#8230;&#8221;</em> and you will enjoy its benefits in the form of more robust code and less surprises.</p>

<p>I also think that asserts are suitable for any programming language. Of course, with lower level languages like C their use is even more recommended, because errors are more obscure and their consequences are harder to debug. But nothing prevent higher level languages from benefitting from them.</p>]]></content:encoded>
			<wfw:commentRss>http://jorgemanrubia.net/2011/09/19/solid_assert-a-simple-ruby-assertion-utility/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Evernicious: A tool for importing del.icio.us bookmarks into Evernote</title>
		<link>http://jorgemanrubia.net/2011/01/02/evernicious-a-tool-for-importing-del-icio-us-bookmarks-into-evernote/</link>
		<comments>http://jorgemanrubia.net/2011/01/02/evernicious-a-tool-for-importing-del-icio-us-bookmarks-into-evernote/#comments</comments>
		<pubDate>Sun, 02 Jan 2011 23:34:44 +0000</pubDate>
		<dc:creator>Jorge Manrubia</dc:creator>
				<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://jorgemanrubia.net/2011/01/02/evernicious-a-tool-for-importing-del-icio-us-bookmarks-into-evernote/</guid>
		<description><![CDATA[Some months ago, following my brother&#8217;s advice I replaced del.icio.us with Evernote as my web bookmarking system. At the moment I had over 2000 del.icio.us bookmarks. Since Evernote didn&#8217;t offer any kind of facility for importing bookmarks from del.icio.us, and coding a converter seemed quite easy I started with it, but never took the time [...]]]></description>
			<content:encoded><![CDATA[<p>Some months ago, <a href="http://twitter.com/#!/jmanrubia/status/20292510908">following my brother&#8217;s advice</a> I replaced <a href="http://www.delicious.com/">del.icio.us</a> with <a href="http://www.evernote.com/">Evernote</a> as my web bookmarking system. At the moment I had <a href="http://www.delicious.com/jmanrubia">over 2000 del.icio.us bookmarks</a>. Since Evernote didn&#8217;t offer any kind of facility for importing bookmarks from del.icio.us, and coding a converter seemed quite easy I started with it, but never took the time to finish it.</p>

<p>It has been the recent <a href="http://blog.delicious.com/blog/2010/12/whats-next-for-delicious.html">rumors regarding to del.icio.us future</a> that I decided to finish it. The result is a tool amazingly named <em>Evernicious</em>. The source code, installation and usage instructions can be found at <a href="https://github.com/jorgemanrubia/evernicious/">the Evernicious github page</a>.</p>

<p>There are many other solutions already available for importing del.icio.us bookmarks into Evernote. Just check the comments at this <a href="http://blog.evernote.com/2010/12/16/making-the-transition-from-delicious-to-evernote/">Evernote post</a> (and I mean the comments, the proposed way in the post is simply wrong in my opinion).</p>

<p>In my current configuration, I have a <a href="http://www.evernote.com/pub/jmanrubia/bookmarks">public Evernote Notebook</a> with my bookmarks. I use the Chrome <a href="https://chrome.google.com/extensions/detail/pioclpoplcdbaefihamjohnefbikjilc">Clip to Evernote extension</a> for capturing web pages into this Notebook.</p>]]></content:encoded>
			<wfw:commentRss>http://jorgemanrubia.net/2011/01/02/evernicious-a-tool-for-importing-del-icio-us-bookmarks-into-evernote/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Skittlish theme for Typo</title>
		<link>http://jorgemanrubia.net/2008/05/23/skittlish-theme-for-typo/</link>
		<comments>http://jorgemanrubia.net/2008/05/23/skittlish-theme-for-typo/#comments</comments>
		<pubDate>Fri, 23 May 2008 11:32:55 +0000</pubDate>
		<dc:creator>Jorge Manrubia</dc:creator>
				<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://jorgemanrubia.net/blog/?p=11</guid>
		<description><![CDATA[I have adapted a theme I really love to use it with the blogging engine that runs this site: Typo. It&#8217;s the Skittlish theme, originally created by Damien Tanner and Cristi Balan for Mephisto&#8217;s. I still have to polish several details before submitting it to the Typo&#8217;s theme repository. For now, you can download the [...]]]></description>
			<content:encoded><![CDATA[<p>I have adapted a theme I really love to use it with the blogging engine that runs this site: <a href="http://www.typosphere.org/">Typo</a>. It&#8217;s the <a href="http://evil.che.lu/projects/skittlish">Skittlish theme</a>, originally created by <a href="http://iamrice.org/">Damien Tanner</a> and <a href="http://evil.che.lu/">Cristi Balan</a> for <a href="http://mephistoblog.com/">Mephisto&#8217;s</a>.</p>

<p>I still have to polish several details before <a href="http://www.typosphere.org/2007/08/27/typo-theming-guide">submitting</a> it to the <a href="http://www.dev411.com/typo/themes/">Typo&#8217;s theme repository</a>.</p>

<p>For now, you can <a href="http://jorgemanrubia.net/blog/wp-content/uploads/2009/02/skittlish-typo.zip">download the Skittlish theme for Typo directly from this site</a>. </p>]]></content:encoded>
			<wfw:commentRss>http://jorgemanrubia.net/2008/05/23/skittlish-theme-for-typo/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

