<?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</title>
	<atom:link href="http://jorgemanrubia.net/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>Using delayed_job with Cucumber</title>
		<link>http://jorgemanrubia.net/2010/09/01/using-delayed_job-with-cucumber/</link>
		<comments>http://jorgemanrubia.net/2010/09/01/using-delayed_job-with-cucumber/#comments</comments>
		<pubDate>Wed, 01 Sep 2010 23:42:22 +0000</pubDate>
		<dc:creator>Jorge Manrubia</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://jorgemanrubia.net/?p=523</guid>
		<description><![CDATA[Delayed_job is a great Ruby solution for executing jobs asynchronously. It is intended to be run in the background, dispatching jobs that are persisted in a table . If you are using Cucumber you have to consider how the dispatching process is launched when your features are executed. My first attempt after googling for this [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://github.com/collectiveidea/delayed_job">Delayed_job</a> is a great Ruby solution for executing jobs asynchronously. It is intended to be run in the background, dispatching jobs that are persisted in a table . If you are using <a href="http://cukes.info/">Cucumber</a> you have to consider how the dispatching process is launched when your features are executed.</p>

<p>My first attempt after <a href="http://groups.google.com/group/delayed_job/tree/browse_frm/month/2009-09?_done=/group/delayed_job/browse_frm/month/2009-09%3F&amp;">googling for this question</a> was to create a custom Cucumber step that launched the execution of the jobs.</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">Given <span style="color:#006600; font-weight:bold;">/</span>^Jobs are being dispatched$<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; <span style="color:#6666ff; font-weight:bold;">Delayed::Worker</span>.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">work_off</span> <br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>

<p>In this approach we have an specific Cucumber step for indicating when do we want to dispatch jobs. This step will be executed synchronously in the same Cucumber thread, so you have to invoke it after some step has introduced a new job in the queue and before the verification steps:</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;">When</span> I perform some action <span style="color:#006600; font-weight:bold;">&#40;</span>that makes the server to create a new job<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">And</span> Jobs are being dispatched<br />
<span style="color:#9966CC; font-weight:bold;">Then</span> I should see the expected results</div></div>

<p>I think this approach is not very convenient:</p>

<ul>
<li><p>Cucumber is intended to be used for writing integration tests. Tests that describe your application from the point of view of its users. Ideally, they should only manipulate the application inputs and verify its outputs through the UI. A user of your application will never need to know you are using a job dispatcher in your server.</p></li>
<li><p>While controlling the exact (and synchronous) execution of jobs makes writing tests easier, it doesn&#8217;t represent the temporal randomness which is in the very nature of an asynchronous job dispatcher. In my opinion, it is good that Cucumber features verify that this randomness is correctly handled (in some controlled limits).</p></li>
</ul>

<p>I think a better approach is launching the jobs in the background, simulating the normal execution environment of your application. The idea is very simple: the job worker is started before each Cucumber scenario and is stopped after it. Cucumber tags represent a good choice for implementing these hooks. In this way, you can easily activate delayed_job only for the scenarios that need it.</p>

<p>When implementing this approach, I found a lot of problems for providing a proper <code>RAILS_ENV=cucumber</code> to the <code>delayed_job</code> command. In fact, I wasn&#8217;t able to make it work using launching the command <code>script/delayed_job start</code> from a Cucumber step. <code>RAILS_ENV</code> was simply ignored. What I finally did was executing the <code>rake</code> task directly.</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">Before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'@background-jobs'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; <span style="color:#CC0066; font-weight:bold;">system</span> <span style="color:#996600;">&quot;/usr/bin/env RAILS_ENV=cucumber rake jobs:work &amp;&quot;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>

<p>For stopping the jobs I had the same <code>RAILS_ENV</code> issue using <code>script/delayed_job stop</code>. I ended up killing the job processes using a <a href="http://www.commandlinefu.com/commands/view/1138/ps-ef-grep-process-grep-v-grep-awk-print-2-xargs-kill-9">parametrized kill command</a>.</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">After<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'@background-jobs'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; <span style="color:#CC0066; font-weight:bold;">system</span> <span style="color:#996600;">&quot;ps -ef | grep 'rake jobs:work' | grep -v grep | awk '{print $2}' | xargs kill -9&quot;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>

<p>Using this approach you can get rid of specific steps for delayed_job. Instead, you just have to tag with <code>@background-jobs</code> the features/scenarios that needed it.</p>

<p>As a conclusion, I think that using background jobs in Cucumber is a better approach in general terms. I would only use the synchronous <code>work_off</code> approach for special cases.</p>]]></content:encoded>
			<wfw:commentRss>http://jorgemanrubia.net/2010/09/01/using-delayed_job-with-cucumber/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Dependency injection and other Java necessary evils</title>
		<link>http://jorgemanrubia.net/2010/05/30/dependency-injection-and-other-java-necessary-evils/</link>
		<comments>http://jorgemanrubia.net/2010/05/30/dependency-injection-and-other-java-necessary-evils/#comments</comments>
		<pubDate>Sun, 30 May 2010 22:39:14 +0000</pubDate>
		<dc:creator>Jorge Manrubia</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://jorgemanrubia.net/?p=470</guid>
		<description><![CDATA[Lately I often find myself thinking about how much I have changed my mind about Java. For a long time I have been interested in the Java Platform and I have tried to be educated on its good practices, patterns and trends. Today, I can say I am not interested in the Java platform anymore. [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I often find myself thinking about how much I have changed my mind about Java. For a long time I have been interested in the Java Platform and I have tried to be educated on its good practices, patterns and trends. Today, I can say I am not interested in the Java platform anymore. In the process, I have learned the wonders of Ruby, <a href="http://rubyonrails.org/">Rails</a>, true Behavioral-Driven Development and a community full of brilliant developers who consider that beautiful code is a primary goal and that software development has to be fun.</p>

<p>In this post I would like to talk about three recurrent Java features I have changed my mind about in recent times: from loving them I have finished considering them as necessary evils you have to live with.</p>

<ul>
<li>Dependency injection</li>
<li>Strong type system</li>
<li>Object disorientation</li>
</ul>

<h3>Dependency injection</h3>

<p>The <a href="http://martinfowler.com/articles/injection.html">dependency injection (<acronym title="Dependency Injection">DI</acronym>) pattern</a> is about separation of concerns. The concern of wiring object dependencies is extracted from objects and centralized in some kind of factory facility that, using some configuration information on how object are wired together, instantiates and configure these objects for you. This pattern is in the core of <a href="http://www.springsource.org/">Spring</a> and is implemented by <a href="http://code.google.com/p/google-guice/">Google Guice</a>.</p>

<p>So why are <acronym title="Dependency Injection">DI</acronym> solutions so important in Java? I think the main reason is because they are the only way to enable good unit testing. Sure there are other benefits, like minimizing coupling between objects or avoiding to write the same wiring boilerplate code again and again, but these are not as important as testing. While you will hardly find yourself in the need of changing a JDBC <acronym title="Data Access Object">DAO</acronym> for a <acronym title="Java Persistence API">JPA </acronym>one, you will always need to mock dependencies when writing unit tests. And for doing so, you need to isolate these dependencies first.</p>

<p>And the Java <code>new</code> operator makes this task very difficult. It is just so rigid. You don&#8217;t have any mechanism for faking it once it is coded within your objects. The alternative usually implies implementing a full ecosystem of interfaces, their implementations enabling the dependency injection (in the form of constructors or setter methods), and factories for instantiating the configured objects. And since the boilerplate code this approach implies is considerable, you would rather use an external <acronym title="Dependency Injection">DI</acronym> library that makes the work for you. So external <acronym title="Dependency Injection">DI</acronym> solutions are good but, from a testing perspective, they solve a problem created by the Java new operator.</p>

<p>I realized of this problem when I tried to build something serious with Rails. The first thing I did was to look which dependency-injection solutions were available for Rails. I you look for <acronym title="Dependency Injection">DI</acronym> in Rails there is a good chance you end up reading this <a href="http://weblog.jamisbuck.org/2008/11/9/legos-play-doh-and-programming">article by Jamis Buck</a>. That post summarized his clarifying experience with <acronym title="Dependency Injection">DI</acronym> libraries in Ruby:</p>

<ul>
<li>He first created <a href="http://copland.rubyforge.org/">Copland</a>: a port of the Java <a href="http://hivemind.apache.org/">Hivemind</a>.</li>
<li>He then created <a href="http://needle.rubyforge.org/">Needle</a> as a more Ruby-like approach to a <acronym title="Dependency Injection">DI</acronym> solution.</li>
<li>He finally concluded that <acronym title="Dependency Injection">DI</acronym> frameworks are unnecessary in Ruby (he talks about <acronym title="Dependency Injection">DI</acronym> tools, not about the pattern itself).</li>
</ul>

<p>In my opinion, Ruby examples used by Jamis are not the best. I think a key aspect in Ruby is that <code>new</code> is just a class method. This means that classes in Ruby are by definition factories, and you can fake them directly when testing. Let me show you an example with a <code>Person</code> class that depends on <code>Mouth</code>, since to say something, a person has to open his mouth.</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> Mouth<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#CC0066; font-weight:bold;">open</span><br />
&nbsp; &nbsp; <span style="color:#996600;">&quot;Very complex, slow and sophisticated processing here&quot;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> Person<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> say_hi<br />
&nbsp; &nbsp; Mouth.<span style="color:#9900CC;">new</span>.<span style="color:#CC0066; font-weight:bold;">open</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>

<p>If you want to test this behavior with RSpec, you would do something like:</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">describe <span style="color:#996600;">&quot;Person&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; it <span style="color:#996600;">&quot;should open the mouth to say hi&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@person</span> = Person.<span style="color:#9900CC;">new</span><br />
&nbsp; &nbsp; mouth = mock<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;mouth&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; mouth.<span style="color:#9900CC;">should_receive</span><span style="color:#006600; font-weight:bold;">&#40;</span>:<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; Mouth.<span style="color:#9900CC;">should_receive</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:new</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">and_return</span><span style="color:#006600; font-weight:bold;">&#40;</span>mouth<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@person</span>.<span style="color:#9900CC;">say_hi</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>

<p>Since the Ruby <code>new</code> method is just a class method, it can be stubbed the way you want to inject the dependencies. This is a key aspect when testing with Ruby. You get used to it quickly because it is a natural thing from the programmer point of view. What is difficult is to go back to Java after knowing it, because of the big mess you have to create to achieve the same thing.</p>

<p>Another nice feature of Java <acronym title="Dependency Injection">DI</acronym> solutions is that they manage the scope of objects for you. The usual default approach is to create an object each time is requested. But sometimes it is needed to have a singleton scope for some objects. Spring or Guice let you configure the scope in a declarative way and they manage the scope for you. The scope management is a production thing. For testing, you just create the objects you want to test directly.</p>

<p>If you hardcode a singleton object in Java you have to take care of cleaning its state between test cases. And this is just laborious and not elegant. <acronym title="Dependency Injection">DI</acronym> frameworks offers a very nice solution for this problem. In Ruby, you still can test singleton classes because you <a href="http://blog.ardes.com/2006/12/11/testing-singletons-with-ruby">can modify the Singleton module to expose a reset method that regenerates the single instance each time</a>. This solution is possible by dynamic capabilities of Ruby. Again, I think it is nice just to be able to use the singleton object directly.</p>

<p>Another approach for the singleton problem is to have a <a href="http://pivotallabs.com/users/chad/blog/articles/219-avoiding-constants-in-rails">global configuration object as a simple hash</a>. In testing time you can override the contents of the hash. Again, Ruby syntax and symbols make to look nice something that seems scary if you come from the java World.</p>

<p>Regarding the scope problem, the solution offered by <acronym title="Dependency Injection">DI</acronym> frameworks is probably more pure. They are also more complete. For example, Guice let you specify how the injected objects are created through the <a href="http://code.google.com/docreader/#p=google-guice&amp;s=google-guice&amp;t=ProviderBindings">concept of providers</a>. The thing is that with power comes complexity. Having many options is never for free. Even if you say: &#8220;I will only use a 10% of Guice&#8221; the rest of options are still there in the form of documentation and code. For example, they make you choose about what approach to take. And my point is that with a pure Ruby approach, without external tools, you can solve the most common cases in a much simpler way. I suppose it is a matter of taste, but I enjoy that approach much more.</p>

<h3>Strong types system</h3>

<p>I really don&#8217;t intend to talk about advantages and disadvantages of dynamic-typing and static-typing. But I would like to show an example of something that in my opinion is wrong. It is related to <a href="http://code.google.com/intl/en/webtoolkit/doc/latest/DevGuideCodingBasicsOverlay.html">GWT concept of overlay types</a>. It is a solution for managing JSON data as Java objects with GWT. This approach proposes to create a Java replication of the JSON data with getters/setters for the properties. The java code has to be mixed with the javascript code to be executed via <a href="http://googlewebtoolkit.blogspot.com/2008/07/getting-to-really-know-gwt-part-1-jsni.html">JSNI</a>. An example taken from the GWT page:</p>

<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #000000; font-weight: bold;">extends</span> JavaScriptObject <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// Overlay types always have protected, zero-arg constructors</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">protected</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// Typically, methods on overlay types are JSNI</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">native</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getFirstName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*-{ return this.FirstName; }-*/</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">native</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getLastName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> &nbsp;<span style="color: #666666; font-style: italic;">/*-{ return this.LastName; &nbsp;}-*/</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>

<p>To me, it is like an implementation of the RY (Repeat Yourself) principle. I mean, do we need strong types that much? If this is the best abstraction you can do with Java of JSON data (and probably it is), then there must be something wrong with the language, because JSON data is only about structures of key/value pairs that can be nested.</p>

<p>Parsing JSON data with Ruby or Javascript is trivial. I will use the former in the next example. It shows how one line of javascript code can convert a JSON string into a plain Javascript object (using the JSON parsing utility of JQuery).</p>

<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> personJSON <span style="color: #339933;">=</span> <span style="color: #3366CC;">'{&quot;name&quot;: &quot;Jorge&quot;}'</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> person <span style="color: #339933;">=</span> $.<span style="color: #660066;">parseJSON</span><span style="color: #009900;">&#40;</span>personJSON<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
person.<span style="color: #000066;">name</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Jorge</span></div></div>

<h3>Object disorientation</h3>

<p>The JSON example is a concrete example where dynamic typing just makes more sense. But to me, the essential problem of strong types is that they make more difficult to represent mental abstractions with code, because every single piece must fit in a very strict puzzle of types. And this means that you are forced to code a lot of things that have nothing to do with your domain, but with the type system of the language.</p>

<p>Take for example the next GWT design. In GWT, widgets that trigger click events implements an interface <a href="http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/event/dom/client/HasClickHandlers.html">HasClickHandler</a>. This interface defines the contract of registering <a href="http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/event/dom/client/ClickHandler.html">ClickHandler</a> objects, which are responsible of processing click events. This is a materialization of the <a href="http://martinfowler.com/eaaDev/PassiveScreen.html">Passive View pattern</a>: a view without any state with controllers performing all the work. The idea is to have a very thin view that can be faked in controllers tests.</p>

<p>Discussions about the pattern apart, there is no any possible human explanation that justify the existence of <code>HasClickHandler</code>. And by &#8220;human&#8221; I am not being sarcastic. From a human point of view, it makes no sense. It makes sense under the constraints imposed by testing tools and, specially, by the Java language itself, where the only way to specify behavior in a modular way is through the addition of interfaces defining the contract.</p>

<p>Object disorientation is what happens when you go from &#8220;My view has a button I have to test&#8221; to &#8220;My view is stupid, I am going to create a <code>HasClickHandler</code> getter in its interface so I can fake it when testing&#8221;. As of today, no technical choice is free of this problem, but in the case of Java I think its effects are just overwhelming.</p>

<p>When considering using GWT a project I am working on, I was impressed by <a href="http://code.google.com/intl/en/events/io/2009/sessions/GoogleWebToolkitBestPractices.html">this presentation of Ray Ryan</a>. I loved the principles and patterns discussed there for architecting the client part of a web application. The good thing about the presentation it that it focused on the concepts. The bad thing it that he showed very little example code.</p>

<p>It was when I saw how <a href="http://blog.hivedevelopment.co.uk/2009/08/google-web-toolkit-gwt-mvp-example.html">an example implementing those principles</a> looked, that I decided I was going to study javascript hard. And it wasn&#8217;t because the authors didn&#8217;t make an excellent work with the example, but because I understood I wasn&#8217;t going to have fun programming that way. The bad thing is not that implementing the basic GWT &#8220;hello world&#8221; in the proposed way took 600 lines of code (without tests). The bad thing was how ugly everything looked, from top to bottom. And since having fun and beautiful code are two very important things to me, I discarded GWT, I studied javascript and JQuery hard, and worked a lot learning how to mount a good testing environment for this technologies and a Rails backend. And I don&#8217;t regret at all of how things are going. Of course, there are other problems but those will be for another post.</p>

<p>It is curious. 7 years ago, Eric Evan&#8217;s warned the world about how important was to focus on the domain on his seminar <a href="http://www.amazon.com/exec/obidos/ASIN/0321125215/ref=nosim/jorgmanrpersp-20">Domain-Driven Design</a> book. Both design and code should reflect it as much as possible. In this time, despite of the big impact the book had in the Java community and all the discussions surrounding it, there is no still a mainstream implementation of many of the concepts he proposed. I wouldn&#8217;t say it is only due to Java being a poor language for representing the domain with code, but I think it has something to do with it.</p>

<h3>Conclusion</h3>

<p>I always find very difficult to explain what do I think is wrong with Java to other people. After writing this post, I still think the best way to understand it is to study other platforms and paradigms. In fact, the Java Platform offers nowadays a variety of languages, and some of them, like Groovy or Scala, are starting to attract a lot of attention. I still don&#8217;t know anything about Scala but I have used Groovy quite extensively and, although it has the same <code>new</code> operator problem that Java, it is a very nice alternative.</p>]]></content:encoded>
			<wfw:commentRss>http://jorgemanrubia.net/2010/05/30/dependency-injection-and-other-java-necessary-evils/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Testing PURE javascript templates from RSpec</title>
		<link>http://jorgemanrubia.net/2010/05/02/testing-pure-javascript-templates-from-rspec/</link>
		<comments>http://jorgemanrubia.net/2010/05/02/testing-pure-javascript-templates-from-rspec/#comments</comments>
		<pubDate>Sun, 02 May 2010 19:21:39 +0000</pubDate>
		<dc:creator>Jorge Manrubia</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://jorgemanrubia.net/?p=427</guid>
		<description><![CDATA[The traditional Rails approach for AJAX has been using RJS templates. Following this approach, controllers respond to requests by rendering javascript code that is executed in the page on the fly, being the main benefit that you can use a powerful set of ruby helpers for specifying the javascript to be generated. Although this technique [...]]]></description>
			<content:encoded><![CDATA[<p>The traditional Rails approach for <acronym title="Asynchronous JavaScript and XML">AJAX</acronym> has been using RJS templates. Following this approach, controllers respond to requests by rendering javascript code that is executed in the page on the fly, being the main benefit that you can use a powerful set of ruby helpers for specifying the javascript to be generated. Although this technique has shown to be a productive way of adding ajax to web interfaces, it also has serious drawbacks and has been explicitly discouraged by first-class experts on Rails and Javascript like <a href="http://yehudakatz.com/">Yehuda Katz</a>: check  <a href="http://events.jquery.com/jquery-conference-2009/presentations/jQueryConf.pdf">these slides</a> or <a href="http://www.slideshare.net/wycats/jquery-presentation-to-rails-developers-110063">these</a>. It is worth to notice that <a href="http://guides.rails.info/3_0_release_notes.html#action-view">Rails 3 has redefined its <acronym title="Asynchronous JavaScript and XML">AJAX</acronym> approach</a> encouraging an unobtrusive use of javascript. This way you can still use the remote Rails helpers without being tied to a concrete Javascript library.</p>

<p>In my opinion, it just makes sense to have a server sending and receiving data in the form of JSON, and rich clients taking all the responsibility of rendering the UI and handling user interactions using <acronym title="HyperText Markup Language">HTML</acronym>, <acronym title="Cascading Style Sheets">CSS</acronym> and Javascript. The key is that this approach enables you to architect the client part of your application. If some part of the client behavior is left to the server, it becomes more difficult to have a consistent architecture in the client part because concerns are mixed. And without a consistent architecture in the client, it is difficult to provide the user experience that is becoming more and more demanded in today web applications.</p>

<p>So, if you want to render everything in the client using Javascript, you need a good system for creating the <acronym title="HyperText Markup Language">HTML</acronym>, and here it is where javascript templating systems appear. In this post I would like to explain how to test <a href="http://beebole.com/pure/">PURE</a> Javascript templates using <a href="http://rspec.info/">RSpec</a>. I chose to use <a href="http://beebole.com/pure/">PURE</a> because it is a production-ready system for rendering JSON data. The other solid approach seems to be <a href="http://jtemplates.tpython.com/">JTemplates</a>, but it doesn&#8217;t appear to be as actively maintained as PURE. There is also a <a href="http://wiki.github.com/nje/jquery/jquery-templates-proposal">proposal for including a templating system in JQuery</a> that was initially submitted by Microsoft. There is already a <a href="http://github.com/nje/jquery-tmpl">demonstration implementation</a> of the proposal but it is still in the incubation phase.</p>

<h3>Tools</h3>

<p>I am using <a href="http://rspec.info/">RSpec</a> and an amazing set of tools for running javascript from Ruby code:</p>

<ul>
<li><a href="http://github.com/mynyml/harmony">Harmony</a>: which wraps <a href="http://github.com/jbarnette/johnson/">Johnson</a> and <a href="http://github.com/jeresig/env-js">env.js</a> letting you run javascript code in a <acronym title="Document Object Model">DOM</acronym> environment from ruby.</li>
<li><a href="http://github.com/mynyml/holygrail">Holy Grail</a>: the Harmony plugin for Rails. It allows you to run javascript code in the context of your view tests.</li>
</ul>

<p>Just follow the instructions in the sites or Harmony and Holy Grail for installing them. For the installation of Holy Grail with RSpec, <a href="http://kenmayer.mp/blog/getting-holygrail-to-work-with-rspec-cucumber">read this article from Ken Mayer</a>.</p>

<h3>The example</h3>

<p>For the sake of simplicity I will try to keep the PURE part as minimal as possible. I want to render the following <code>person</code> object.</p>

<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Jorge&quot;</span><span style="color: #009900;">&#125;</span></div></div>

<p>And I will use the following PURE template:</p>

<div class="codecolorer-container html4strict blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;person-template&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;name&quot;</span><span style="color: #66cc66;">/</span>&gt;</span></div></div>

<p>Although in practice you will be using <a href="http://beebole.com/pure/documentation/rendering-with-directives/">PURE directives</a> for sure, for the example I will use the <a href="http://beebole.com/pure/documentation/auto-rendering/">PURE auto-rendering mechanism</a>.</p>

<p>What I want is to write a RSpec view spec to test that the template renders JSon as expected:</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">describe <span style="color:#996600;">&quot;persons/_person_template.html.erb&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; it <span style="color:#996600;">&quot;should render a proper container for the person&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; person = <span style="color:#006600; font-weight:bold;">&#123;</span>:name<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;Jorge&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; render_javascript_template<span style="color:#006600; font-weight:bold;">&#40;</span>person<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>rendered_text<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; rendered_text.<span style="color:#9900CC;">should</span> have_selector<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;div.name&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:content</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;Jorge&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>

<p>The helper method <code>render_javascript_template</code> receives a model object to render, and yields to a closure providing it with the result of the rendered template. The rendered text can be then checked using, in this case, <a href="http://github.com/brynary/webrat">Webrat</a> matchers. The code of this helper method is shown bellow.</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;">module</span> ViewHelpers<br />
&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> render_javascript_template<span style="color:#006600; font-weight:bold;">&#40;</span>data<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; render<br />
&nbsp; &nbsp; include_javascript_files<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">yield</span> do_render_javascript_template<span style="color:#006600; font-weight:bold;">&#40;</span>data<span style="color:#006600; font-weight:bold;">&#41;</span> &nbsp; &nbsp;<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> include_javascript_files<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#123;</span>jquery.<span style="color:#9900CC;">js</span> pure.<span style="color:#9900CC;">js</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>file<span style="color:#006600; font-weight:bold;">|</span> js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;load('public/javascripts/#{file}');&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> as_javascript_string<span style="color:#006600; font-weight:bold;">&#40;</span>text<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; text.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">collect</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>line<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#996600;">&quot;'#{line}'&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'+'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> template_id_from_path<span style="color:#006600; font-weight:bold;">&#40;</span>template_path<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; template_path.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^<span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">+</span>\<span style="color:#006600; font-weight:bold;">/</span>_<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>_<span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">&quot;-&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">html</span>.<span style="color:#9900CC;">erb</span>$<span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> do_render_javascript_template<span style="color:#006600; font-weight:bold;">&#40;</span>data<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; json_data = as_javascript_string<span style="color:#006600; font-weight:bold;">&#40;</span>data.<span style="color:#9900CC;">to_json</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; template_id = template_id_from_path<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">description_parts</span>.<span style="color:#9900CC;">first</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;var data=$.parseJSON(#{json_data});&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;var $template = $('##{template_id}').clone().appendTo($('&lt;div&gt;&lt;/div&gt;'));&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;var renderedElement = $template.autoRender(data);&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">return</span> js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;$('&lt;div&gt;&lt;/div&gt;').append(renderedElement).html()&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>

<p>The <code>render_javascript_template</code> method has to do basically two things before rendering the templates using PURE:</p>

<ul>
<li>Rendering the template that contains the <acronym title="HyperText Markup Language">HTML</acronym> of the template (in this example it is the ERB file itself).</li>
<li>Including the required javascript files of PURE and JQuery.</li>
</ul>

<p>The order of these steps is important: if you don&#8217;t invoke <code>render</code> before requiring the javascript files, JQuery won&#8217;t work properly.</p>

<p>The method that does the work is <code>do_render_javascript_template</code>. It basically does three things:</p>

<ul>
<li>It converts the json of the data to be rendered to a form suitable to be injected in the javascript runtime as a string literal (javascript doesn&#8217;t admit multi-lines literals).</li>
<li>It then obtains the template <acronym title="Cascading Style Sheets">CSS</acronym> id that is going to be used to locate the template node we want to use. It obtains it from the file path of the template, which is specified in the outer describe of the spec.</li>
<li>Finally it invokes the PURE auto-rendering process with the template. The only tip is that it appends an artificial container to the template because PURE fails if the template node hasn&#8217;t a parent. In the same way, before calling the JQuery function <code>html()</code>, an artificial root node is added because JQuery ignore root nodes with elements detached from the <acronym title="Document Object Model">DOM</acronym>.</li>
</ul>

<h3>Conclussions</h3>

<p>While I test all my javascript code using <a href="http://visionmedia.github.com/jspec/">JSpec</a>, it didn&#8217;t fit well for testing my PURE templates. I had read about Harmony and I wondered if it would be possible to make the testing of PURE templates using RSpec and the powerful Webrat matchers. It happened to be not only possible, but a surprisingly fast and comfortable way of testing the templates. I am willing to see a templating system finally included with JQuery, but PURE is a powerful and fast choice that works today. I really think Javascript templates are here to stay.</p>

<p>The code of this post is availabe as a <a href="http://gist.github.com/387503">gist at github</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://jorgemanrubia.net/2010/05/02/testing-pure-javascript-templates-from-rspec/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>I have read&#8230; &#8216;The Inmates Are Running the Asylum&#8217;</title>
		<link>http://jorgemanrubia.net/2010/03/19/i-have-read-the-inmates-are-running-the-asylum/</link>
		<comments>http://jorgemanrubia.net/2010/03/19/i-have-read-the-inmates-are-running-the-asylum/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 21:14:28 +0000</pubDate>
		<dc:creator>Jorge Manrubia</dc:creator>
				<category><![CDATA[Books]]></category>

		<guid isPermaLink="false">http://jorgemanrubia.net/?p=384</guid>
		<description><![CDATA[I have recently finished reading The Inmates Are Running the Asylum: Why High Tech Products Drive Us Crazy and How to Restore the Sanity, by Alan Cooper. It is a book about user experience and interaction design. About why it is so important, why most people in the industry of software development don&#8217;t pay attention [...]]]></description>
			<content:encoded><![CDATA[<p>I have recently finished reading <a href="http://www.amazon.com/exec/obidos/ASIN/0672326140/ref=nosim/jorgmanrpersp-20"><em>The Inmates Are Running the Asylum: Why High Tech Products Drive Us Crazy and How to Restore the Sanity</em></a>, by Alan Cooper. It is a book about user experience and interaction design. About why it is so important, why most people in the industry of software development don&#8217;t pay attention to it, and the consequences of that. It is a superb reading I totally recommend.</p>

<p>In my opinion, its current scoring in Amazon (4 stars) is difficult to understand. I would give it a 6 if I could. I think it is better than <a href="http://www.amazon.com/exec/obidos/ASIN/0470084111/ref=nosim/jorgmanrpersp-20">About Face 3: The Essentials of Interaction Design</a>, also by Cooper. With this last one my expectations were very high but weren&#8217;t completely fulfilled (I think a 40% percent of the book was avoidable). But each page of <em>The inmates&#8230;</em> contains a lot of thoughts you can learn from. With many sentences I found myself thinking: &#8216;that&#8217;s totally obvious&#8230; but I hadn&#8217;t stopped to think about it until I read this&#8217;. And that&#8217;s exactly my favorite reaction when I am reading a technical book.</p>

<p>I would like to talk about the role of interaction design in two applications I use daily.</p>

<h3>Lotus Notes: how not to design user interfaces</h3>

<p><a href="http://en.wikipedia.org/wiki/IBM_Lotus_Notes">Lotus Notes</a> is the mail client I have to use at work. There are so many wrong things about it that you can easily find web pages like <a href="http://techrepublic.com.com/5208-6230-0.html?forumID=102&amp;threadID=248584&amp;start=0">Is Lotus Notes the worst business application ever?</a>, <a href="http://lotusnotessucks.4t.com/index.html">Lotus Notes Sucks</a> or <a href="http://www.guardian.co.uk/technology/blog/2006/jan/24/islotusnotes">Is Lotus Notes the world&#8217;s worst application?</a>.</p>

<p>Why does this application provoke these kind of reactions?. I think the key is its user interface and the experience it produces in the poor user. Let me show you an example:</p>

<p><a href="http://jorgemanrubia.net/blog/wp-content/uploads/2010/03/lotus_notes_screenshot.jpg">
    <img src="http://jorgemanrubia.net/blog/wp-content/uploads/2010/03/lotus_notes_screenshot.jpg" style="width: 70%;"></img>
</a></p>

<p>The screenshot shows how Lotus Notes let you manage your mail. It offers you 48 buttons with their 48 icons to do that (and the real scary part comes when you try to search for some functionality using the chaotic  and non-standard menus). Managing your mail is about reading, writing, searching and organizing. None of these very fundamental goals is properly covered by Notes.</p>

<ul>
<li><p><strong>Reading.</strong> Notes let you activate a &#8220;Preview Panel&#8221; that split the window in two parts, so you can read the contents of mails without opening them in new windows. It also draws in red color the mails you haven&#8217;t read yet. If you have your inbox empty (and I always keep it empty), when a new mail arrives, and you have the preview panel open, the mail is automatically marked as &#8216;read&#8217;. This behavior has made me miss several mails.</p>

<p>Another good feature is that there is no obvious mechanism to force the checking of new mail. When some colleague has just send me an email I want to check, I close my inbox and open it again.</p></li>
<li><p><strong>Searching.</strong> This is where Notes really rocks. It makes searching your messages so difficult that I really think about it twice before doing it. It offers you two icons: a magnifier and some binoculars (you can look for them in the screenshot, if you like the &#8216;Where&#8217;s Wally?&#8217; kind of games). Even today I can&#8217;t tell you the difference. I always use the keyboard shortcut that let me search in the subjects of the selected folder (so if I want to search in all my mail, I have to select the &#8220;All messages&#8221; folder).</p>

<p>There is another &#8220;search in deep&#8221; functionality but it is so slow that I difficultly dare to use it. When you are used to Gmail, or you have seen the way Apple Mail does it, searching in Notes is like going back 20 years.</p></li>
<li><p><strong>Organizing.</strong> I like to archive most of the mails I receive. I have to do it in a local inbox, since my remote account has a limited size. I have tried hardly to find a shortcut for the action of archiving email, since I am always doing it. I haven&#8217;t being able to. I have googled for it, searched in the menus, looked for it in the help of Notes&#8230; Nothing. I am sure there is a way but I have been more than two years dragging the mail with my mouse, from the inbox, to the archive folder. There are like 10 buttons in the top of my inbox, each one with its shortcut, and no one of them could do this.</p>

<p>Organizing in folders is also very difficult. The problems start even before the drag and drop process. If you try to reorganize the folders hierarchy you&#8217;ll know what I am talking about.</p></li>
</ul>

<p>Allan Cooper talks about 14 factors that make software polite. Lotus Notes violates them all. For example, when you want to delete a file attachment of a message, an annoying modal dialog ask you whether you are sure, because it cannot be undone. Another example is the inability Lotus has to remember the state I left the workbench. Everyday I start Notes and it shows me the start panel. I always open my personal mail, and also the corporate mail of my team. Everyday I have to repeat the same operation and it annoys me, specially because Notes is not a lightweight and responsive product.</p>

<p>In a few months we will start using Microsoft Outlook at work. I haven&#8217;t used Outlook in the last 6 years but I am totally convinced I am going to be much more happy (because whatever Outlook does to manage your mail, it couldn&#8217;t be worse).</p>

<h3>Mac <acronym title="Operating System">OS</acronym> X Time Machine: how complex systems can have wonderful user interfaces</h3>

<p>If you have ever used some kind of Backup software, you can easily think about how many options you can configure: destination folder, backups periodicity, whether to compress them or not and the level of compression, scheduling, folders to include and exclude&#8230; Simply configuring the scheduling can make you visit several wizard pages choosing the days of the week, the exact time of each backup and so on. The next screenshot shows the configuration options of <a href="timemachine">Time Machine</a>, the Mac <acronym title="Operating System">OS</acronym> X backup system:</p>

<p><a href="http://jorgemanrubia.net/blog/wp-content/uploads/2010/03/time_machine_screenshot.png">
    <img src="http://jorgemanrubia.net/blog/wp-content/uploads/2010/03/time_machine_screenshot.png" style="width: 70%;"></img>
</a></p>

<p>You don&#8217;t have to worry about configuring anything because the application makes all the decisions for you. And I loved that approach. Even in the case where the backup disk run out of space, the system warns you about it, telling you that it is going to start discarding old backups. It makes the decision for you and let you keep working.</p>

<p>And this way of doing things is totally logical because, after all, your most probable goals when you are using backup software don&#8217;t include spending your time creating enterprise-class backup configurations and schedules. What you probably want are two things: recovering some missing files occasionally, or restoring the full system in case of disaster. And both things can be done easily with the Time Capsule. And in the case of browsing the history of your files, the <a href="http://www.youtube.com/watch?v=R8vx9cI9yTY">interaction design is totally outstanding</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://jorgemanrubia.net/2010/03/19/i-have-read-the-inmates-are-running-the-asylum/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>On Eclispe buddy class loading and performance problems</title>
		<link>http://jorgemanrubia.net/2010/02/09/on-eclispe-buddy-class-loading-and-performance-problems/</link>
		<comments>http://jorgemanrubia.net/2010/02/09/on-eclispe-buddy-class-loading-and-performance-problems/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 21:42:50 +0000</pubDate>
		<dc:creator>Jorge Manrubia</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://jorgemanrubia.net/?p=322</guid>
		<description><![CDATA[At work we have been experiencing serious performance problems with the Eclipse feature we are developing. We have just solved the problem and I would like to share the chain of events that caused it. I will elaborate on this a little bit, but in a few words: If you want to wait for jobs [...]]]></description>
			<content:encoded><![CDATA[<p>At work we have been experiencing serious performance problems with the Eclipse feature we are developing. We have just solved the problem and I would like to share the chain of events that caused it. I will elaborate on this a little bit, but in a few words:</p>

<ul>
<li>If you want to wait for jobs in Eclipse Junit Plugin tests, <a href="https://jira.jboss.org/jira/browse/JBIDE-2128">use this method</a> instead of the proposed in the second edition of the book <a href="http://www.amazon.com/exec/obidos/ASIN/0321228472/ref=nosim/jorgmanrpersp-20"><em>Eclipse: Building Commercial-Quality Plug-Ins</em></a>.</li>
<li>The <code>global</code> buddy class loading policy can affect performance seriously. It is logical and they warn you about it. I can confirm it is true. </li>
</ul>

<h3>Waiting for jobs in Eclipse JUnit Plugin Tests</h3>

<p>The <a href="http://www.eclipse.org/pde/">Eclipse PDE</a> provides a launcher that let you test your plugins using JUnit. It basically instantiates a new Eclipse workbench and, when it is up, the tests are run inside it. When writing tests, a common need is waiting for jobs to finish. After launching a job for testing its results, the job is run in a separate thread, so the test code will continue running if you don&#8217;t stop it.</p>

<p>The book <a href="http://www.amazon.com/exec/obidos/ASIN/0321228472/ref=nosim/jorgmanrpersp-20"><em>Eclipse: Building Commercial-Quality Plug-Ins</em></a> is an excellent resource for learning Eclipse plugin development. It proposes some utility methods for using with Eclipse Plugin tests. One of this methods is indeed <code>waitForJobs()</code>:</p>

<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> waitForJobs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>Platform.<span style="color: #006633;">getJobManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">currentJob</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; delay<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>

<p>This implementation worked well for us until one day we decided to divide a plugin that had become too big. I started by creating a new plugin extracting some code of the plugin along with its tests. But the tests always failed launching a <code>NoClassDefFoundError</code> exception. I was totally convinced that this problem was related to the way we had configured class dependencies between plugins. We use Groovy and the exception was always launched from the groovy libraries referenced from groovy compiled code. I spend a lot of time trying to figure out what was going on. It was a colleague who pointed out that it seemed that the test code was not waiting for the job under test to finish. And he was right. After searching in google I found <a href="waitforjobs">a more robust implementation of the <code>waitForJobs()</code></a> method:</p>

<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> waitForJobs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">long</span> start <span style="color: #339933;">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Job.<span style="color: #006633;">getJobManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isIdle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; delay<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span>start<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> MAX_IDLE <span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aruntimeexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">RuntimeException</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;A long running task detected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>

<p>Using this approach solved the problem. This would had nothing to do with performance if I hadn&#8217;t messed up with the class loading configuration trying to solve it. Basically I made many of our plugins to use a <code>global</code> buddy class loading policy and, once the problem was solved, I didn&#8217;t undo it. And, while we perceived a poor performance in our plugin, it wasn&#8217;t until recently that we discovered the cause was precisely that <code>global</code> policy (we thought it was a problem with the development installation of the system our product connects to). And this brings me to the next section.</p>

<h3>Eclipse Buddy Class Loading</h3>

<p>The Eclipse core platform runs on a sophisticated runtime for Java components (<a href="http://www.eclipse.org/equinox/">Equinox</a>, an <a href="http://en.wikipedia.org/wiki/Osgi">OSGI</a> implementation). This runtime provides a powerful architecture for installing and running Java modules (bundles or plugins). For doing so, it redefines completely the class loading system of the Standard Java Platform. When running inside Eclipse, each bundle has its own class loader. Bundles declare in their manifests how do they relate to other bundles, so their class loaders know how to locate external classes.</p>

<p>When a bundle <em>A</em> depends on a bundle <em>B</em> it can access its exported classes with no problem. The problem comes when it is <em>B</em> who wants to load classes from <em>A</em> dynamically,  but it can&#8217;t depend on it (because it would create a circular dependence, or just because it doesn&#8217;t make sense). For example, the <em>Log4j</em> bundle can&#8217;t depend on a bundle that provides a <code>log4.properties</code> configuration file but it still wants to locate it in execution time. We have a number of scenarios like this in our application, where some bundles need to load classes/resources from other bundles they don&#8217;t know in execution time.</p>

<p>In these situations <a href="http://wiki.eclipse.org/index.php/Context_Class_Loader_Enhancements#Buddy_Policy">Eclipse Buddy Class Loading</a> come to the rescue (another approach, better but more complex, is to use <a href="http://www.eclipsezone.com/articles/extensions-vs-services/">extension points</a> for allowing plugins to be extended in execution time). Buddy policies allow one bundle to declare that it needs to load classes from other bundles (buddies). When a bundle class loader fails to locate some class, it starts searching for it in the declared buddies. The system can configure a number of policies that determine the way classes are searched:</p>

<ul>
<li><code>dependent</code>: search in bundles that directly or indirectly depend on the bundle.</li>
<li><code>required</code>: search in bundles that explicitly declare themselves as buddies of the caller bundle.</li>
<li><code>global</code>: search in all the bundles.</li>
</ul>

<p>There are <a href="http://wiki.eclipse.org/index.php/Context_Class_Loader_Enhancements#Buddy_Policy_2">others policies available</a> but I think the first two are the most common. Buddy policies are configured in the <code>MANIFEST.MF</code> file of the bundle that needs to load the classes. For example, in the manifest of the <code>org.apache.log4j</code> plugin you can find this:</p>

<div class="codecolorer-container properties blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="properties codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Eclipse-BuddyPolicy: registered</div></div>

<p>Since it uses a <code>registered</code> policy, if you want your bundle to provide a properties file for log4j, you should declare your bundle as a buddy of Log4J.</p>

<div class="codecolorer-container properties blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="properties codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Eclipse-RegisterBuddy: org.apache.log4j</div></div>

<p>With a <code>dependent</code> policy this last step is not necessary. As it is obvious, <code>registered</code> is more efficient than <code>dependent</code>, and this one is more efficient than <code>global</code>. In a product like our feature, which is big but not huge, when we changed from <code>global</code> to <code>dependent</code> we observed a huge performance boost, specially the first time the product functions were launched (which is when classes are loaded).</p>

<p>When you try to solve a mysterious bug for long time enough, you are so satisfied when yo do it that you don&#8217;t spend too much time blaming yourself for having produced it. And that was my state after this one&#8230;</p>]]></content:encoded>
			<wfw:commentRss>http://jorgemanrubia.net/2010/02/09/on-eclispe-buddy-class-loading-and-performance-problems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using macros to create custom example groups in RSpec</title>
		<link>http://jorgemanrubia.net/2010/01/16/using-macros-to-create-custom-example-groups-in-rspec/</link>
		<comments>http://jorgemanrubia.net/2010/01/16/using-macros-to-create-custom-example-groups-in-rspec/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 01:25:54 +0000</pubDate>
		<dc:creator>Jorge Manrubia</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://jorgemanrubia.net/2010/01/16/using-macros-to-create-custom-example-groups-in-rspec/</guid>
		<description><![CDATA[I am involved in a personal project where I am developing a web application with Rails. I am using RSpec and Cucumber, and I am following an outside-in, behavior-driven development approach, as recommended in the excellent RSpec Book. In this post I would like to share a problem I found and how I solved it. [...]]]></description>
			<content:encoded><![CDATA[<p>I am involved in a personal project where I am developing a web application with <a href="http://rubyonrails.org/">Rails</a>. I am using <a href="http://rspec.info/">RSpec</a> and <a href="http://cukes.info/">Cucumber</a>, and I am following an outside-in, behavior-driven development approach, as recommended in <a href="http://pragprog.com/titles/achbd/the-rspec-book">the excellent RSpec Book</a>. In this post I would like to share a problem I found and how I solved it.</p>

<p>It started when I tried to refactor some duplicated code in my controllers specs. It was related to the authentication system. Specs should test their behavior in both authenticated and insecure contexts, to be sure that anything happens if an anonymous user sends a <code>POST</code> action to your <code>WorldDestroyerController</code>. For example:</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">describe UserSessionsController, <span style="color:#996600;">&quot;DELETE destroy&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; should_require_login <span style="color:#ff3333; font-weight:bold;">:delete</span>, <span style="color:#ff3333; font-weight:bold;">:destroy</span> <br />
<br />
&nbsp; context <span style="color:#996600;">&quot;authenticated user&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; login_as_user<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; it <span style="color:#996600;">&quot;should destroy the user session&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@current_user_session</span>.<span style="color:#9900CC;">should_receive</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:destroy</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; delete <span style="color:#ff3333; font-weight:bold;">:destroy</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; it <span style="color:#996600;">&quot;should redirect to the login page&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; delete <span style="color:#ff3333; font-weight:bold;">:destroy</span><br />
&nbsp; &nbsp; &nbsp; response.<span style="color:#9900CC;">should</span> redirect_to login_path<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> &nbsp;<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>

<p>The next fragment was recurrent in all my examples.</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">context <span style="color:#996600;">&quot;authenticated user&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; login_as_user<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
&nbsp; ...</div></div>

<p>For every example in the example group, it basically stubs the controller method that validates sessions in order to get a valid one when invoked (I am using <a href="http://github.com/binarylogic/authlogic">authlogic</a>). I wanted to create a macro <code>context_authenticated</code> so I could code the previous example like this:</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">describe UserSessionsController, <span style="color:#996600;">&quot;DELETE destroy&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; should_require_login <span style="color:#ff3333; font-weight:bold;">:delete</span>, <span style="color:#ff3333; font-weight:bold;">:destroy</span> <br />
<br />
&nbsp; context_authenticated <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; it <span style="color:#996600;">&quot;should destroy the user session&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@current_user_session</span>.<span style="color:#9900CC;">should_receive</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:destroy</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; delete <span style="color:#ff3333; font-weight:bold;">:destroy</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; it <span style="color:#996600;">&quot;should redirect to the login page&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; delete <span style="color:#ff3333; font-weight:bold;">:destroy</span><br />
&nbsp; &nbsp; &nbsp; response.<span style="color:#9900CC;">should</span> redirect_to login_path<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> &nbsp;<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>

<p>My first attempt was to write a macro that included the before block and yielded to the block provided in the invocation:</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> context_authenticated<br />
&nbsp; context <span style="color:#996600;">&quot;authenticated user&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; login_as_user<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">yield</span> <span style="color:#008000; font-style:italic;">#Wrong</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>

<p>The examples failed because the <code>before(:each)</code> fragment was not being invoked before executing them. To discover the problem I had to <a href="http://rspec.rubyforge.org/rspec/1.3.0/">investigate a little bit about how the RSpec <acronym title="Domain Specific Language">DSL</acronym> works</a>. Basically:</p>

<ul>
<li>Each <code>context</code> (alias for <code>describe</code>) creates a new <code>ExampleGroup</code> subclass.</li>
<li>Each <code>it</code> (alias for <code>example</code>) creates a new instance of the current <code>ExampleGroup</code> subclass where it is invoked.</li>
</ul>

<p>So the problem was related to the very nature of closures. In Ruby, a block of code (<code>proc</code> or <code>lambda</code>) maintains the bindings in effect for that closure. The bindings contains not only variable references, but also the <code>self</code> reference itself.</p>

<p>In my case, what I was doing was to submit a closure with the <code>it</code> examples to the macro. The execution context of this closure was the one where it was defined: the <code>ExampleGroup</code> subclass created by <code>describe UserSessionsController...</code>. However, inside the macro, a new <code>ExampleGroup</code> subclass was created, and the <code>before</code> block was associated with that example group. That was the reason why the the <code>before</code> code was not getting executed before the submitted examples. Once I understood this, the solution was simple:</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> context_authenticated<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>example_group_block<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; example_group_class = context <span style="color:#996600;">&quot;authenticated user&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; login_as_user<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; example_group_class.<span style="color:#9900CC;">class_eval</span> <span style="color:#006600; font-weight:bold;">&amp;</span>example_group_block<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>

<p>What the previous code does is to change the evaluation context of the closure, so it get executed inside the <code>ExampleGroup</code> class created by the macro.</p>]]></content:encoded>
			<wfw:commentRss>http://jorgemanrubia.net/2010/01/16/using-macros-to-create-custom-example-groups-in-rspec/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>I have read&#8230; &#8216;The Mythical Man-Month: Essays on Software Engineering&#8217;</title>
		<link>http://jorgemanrubia.net/2010/01/09/i-have-read-the-mythical-man-month-essays-on-software-engineering/</link>
		<comments>http://jorgemanrubia.net/2010/01/09/i-have-read-the-mythical-man-month-essays-on-software-engineering/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 02:18:52 +0000</pubDate>
		<dc:creator>Jorge Manrubia</dc:creator>
				<category><![CDATA[Books]]></category>

		<guid isPermaLink="false">http://jorgemanrubia.net/2010/01/09/i-have-read-the-mythical-man-month-essays-on-software-engineering/</guid>
		<description><![CDATA[I have decided to write my first review on my last reading: The Mythical Man-Month: Essays on Software Engineering. The title of the book refers to the the unit of effort man-month. It is used by (I want to think &#8216;old&#8217;) metrics to estimate and schedule software development. As Brooks explains in his book, while [...]]]></description>
			<content:encoded><![CDATA[<p>I have decided to write my first review on my last reading: <a href="http://www.amazon.com/exec/obidos/ASIN/0201835959/ref=nosim/jorgmanrpersp-20">The Mythical Man-Month: Essays on Software Engineering</a>.</p>

<p><a href="http://www.amazon.com/exec/obidos/ASIN/0201835959/ref=nosim/jorgmanrpersp-20"><img src="http://ecx.images-amazon.com/images/I/51WIpM70FEL._SL160_.jpg" alt="Cover image of 'The Mythical Man-Month: Essays on Software Engineering'" /></a></p>

<p>The title of the book refers to the the unit of effort <em>man-month</em>. It is used by (I want to think &#8216;old&#8217;) metrics to estimate and schedule software development. As Brooks explains in his book, while <em>cost</em> varies on the product of number of people and number of months, <em>progress</em> does not. It would do it if tasks in software development could be partitioned between individuals with no communication among them. When you take into consideration the need of <em>communication</em> between parts, the effort increases as <code>n(n-1)/2</code>. The famous <a href="http://en.wikipedia.org/wiki/Brooks_Law">Brook&#8217;s Law</a> appeared in this book as an oversimplifying but eloquent way to capture the problem:</p>

<blockquote>
  <p>Adding manpower to a late software project makes it later</p>
</blockquote>

<p>If you are interested in reading about the importance of communication in software development I recommend you to read <em><a href="http://www.amazon.com/exec/obidos/ASIN/0321482751/ref=nosim/jorgmanrpersp-20">Agile Software Development: The Cooperative Game</a></em>. This book contains the most sensible discussion on the nature of software development that I have ever read. Cockburn defends that software development is <q>a cooperative game of invention and communication</q>.</p>

<p>Returning to the <em>The Mythical Man-Month</em> book, Another thing I loved is how it talks about the human factor in software development. In the chapter <em>The surgical team</em>, Fred Brooks says the following:</p>

<blockquote>
  <p>The conclusion is simple: if a 200-man project has 25 managers who are the most competent and experienced programmers, fire the 175 troops and put the managers back to programming</p>
</blockquote>

<p>When I read this sentence I was totally amazed. Notice that the author was the project manager of the massive software system of IBM System/360 (<acronym title="Operating System">OS</acronym>/360). So more than 3 decades ago, a software developer with experience in developing huge programs, didn&#8217;t emphasized the need of ceremony and heavyweight recipes for software development. Instead he could clearly see what many people can&#8217;t today. He proposed that the most expert programmers should act as surgeons leading a surgical team. The rest of the team should assist them in order to maximize their effectiveness with the core tasks of writing the specs, designing, coding and testing.</p>

<p>Fred Brooks mentions a <a href="http://portal.acm.org/citation.cfm?id=362858">1968 study</a> that concludes that the best programmers are 10 times more <em>productive</em> that the worst ones. Robert L. Glass in <a href="http://www.amazon.com/exec/obidos/ASIN/0321117425/ref=nosim/jorgmanrpersp-20">Facts and Fallacies of Software Engineering</a> talks about numbers ranging from 5 times better to 28 times better. Ignoring this fact is, in my opinion, the source of many deep problems in the spanish software development industry today. Companies don&#8217;t see technical talent as an asset to make more money, but simply like something academical.</p>

<p>Although these were the topics I enjoyed most, the book covers many other software development principles which are totally valid today: the importance of prototyping and the <a href="http://en.wikipedia.org/wiki/Wicked_problem">wicked</a> nature of the discipline, the dangers of over-designing, the need of cohesive teams with good communication mechanisms&#8230; The visionary condition of the author was proved again with his famous article <a href="http://en.wikipedia.org/wiki/No_Silver_Bullet"><em>No silver bullet</em></a>, which stated that nothing in the next 10 years (from 1986) would provide one order of magnitude improvement in software development. The article is included in the book by the way.</p>

<p>In conclusion, it is a book that I totally recommend. The principles it talks about are totally valid today and I really enjoyed reading about them considering the time the book was written. I am simply amazed by the fact that 34 years ago, someone was able to recapitulate so many true and concise ideas on software engineering, even before that software engineering, as a discipline, existed.</p>]]></content:encoded>
			<wfw:commentRss>http://jorgemanrubia.net/2010/01/09/i-have-read-the-mythical-man-month-essays-on-software-engineering/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My book backlog</title>
		<link>http://jorgemanrubia.net/2009/12/07/my-book-backlog/</link>
		<comments>http://jorgemanrubia.net/2009/12/07/my-book-backlog/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 01:30:43 +0000</pubDate>
		<dc:creator>Jorge Manrubia</dc:creator>
				<category><![CDATA[Books]]></category>

		<guid isPermaLink="false">http://jorgemanrubia.net/2009/12/07/my-book-backlog/</guid>
		<description><![CDATA[I have created an electronic registry of the books I read. To create it I have used the Now Reading Reloaded extension, based in the excellent WordPress Now Reading extension. This extension makes very easy adding books searching for them in Amazon, and retrieving their data from Amazon&#8217;s servers (including their cover image). It also [...]]]></description>
			<content:encoded><![CDATA[<p>I have created an <a href="http://jorgemanrubia.net/book-backlog/">electronic registry of the books I read</a>. To create it I have used the  <a href="http://wordpress.org/extend/plugins/now-reading-reloaded/">Now Reading Reloaded extension</a>, based in the excellent <a href="http://robm.me.uk/projects/plugins/wordpress/now-reading">WordPress Now Reading extension</a>. This extension makes very easy adding books searching for them in <a href="http://www.amazon.com/">Amazon</a>, and retrieving their data from Amazon&#8217;s servers (including their cover image). It also let you manage the life cycle of books (reading now, finished and unread).</p>

<p>So I installed this extension, went to my physical library and added the technical books I own. Then I modified the <acronym title="Pre-Hypertext Processing">PHP</acronym> templates to fit my page theme and show the results in the way I wanted.</p>

<p>My last two Amazon&#8217;s orders have been quite big (the current USD/EUR exchange rate has something to do with it), so now I have 16 books waiting to be read in my library (and a few more in <a href="http://www.amazon.com/registry/wishlist/36SJ6E0V7WWWR/ref=cm_wl_act_vv?_encoding=UTF8&amp;visitor-view=1&amp;reveal=">my Amazon&#8217;s wish list</a>). In recent times I have focused my reading mainly in agile development, ruby on rails, good programming practices and, to a lesser extent, user experience design.</p>

<p>I intend to write reviews of the books I read in the form of blog posts. I&#8217;ll link back the reviews from the <a href="http://jorgemanrubia.net/book-backlog/">book backlog</a>, using a Now Reading feature that let you associate a post number to book entries.</p>]]></content:encoded>
			<wfw:commentRss>http://jorgemanrubia.net/2009/12/07/my-book-backlog/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

