<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>silk and spinach</title>
	
	<link>http://silkandspinach.net</link>
	<description>development, by example</description>
	<lastBuildDate>Tue, 21 May 2013 12:50:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain="silkandspinach.net" port="80" path="/?rsscloud=notify" registerProcedure="" protocol="http-post" />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>silk and spinach</title>
		<link>http://silkandspinach.net</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://silkandspinach.net/osd.xml" title="silk and spinach" />
	
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/SilkAndSpinach" /><feedburner:info uri="silkandspinach" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://silkandspinach.net/?pushpress=hub" /><feedburner:browserFriendly>This is an XML content feed. It is intended to be viewed in a newsreader or syndicated to another site.</feedburner:browserFriendly><item>
		<title>Query actions in Rails controllers</title>
		<link>http://feedproxy.google.com/~r/SilkAndSpinach/~3/eIMsMutySe8/</link>
		<comments>http://silkandspinach.net/2013/05/14/query-actions-in-rails-controllers/#comments</comments>
		<pubDate>Tue, 14 May 2013 19:04:05 +0000</pubDate>
		<dc:creator>Kevin Rutherford</dc:creator>
				<category><![CDATA[hexagonalarchitecture]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[hexagonalrails]]></category>

		<guid isPermaLink="false">http://silkandspinach.net/?p=1442</guid>
		<description><![CDATA[Recently some of my controller actions have taken on a definite new shape. Particularly when the action is a read-only query of the app&#8217;s state. Such actions tend to make up the bulk of my apps, and they can be simple because they are unlikely to &#8220;fail&#8221; in any predictable way. Here&#8217;s an example from [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1442&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Recently some of my controller actions have taken on a definite new shape. Particularly when the action is a read-only query of the app&#8217;s state. Such actions tend to make up the bulk of my apps, and they can be simple because they are unlikely to &#8220;fail&#8221; in any predictable way. Here&#8217;s an example from my wiki app:</p>
<script src="https://gist.github.com/kevinrutherford/5578425.js?file=cards_controller.rb"></script>
<p>This has a couple of significant plus points: First, no instance variables, so both the controller action and the view are more readable and easier to refactor. Second, no instance variables! So there&#8217;s a clear, documented, named interface between the controller and the view. And third, this is so transparently readable that I never bother to test it.</p>
<p>The wiki used in the above action is a repository, built in a memoizing helper method that most of the controllers use:</p>
<script src="https://gist.github.com/kevinrutherford/5578425.js?file=application_controller.rb"></script>
<p>In this case the correct kind of repository is created for the current user, and all of the other code in this request sits on top of that. So the controller action, helped by the memoized repository builder, effectively constructs an entire hexagonal architecture for each request, and the domain logic is thus blissfully unaware of its context.</p>
<p>Here&#8217;s a slightly bigger example. This is for a page that shows a variety of informative graphs about the wiki; and because I may want to re-organise my admin pages in the future, each graph&#8217;s data is built independently of the others:</p>
<script src="https://gist.github.com/kevinrutherford/5578425.js?file=statistics_controller.rb"></script>
<p>That&#8217;s the most complex query controller action I have, and I maintain that it&#8217;s so simple I don&#8217;t need to test it. Would you?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/silkandspinach.wordpress.com/1442/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/silkandspinach.wordpress.com/1442/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1442&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=eIMsMutySe8:YOJrldZi4g4:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=eIMsMutySe8:YOJrldZi4g4:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=eIMsMutySe8:YOJrldZi4g4:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=eIMsMutySe8:YOJrldZi4g4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=eIMsMutySe8:YOJrldZi4g4:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SilkAndSpinach/~4/eIMsMutySe8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://silkandspinach.net/2013/05/14/query-actions-in-rails-controllers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c39ead2d025bde14f3f39b20ff7d113?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">kevinrutherford</media:title>
		</media:content>
	<feedburner:origLink>http://silkandspinach.net/2013/05/14/query-actions-in-rails-controllers/</feedburner:origLink></item>
		<item>
		<title>Why shorter methods are better</title>
		<link>http://feedproxy.google.com/~r/SilkAndSpinach/~3/wT6lEtOsE38/</link>
		<comments>http://silkandspinach.net/2013/01/30/why-shorter-methods-are-better/#comments</comments>
		<pubDate>Wed, 30 Jan 2013 19:40:39 +0000</pubDate>
		<dc:creator>Kevin Rutherford</dc:creator>
				<category><![CDATA[adaptability]]></category>
		<category><![CDATA[quality]]></category>

		<guid isPermaLink="false">http://silkandspinach.net/?p=1439</guid>
		<description><![CDATA[TL;DR Longer methods are more likely to need to change when the application changes. The Longer Story Shorter methods protect my investment in the code, in a number of ways. First: Testability. I expect there to be a correlation between method length (number of &#8220;statements&#8221;) and the number of (unit) test cases required to validate the method [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1439&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><strong>TL;DR</strong><br />
Longer methods are more likely to need to change when the application changes.</p>
<p><strong>The Longer Story</strong><br />
Shorter methods protect my investment in the code, in a number of ways.</p>
<p>First: Testability. I expect there to be a correlation between method length (number of &#8220;statements&#8221;) and the number of (unit) test cases required to validate the method completely. In fact, as method size increases I expect the number of test cases to increase faster than linearly, in general. Thus, breaking a large method into smaller independent methods means I can test parts of my application&#8217;s behaviour independently of each other, and probably with fewer tests. All of which means I will have to invest less in creating and running the tests for a group of small methods, compared to the investment required in a large method.</p>
<p>Second: Cohesion. I expect there to be a strong correlation between method length and the number of unknown future requirements demanding that method to change. Thus, the investment I make in developing and testing a small method will be repaid countless times, because I will have fewer reasons in the future to break it open and undo what I did. Smaller methods are more likely to &#8220;stabilize out&#8221; and drop out of the application&#8217;s overall churn.</p>
<p>Third: Coupling (DRY). I expect that longer methods are more likely to duplicate the knowledge or logic found elsewhere in the same codebase. Which means, again, that the effort I invest in getting this (long) method correct could be lost if ever any of those duplicated pieces of knowledge has to change.</p>
<p>And finally: Understandability. A method that does very little is likely to require fewer mental gymnastics to fully understand, compared to a longer method. I am less likely to need to invest significant amounts of time on each occasion I encounter it.</p>
<p>The Open-Closed Principle says that we should endeavour to design our applications such that new features can be added without the need to change existing code &#8212; because that state protects the investment we&#8217;ve made in that existing, working code, and removes the risk of breaking it if we were to open it up and change it. Maybe it&#8217;s also worth thinking of the OCP as applying equally to methods.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/silkandspinach.wordpress.com/1439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/silkandspinach.wordpress.com/1439/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1439&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=wT6lEtOsE38:I5lOKKx9fA0:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=wT6lEtOsE38:I5lOKKx9fA0:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=wT6lEtOsE38:I5lOKKx9fA0:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=wT6lEtOsE38:I5lOKKx9fA0:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=wT6lEtOsE38:I5lOKKx9fA0:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SilkAndSpinach/~4/wT6lEtOsE38" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://silkandspinach.net/2013/01/30/why-shorter-methods-are-better/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c39ead2d025bde14f3f39b20ff7d113?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">kevinrutherford</media:title>
		</media:content>
	<feedburner:origLink>http://silkandspinach.net/2013/01/30/why-shorter-methods-are-better/</feedburner:origLink></item>
		<item>
		<title>A testing strategy</title>
		<link>http://feedproxy.google.com/~r/SilkAndSpinach/~3/J7Md7WpgPBg/</link>
		<comments>http://silkandspinach.net/2013/01/18/a-testing-strategy/#comments</comments>
		<pubDate>Fri, 18 Jan 2013 20:51:45 +0000</pubDate>
		<dc:creator>Kevin Rutherford</dc:creator>
				<category><![CDATA[hexagonal architecture]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://silkandspinach.net/?p=1437</guid>
		<description><![CDATA[The blog post Cucumber and Full Stack Testing by @tooky sparked a very interesting Twitter conversation, during the course of which I realised I had fairly clear views on what tests to write for a web application. Assuming an intention to create (or at least work towards creating) a hexagonal architecture, here are the tests [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1437&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>The blog post <a href="http://tooky.co.uk/2013/01/18/cucumber-and-full-stack-testing.html">Cucumber and Full Stack Testing</a> by <a href="https://twitter.com/tooky">@tooky</a> sparked a very interesting <a href="https://twitter.com/tooky/status/292337337671761920">Twitter conversation</a>, during the course of which I realised I had fairly clear views on what tests to write for a web application. Assuming an intention to create (or at least work towards creating) a <a href="http://silkandspinach.net/category/hexagonal-architecture/">hexagonal architecture</a>, here are the tests I would ideally aim to have at some point:</p>
<ul>
<li><span style="line-height:13px;">A couple of end-to-end tests that hit the UI and the database, to prove that we have at least one configuration in which those parts join up. These only need to be run rarely, say on CI and maybe after each deployment.</span></li>
<li>An integration test for each adapter, proving that the adapter meets its contract with the domain objects AND that it works correctly with whatever external service it is adapting. This applies to the views-and-controllers pairings too, with the service objects in the middle hexagon stubbed or mocked as appropriate. These will each need to run when the adapters or the external services change, which should be infrequent once initial development of an adapter has settled out.</li>
<li>Unit tests for each object in the middle hexagon, in which commands issued to other objects are mocked (see <a href="https://twitter.com/sandimetz">@sandimetz</a>&#8216;s testing rules, for which I have no public link). And for every mocked interaction, a contract test proving that the mocked object really would respond as per the mocked interaction (see <a href="https://twitter.com/jbrains">@jbrains</a>&#8216;s <a href="http://blog.thecodewhisperer.com/2010/10/16/integrated-tests-are-a-scam/">Integrated tests are a scam</a> articles). These will be extremely fast, and will be run every few seconds during the TDD cycle.</li>
</ul>
<p>I&#8217;ve never yet reached this goal, but that&#8217;s what I&#8217;m striving for when I create tests. It seems perfectly adequate to me, given sufficient discipline around the creation of the contract tests. Have I missed anything? Would it give you confidence in your app?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/silkandspinach.wordpress.com/1437/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/silkandspinach.wordpress.com/1437/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1437&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=J7Md7WpgPBg:QiWpXfoNoNA:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=J7Md7WpgPBg:QiWpXfoNoNA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=J7Md7WpgPBg:QiWpXfoNoNA:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=J7Md7WpgPBg:QiWpXfoNoNA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=J7Md7WpgPBg:QiWpXfoNoNA:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SilkAndSpinach/~4/J7Md7WpgPBg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://silkandspinach.net/2013/01/18/a-testing-strategy/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c39ead2d025bde14f3f39b20ff7d113?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">kevinrutherford</media:title>
		</media:content>
	<feedburner:origLink>http://silkandspinach.net/2013/01/18/a-testing-strategy/</feedburner:origLink></item>
		<item>
		<title>I’m growing a mo</title>
		<link>http://feedproxy.google.com/~r/SilkAndSpinach/~3/oUPtNA5_Yfg/</link>
		<comments>http://silkandspinach.net/2012/11/04/im-growing-a-mo/#comments</comments>
		<pubDate>Sun, 04 Nov 2012 17:30:07 +0000</pubDate>
		<dc:creator>Kevin Rutherford</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://silkandspinach.net/?p=1435</guid>
		<description><![CDATA[Two years ago the fantastic staff at Macclesfield hospital saved my life. To cut a long story short, my colon burst due to diverticular disease; Mr Khan and Mr Hadjiloucas correctly diagnosed it and operated in the nick of time. (Seriously. Two hours later and I would have been gone.) I had two 6-hour operations [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1435&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Two years ago the fantastic staff at <a href="http://www.eastcheshire.nhs.uk/contact%20us/macclesfield-district-hospital.htm">Macclesfield hospital</a> saved my life. To cut a long story short, my colon burst due to diverticular disease; Mr Khan and Mr Hadjiloucas correctly diagnosed it and operated in the nick of time. (Seriously. Two hours later and I would have been gone.) I had two 6-hour operations 5 months apart, and received superb care from all the staff involved during that period.</p>
<p>So I feel it&#8217;s the least I can do to raise a little money to help the hospital. And that&#8217;s why, in an event that runs spookily parallel to Movember, I&#8217;m growing a moustache for money. Your money. Please help the hospital by giving a little at <a href="http://www.justgiving.com/Kevin-Rutherford1">my JustGiving page</a>, or by telling everyone you know and raising my mo&#8217;s profile. Thank you.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/silkandspinach.wordpress.com/1435/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/silkandspinach.wordpress.com/1435/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1435&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=oUPtNA5_Yfg:HjEKlO5L9Bo:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=oUPtNA5_Yfg:HjEKlO5L9Bo:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=oUPtNA5_Yfg:HjEKlO5L9Bo:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=oUPtNA5_Yfg:HjEKlO5L9Bo:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=oUPtNA5_Yfg:HjEKlO5L9Bo:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SilkAndSpinach/~4/oUPtNA5_Yfg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://silkandspinach.net/2012/11/04/im-growing-a-mo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c39ead2d025bde14f3f39b20ff7d113?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">kevinrutherford</media:title>
		</media:content>
	<feedburner:origLink>http://silkandspinach.net/2012/11/04/im-growing-a-mo/</feedburner:origLink></item>
		<item>
		<title>The problem with code smells</title>
		<link>http://feedproxy.google.com/~r/SilkAndSpinach/~3/udObp1JOG6c/</link>
		<comments>http://silkandspinach.net/2012/09/03/the-problem-with-code-smells/#comments</comments>
		<pubDate>Mon, 03 Sep 2012 12:41:34 +0000</pubDate>
		<dc:creator>Kevin Rutherford</dc:creator>
				<category><![CDATA[quality]]></category>

		<guid isPermaLink="false">http://silkandspinach.net/?p=1413</guid>
		<description><![CDATA[Like most developers I know, I have used code smells to describe problems in code since I first heard about them. The idea was introduced by Kent Beck in Fowler&#8217;s Refactoring back in 1999, and has taken root since then. The concept of code smells has several benefits, not least the fact that it gives [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1413&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Like most developers I know, I have used <a href="http://c2.com/cgi/wiki?CodeSmell">code smells</a> to describe problems in code since I first heard about them. The idea was introduced by Kent Beck in Fowler&#8217;s <a href="http://refactoring.com/">Refactoring</a> back in 1999, and has taken root since then. The concept of code smells has several benefits, not least the fact that it gives names to ideas that were previously only vague. Having a list of named code quality anti-patterns helps all of us discuss them on the same terms.</p>
<p>But while I was writing <a href="http://refactoringinruby.info/">Refactoring in Ruby</a> with <a href="https://twitter.com/wwake">@wwake</a>, and writing <a href="https://github.com/troessner/reek">Reek</a> at the same time, I began to feel a little uneasy about them. I was never able to put my finger on exactly why that was, or what I was uneasy about, but the feeling never went away. This year I think I have finally understood what I think about code smells, and why I think we can do somewhat better. So before reading on, take a moment to list the things you don&#8217;t like about them. Then let&#8217;s compare lists. Go ahead, I&#8217;ll wait.</p>
<p>Ok, done that? Here are my current reasons for wanting a different tool for describing code quality:</p>
<ul>
<li>The names aren&#8217;t all that helpful for people unfamiliar with the concepts. If you had never heard of them, what would you make of Feature Envy, Shotgun Surgery, Data Clump etc? Sure, the names are memorable, but that only helps with hindsight, for people who have taken the time and trouble to investigate and learn them.</li>
<li>Some code smells can overlap. For example, I&#8217;m often unsure whether I have seen Feature Envy or Inappropriate Intimacy, Divergent Change or Large Class. There&#8217;s a sense in which this doesn&#8217;t matter, of course; but it undermines their use as a communication tool.</li>
<li>Some of the smells can be subjective or contextual, leaving the quality of the code open to interpretation. For example, how large is a Large Class or a Long Parameter List?</li>
<li>Some of the smells apply only under certain circumstances. For example, a Switch Statement is perfectly fine at the system boundary when we are figuring out the type of an incoming message, but often bad news when it represents a type check on code we own. And it can be acceptable at the system boundary to grab the fields of a Data Class in order to display them, while elsewhere that might be seen as Feature Envy.</li>
<li>The list of code smells is not canonical; different people have added their own smells to Beck &amp; Fowler&#8217;s original list. This situation is even worse when it comes to smells in unit tests; try looking for a canonical list of test smells and you&#8217;ll find no consensus whatever. In my opinion, this fact alone completely undermines the idea that code smells form a pattern language for describing code quality.</li>
<li>There are no clear and obvious code smells covering some dynamic problems, such as the coupling between variables whose values depend on each other, or the problems introduced by mutable objects.</li>
</ul>
<p>Did you have the same list, or something similar?</p>
<p>So, what can we do about it? I think the answer lies with Connascence. This is an idea introduced by Meilir Page-Jones in two books in the 1990s, and later popularised by <a href="https://twitter.com/jimweirich">@jimweirich</a> in a series of <a href="http://vimeo.com/10837903">conference talks</a>. I&#8217;m not going to cover Connascence in detail here &#8212; you can find it all for yourself by reading <a href="https://twitter.com/jimweirich">@jimweirich</a>&#8216;s <a href="http://onestepback.org/articles/connascence/index.html">articles</a> or looking at <a href="http://www.slideshare.net/kevinrutherford/connascence">my summary slides</a>. I just wanted to take a moment to write down my current opinions about code smells. I&#8217;ll probably write in more detail about Connascence in the coming weeks, but for now what do you think?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/silkandspinach.wordpress.com/1413/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/silkandspinach.wordpress.com/1413/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1413&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=udObp1JOG6c:ujHxwmt2XUo:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=udObp1JOG6c:ujHxwmt2XUo:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=udObp1JOG6c:ujHxwmt2XUo:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=udObp1JOG6c:ujHxwmt2XUo:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=udObp1JOG6c:ujHxwmt2XUo:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SilkAndSpinach/~4/udObp1JOG6c" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://silkandspinach.net/2012/09/03/the-problem-with-code-smells/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c39ead2d025bde14f3f39b20ff7d113?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">kevinrutherford</media:title>
		</media:content>
	<feedburner:origLink>http://silkandspinach.net/2012/09/03/the-problem-with-code-smells/</feedburner:origLink></item>
		<item>
		<title>Hexagonal rails: Rake tasks are adapters</title>
		<link>http://feedproxy.google.com/~r/SilkAndSpinach/~3/VYSgK35fxjQ/</link>
		<comments>http://silkandspinach.net/2012/07/11/hexagonal-rails-rake-tasks-are-adapters/#comments</comments>
		<pubDate>Wed, 11 Jul 2012 19:53:19 +0000</pubDate>
		<dc:creator>Kevin Rutherford</dc:creator>
				<category><![CDATA[hexagonal architecture]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://silkandspinach.net/?p=1378</guid>
		<description><![CDATA[If I&#8217;m thinking about my Rails app in terms of a hexagonal architecture, I find it also pays to consider every rake task to be an Adapter. Thus: The true picture is a little more complicated than that, but the principal ideas are there. The rake task acts as a mediator, allowing me to send [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1378&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>If I&#8217;m thinking about my Rails app in terms of a <a href="http://alistair.cockburn.us/Hexagonal+architecture">hexagonal architecture</a>, I find it also pays to consider every rake task to be an Adapter. Thus:</p>
<div id="attachment_1388" class="wp-caption aligncenter" style="width: 310px"><a href="http://silkandspinach.files.wordpress.com/2012/07/hex-rake.jpg"><img class="size-medium wp-image-1388" title="hex-rake" src="http://silkandspinach.files.wordpress.com/2012/07/hex-rake.jpg?w=300&#038;h=249" alt="" width="300" height="249" /></a><p class="wp-caption-text">Picture: @rosieemackenzie</p></div>
<p>The true picture is a little more complicated than that, but the principal ideas are there. The rake task acts as a mediator, allowing me to send messages to (ie. call methods on) my application&#8217;s objects.</p>
<p>In general, we want our Adapters to be as thin as possible (and no thinner). That&#8217;s because the Adapter code inherently depends on some framework, and that fact will usually make its tests difficult and/or slow. We still need those tests, but we want to have as few paths through the Adapter as possible, and thus fewer tests of the Adapter, so that the total test complexity and test run-time are minimized.</p>
<p>As an example, suppose I have a rake task that validates the posts and comments on my blog:</p>
<p><script src="https://gist.github.com/3089723.js"></script></p>
<p>I want to maximize the unit tested percentage of the code executed during the task, so I move the code out of the rake task and into a new domain object. That new object &#8220;is&#8221; the task, and usually my rake tasks can then slim down to a single line of code.</p>
<p><script src="https://gist.github.com/3091149.js"></script></p>
<p>Stripping all of the code out of the rake task and moving it into a domain object is analogous to the approach currently being explored by <a href="https://twitter.com/mattwynne">@mattwynne</a> and <a href="https://twitter.com/tooky">@tooky</a> in their Rails <a href="https://twitter.com/tooky/status/221347422742388736">controller refactorings</a>. In my own code I call these task objects &#8220;use case objects&#8221;. Currently I keep them in a UseCases namespace within app/models, although I&#8217;m open to exploring alternative conventions. One of the nice side-effects of this is that I sometimes discover synergies between the work done by rake tasks and that done by controllers. By pulling Use Case objects out of both kinds of adapter I&#8217;m creating a convention for some of the code that turns out to be cleaner, more (quickly) testable, and which names things better.</p>
<p>Please let me know if you try this &#8212; or indeed if you&#8217;re already doing it.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/silkandspinach.wordpress.com/1378/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/silkandspinach.wordpress.com/1378/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1378&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=VYSgK35fxjQ:O3UUDbunwi4:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=VYSgK35fxjQ:O3UUDbunwi4:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=VYSgK35fxjQ:O3UUDbunwi4:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=VYSgK35fxjQ:O3UUDbunwi4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=VYSgK35fxjQ:O3UUDbunwi4:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SilkAndSpinach/~4/VYSgK35fxjQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://silkandspinach.net/2012/07/11/hexagonal-rails-rake-tasks-are-adapters/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c39ead2d025bde14f3f39b20ff7d113?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">kevinrutherford</media:title>
		</media:content>

		<media:content url="http://silkandspinach.files.wordpress.com/2012/07/hex-rake.jpg?w=300" medium="image">
			<media:title type="html">hex-rake</media:title>
		</media:content>
	<feedburner:origLink>http://silkandspinach.net/2012/07/11/hexagonal-rails-rake-tasks-are-adapters/</feedburner:origLink></item>
		<item>
		<title>Hexagonal rails: Hiding the finders</title>
		<link>http://feedproxy.google.com/~r/SilkAndSpinach/~3/cWLJhDho0Ug/</link>
		<comments>http://silkandspinach.net/2012/07/06/hexagonal-rails-hiding-the-finders/#comments</comments>
		<pubDate>Fri, 06 Jul 2012 18:30:56 +0000</pubDate>
		<dc:creator>Kevin Rutherford</dc:creator>
				<category><![CDATA[hexagonal architecture]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://silkandspinach.net/2012/07/06/hexagonal-rails-hiding-the-finders/</guid>
		<description><![CDATA[This is a brief follow-up to the Hexagonal Rails sessions I did last week at the Scottish Ruby Conference with Matt and Steve. We tried to cram a 3-day course into 45 minutes, with inevitable consequences. So by way of an apology, here&#8217;s another brief foray into some of the same territory&#8230; Today I&#8217;ve been [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1364&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This is a brief follow-up to the <a href="http://programme.scottishrubyconference.com/schedule#proposal_105">Hexagonal Rails</a> sessions I did last week at the Scottish Ruby Conference with <a href="https://twitter.com/#!/mattwynne">Matt</a> and <a href="https://twitter.com/#!/tooky">Steve</a>. We tried to cram a 3-day course into 45 minutes, with inevitable consequences. So by way of an apology, here&#8217;s another brief foray into some of the same territory&#8230;</p>
<p>Today I&#8217;ve been exploring ways of improving the unit tests in one of my Rails apps. Let&#8217;s pretend it&#8217;s a blog app, and I have to add a feature to publish posts. I&#8217;ve pulled some code out of a controller to make a UseCase object:</p>
<p><script src="https://gist.github.com/3060597.js"></script></p>
<p>(Here ui is a controller, and makes decisions about routing and rendering in response to the events sent by the use case object.)</p>
<p>I&#8217;m really sick and tired of looking at code like this. It seems that most of the classes in any Rails app know that finders return nil when they can&#8217;t find something, and so they all have to cope with that case. I want to get rid of that if, or at least hide it away in a single place so that it doesn&#8217;t contaminate the rest of the app. I want the test to be a bit simpler and more readable too. And I want the option to have richer finders, maybe coping with fuzzy edge cases and still finding the desired database record. In short, I want to wrap the finder in an Adapter.</p>
<p>So I experimented with a couple of alternatives to the above. First, a variant on Smalltalk&#8217;s ifTrue:ifFalse message argument style:</p>
<p><script src="https://gist.github.com/3060924.js"></script></p>
<p>I don&#8217;t like this because it&#8217;s near impossible to isolate and test the interactions. For example, <strong>Blog.should_receive(:lookup_post).with(</strong> &#8230; what?</p>
<p>Another approach might be something like this:</p>
<p><script src="https://gist.github.com/3060969.js"></script></p>
<p>Indeed Rails itself uses this style in its controllers, and so does the <a href="http://rubygems.org/gems/inherited_resources">inherited_resources</a> gem. But it suffers from the same testing problems as my previous attempt. So I finally settled on this:</p>
<p><script src="https://gist.github.com/3062296.js"></script></p>
<p>This version uses an explicit listener object, which I can thus instantiate and test independently of the calling context. (In real code I might make it an inner class too.) And I&#8217;ve already found a couple more uses for the lookup_post method, removing dependencies and ifs along the way.</p>
<p>I find this approach highly readable and testable, nicely separating different responsibilities and allowing me to give them names. But that doesn&#8217;t mean I&#8217;ll stop exploring. Have you tried other approaches? How did they work out?</p>
<p><span style="text-decoration:underline;">Update</span></p>
<p><a href="https://twitter.com/lukeredpath">Luke</a> points out that I forgot to include the option I actually use the most, the <a href="http://c2.com/cgi/wiki?SelfShuntPattern">Self Shunt</a>:</p>
<p><script src="https://gist.github.com/3065695.js"></script></p>
<p>This is the same as the listener option above, but the UseCase object passes itself as the listener, thus saving a class and in my opinion) improving readability a little. Can&#8217;t think why I forgot it, cos it&#8217;s the best and neatest of them all. Anyroadup, thanks Luke!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/silkandspinach.wordpress.com/1364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/silkandspinach.wordpress.com/1364/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1364&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=cWLJhDho0Ug:oyAYCDjSn14:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=cWLJhDho0Ug:oyAYCDjSn14:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=cWLJhDho0Ug:oyAYCDjSn14:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=cWLJhDho0Ug:oyAYCDjSn14:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=cWLJhDho0Ug:oyAYCDjSn14:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SilkAndSpinach/~4/cWLJhDho0Ug" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://silkandspinach.net/2012/07/06/hexagonal-rails-hiding-the-finders/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c39ead2d025bde14f3f39b20ff7d113?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">kevinrutherford</media:title>
		</media:content>
	<feedburner:origLink>http://silkandspinach.net/2012/07/06/hexagonal-rails-hiding-the-finders/</feedburner:origLink></item>
		<item>
		<title>The wrong duplication (reprise)</title>
		<link>http://feedproxy.google.com/~r/SilkAndSpinach/~3/KMU9UDh6b50/</link>
		<comments>http://silkandspinach.net/2011/12/23/the-wrong-duplication-reprise/#comments</comments>
		<pubDate>Fri, 23 Dec 2011 14:56:23 +0000</pubDate>
		<dc:creator>Kevin Rutherford</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://silkandspinach.wordpress.com/?p=1337</guid>
		<description><![CDATA[I came across this old post again today, and I still like what it has to say: The Wrong Duplication. Maybe I should add a code example&#8230;?<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1337&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I came across this old post again today, and I still like what it has to say: <a href="http://silkandspinach.net/2006/11/05/the-wrong-duplication/">The Wrong Duplication</a>. Maybe I should add a code example&#8230;?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/silkandspinach.wordpress.com/1337/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/silkandspinach.wordpress.com/1337/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1337&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=KMU9UDh6b50:zngFbUnm3k4:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=KMU9UDh6b50:zngFbUnm3k4:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=KMU9UDh6b50:zngFbUnm3k4:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=KMU9UDh6b50:zngFbUnm3k4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=KMU9UDh6b50:zngFbUnm3k4:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SilkAndSpinach/~4/KMU9UDh6b50" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://silkandspinach.net/2011/12/23/the-wrong-duplication-reprise/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c39ead2d025bde14f3f39b20ff7d113?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">kevinrutherford</media:title>
		</media:content>
	<feedburner:origLink>http://silkandspinach.net/2011/12/23/the-wrong-duplication-reprise/</feedburner:origLink></item>
		<item>
		<title>Global Day of Coderetreat</title>
		<link>http://feedproxy.google.com/~r/SilkAndSpinach/~3/wTytxvHj2zY/</link>
		<comments>http://silkandspinach.net/2011/11/16/global-day-of-coderetreat/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 16:43:45 +0000</pubDate>
		<dc:creator>Kevin Rutherford</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://silkandspinach.net/?p=1335</guid>
		<description><![CDATA[In case you hadn&#8217;t noticed, XP-Manchester is running a Coderetreat as part of the Global Day of Coderetreat on December 3rd 2011. You can find out everything you need to know by visiting our page on Eventbrite, where you can find FAQs, links to further information, and the all-important sign-me-up button. Oh, and it&#8217;s FREE [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1335&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In case you hadn&#8217;t noticed, XP-Manchester is running a <a href="http://coderetreat.com/">Coderetreat</a> as part of the <a href="http://coderetreat.com/global_day.html">Global Day of Coderetreat on December 3rd 2011</a>.</p>
<p>You can find out everything you need to know by visiting <a href="http://coderetreatmanchester.eventbrite.co.uk/">our page on Eventbrite</a>, where you can find FAQs, links to further information, and the all-important sign-me-up button. Oh, and it&#8217;s FREE to attend.</p>
<p>Places are limited, and tickets are going fast now, so make sure you book your place soon!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/silkandspinach.wordpress.com/1335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/silkandspinach.wordpress.com/1335/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1335&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=wTytxvHj2zY:UcIyxeZ3Wt8:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=wTytxvHj2zY:UcIyxeZ3Wt8:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=wTytxvHj2zY:UcIyxeZ3Wt8:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=wTytxvHj2zY:UcIyxeZ3Wt8:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=wTytxvHj2zY:UcIyxeZ3Wt8:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SilkAndSpinach/~4/wTytxvHj2zY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://silkandspinach.net/2011/11/16/global-day-of-coderetreat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c39ead2d025bde14f3f39b20ff7d113?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">kevinrutherford</media:title>
		</media:content>
	<feedburner:origLink>http://silkandspinach.net/2011/11/16/global-day-of-coderetreat/</feedburner:origLink></item>
		<item>
		<title>Ubuntu, mysql and rails</title>
		<link>http://feedproxy.google.com/~r/SilkAndSpinach/~3/y3pC5J5tbJE/</link>
		<comments>http://silkandspinach.net/2011/11/10/ubuntu-mysql-and-rails/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 16:16:06 +0000</pubDate>
		<dc:creator>Kevin Rutherford</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://silkandspinach.net/?p=1326</guid>
		<description><![CDATA[I just upgraded my work laptop to Ubuntu Natty Narwhal, and then I found all my rails projects broken. Whenever I tried to run up a rails server, I got this: $ rails server =&#62; Booting WEBrick =&#62; Rails 3.0.3 application starting in development on http://0.0.0.0:3000 =&#62; Call with -d to detach Exiting /path/to/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:30: in [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1326&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I just upgraded my work laptop to Ubuntu Natty Narwhal, and then I found all my rails projects broken. Whenever I tried to run up a rails server, I got this:</p>
<pre>$ rails server
=&gt; Booting WEBrick
=&gt; Rails 3.0.3 application starting in development on http://0.0.0.0:3000
=&gt; Call with -d to detach

Exiting
/path/to/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:30:
in `mysql_connection': undefined method `init' for Mysql:Class (NoMethodError)</pre>
<p>(followed by a huge stacktrace). Googling didn&#8217;t help much, because every time anyone has asked about this on a forum, all of the answers are specific to Macs.</p>
<p>It turns out that the problem is that the mysql gem includes native code, which is built when you install the gem. And my existing mysql gem had been built against mysql libraries that are now, since my upgrade, out of date. So the fix turns out to be maddeningly simple. First, get rid of the gem that doesn&#8217;t work:</p>
<pre>$ gem uninstall mysql</pre>
<p>Next, get hold of the developer API libraries for mysql:</p>
<pre>$ sudo apt-get install libmysqlclient16-dev</pre>
<p>And finally, download and rebuild the gem again:</p>
<pre>$ gem install mysql</pre>
<p>Simples. I just hope this quick note saves you the hours of trawling through forums that I had to endure this morning.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/silkandspinach.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/silkandspinach.wordpress.com/1326/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1326&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=y3pC5J5tbJE:5xwbnDGXNLQ:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=y3pC5J5tbJE:5xwbnDGXNLQ:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=y3pC5J5tbJE:5xwbnDGXNLQ:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=y3pC5J5tbJE:5xwbnDGXNLQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=y3pC5J5tbJE:5xwbnDGXNLQ:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SilkAndSpinach/~4/y3pC5J5tbJE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://silkandspinach.net/2011/11/10/ubuntu-mysql-and-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c39ead2d025bde14f3f39b20ff7d113?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">kevinrutherford</media:title>
		</media:content>
	<feedburner:origLink>http://silkandspinach.net/2011/11/10/ubuntu-mysql-and-rails/</feedburner:origLink></item>
		<item>
		<title>Simple Design with Design Patterns</title>
		<link>http://feedproxy.google.com/~r/SilkAndSpinach/~3/c0wvxHMbAIg/</link>
		<comments>http://silkandspinach.net/2011/10/28/1315/#comments</comments>
		<pubDate>Fri, 28 Oct 2011 08:01:42 +0000</pubDate>
		<dc:creator>Kevin Rutherford</dc:creator>
				<category><![CDATA[design]]></category>
		<category><![CDATA[pattern]]></category>

		<guid isPermaLink="false">http://silkandspinach.net/?p=1315</guid>
		<description><![CDATA[In the blog post Simple Design with Design Patterns, Cory Foy runs a thought experiment in which he explores the use of patterns in solving the Game of Life kata. Cory&#8217;s starting point is the intention behind the GoF patterns, coupled with the XP rules of Simple Design and Uncle Bob&#8217;s SOLID principles. Cory stops his analysis [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1315&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In the blog post <a href="http://blog.coryfoy.com/2011/10/simple-design-with-design-patterns/">Simple Design with Design Patterns</a>, Cory Foy runs a thought experiment in which he explores the use of patterns in solving the <a href="http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">Game of Life</a> kata. Cory&#8217;s starting point is the <em>intention</em> behind the <a href="http://en.wikipedia.org/wiki/Design_Patterns">GoF</a> patterns, coupled with the XP rules of <a href="http://c2.com/cgi/wiki?XpSimplicityRules">Simple Design</a> and Uncle Bob&#8217;s <a href="http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod">SOLID</a> principles.</p>
<p>Cory stops his analysis when he has the following model:</p>
<pre>|Cell| &lt;------- |Status|
           |Alive|    |Dead|</pre>
<p>At this point Cory says</p>
<blockquote><p>&#8220;Certainly if we implemented a Cell with a Status object with an AliveStatus class and a DeadStatus class someone, somewhere, should slap us upside the head.&#8221;</p></blockquote>
<p>However, I&#8217;d like to carry on from that point just a little further&#8230;</p>
<p>I like to teach the use of CRC for exploring design, and so I naturally looked at Cory&#8217;s objects from the point of view of their behaviours and responsibilities. Thus it struck me that if we <em>don&#8217;t</em> make the split suggested by Cory, we&#8217;d have a Cell that knows about its position/neighbours <strong>and</strong> knows what change to make when the clock ticks. That would violate the Single Responsibility Principle. So the above split into Cell and Status makes sense from the point of view of responsibilities, if we give the Status object the job of deciding how the Cell should change.</p>
<p>With those responsibilities in mind, a little mental CRC session suggests that, when the clock ticks, the Cell would tell its Status object how many neighbours it had, and ask for a new Status object in return.</p>
<pre class="brush: ruby; title: ; notranslate">
class Cell
  def change
    @status = @status.update_based_on(@number_of_neighbours)
  end
end</pre>
<p>The Status objects become Strategy/Policy objects, and thus don&#8217;t seem to need any instance state. All of which means that the Status objects could be <a href="http://c2.com/cgi/wiki?FlyweightPattern">Flyweights</a>, with only one extant instance of each type, referenced by every Cell that happens to be in the corresponding state.</p>
<p>I think this approach could well be worth a punt during the upcoming <a href="http://coderetreat.ning.com/events/event/listByDate?date=2011-12-03">Global Day of Coderetreats</a>&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/silkandspinach.wordpress.com/1315/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/silkandspinach.wordpress.com/1315/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1315&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=c0wvxHMbAIg:S1SKPRR3FcY:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=c0wvxHMbAIg:S1SKPRR3FcY:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=c0wvxHMbAIg:S1SKPRR3FcY:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=c0wvxHMbAIg:S1SKPRR3FcY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=c0wvxHMbAIg:S1SKPRR3FcY:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SilkAndSpinach/~4/c0wvxHMbAIg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://silkandspinach.net/2011/10/28/1315/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c39ead2d025bde14f3f39b20ff7d113?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">kevinrutherford</media:title>
		</media:content>
	<feedburner:origLink>http://silkandspinach.net/2011/10/28/1315/</feedburner:origLink></item>
		<item>
		<title>Don’t inherit code</title>
		<link>http://feedproxy.google.com/~r/SilkAndSpinach/~3/Sz_r-s4NhJg/</link>
		<comments>http://silkandspinach.net/2011/09/28/dont-inherit-code/#comments</comments>
		<pubDate>Wed, 28 Sep 2011 07:01:16 +0000</pubDate>
		<dc:creator>Kevin Rutherford</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://silkandspinach.net/?p=1309</guid>
		<description><![CDATA[I learned a long time ago that there are essentially three different kinds of relationship between two classes. They are, in order of increasing tightness: Uses, in which I care only about your public API; Creates, in which I also need to know your class; Inherits/extends, in which I can also see some of your [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1309&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I learned a long time ago that there are essentially three different kinds of relationship between two classes. They are, in order of increasing tightness:</p>
<ol>
<li><strong>Uses</strong>, in which I care only about your public API;</li>
<li><strong>Creates</strong>, in which I also need to know your class;</li>
<li><strong>Inherits/extends</strong>, in which I can also see some of your workings and I become part of you.</li>
</ol>
<p>Don&#8217;t do that last one. Ever.</p>
<p>Many of the GoF patterns help move code up this list, from tight towards loose coupling. Except <strong>TemplateMethod</strong>, which not only encourages inheritance, it also creates a circular dependency by having the superclass only &#8220;work&#8221; in the presence of extensions. Bad.</p>
<p>Don&#8217;t inherit code. Bad.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/silkandspinach.wordpress.com/1309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/silkandspinach.wordpress.com/1309/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1309&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=Sz_r-s4NhJg:wIgU5bzj03k:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=Sz_r-s4NhJg:wIgU5bzj03k:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=Sz_r-s4NhJg:wIgU5bzj03k:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=Sz_r-s4NhJg:wIgU5bzj03k:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=Sz_r-s4NhJg:wIgU5bzj03k:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SilkAndSpinach/~4/Sz_r-s4NhJg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://silkandspinach.net/2011/09/28/dont-inherit-code/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c39ead2d025bde14f3f39b20ff7d113?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">kevinrutherford</media:title>
		</media:content>
	<feedburner:origLink>http://silkandspinach.net/2011/09/28/dont-inherit-code/</feedburner:origLink></item>
		<item>
		<title>Conditionals on the edge</title>
		<link>http://feedproxy.google.com/~r/SilkAndSpinach/~3/F5PFxmylERA/</link>
		<comments>http://silkandspinach.net/2011/09/19/conditionals-on-the-edge/#comments</comments>
		<pubDate>Mon, 19 Sep 2011 20:33:03 +0000</pubDate>
		<dc:creator>Kevin Rutherford</dc:creator>
				<category><![CDATA[duplication]]></category>
		<category><![CDATA[hexagonal architecture]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://silkandspinach.net/?p=1283</guid>
		<description><![CDATA[As you know, I have a thing about conditional code. Most conditionals are duplicates, and the only &#8220;genuine&#8221; conditionals are at the system boundaries, where they test external state and input information. But I discovered recently that, even at the edges, not every conditional is necessary&#8230; Here&#8217;s (a drastically simplified version of) some code I [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1283&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>As you know, I have a thing about <a title="the anti-if campaign" href="http://silkandspinach.net/2008/10/23/the-anti-if-campaign/">conditional code</a>. <a title="if…" href="http://silkandspinach.net/2004/07/16/if/">Most conditionals are duplicates</a>, and the only &#8220;genuine&#8221; conditionals are at the <a title="hexagonal soup" href="http://silkandspinach.net/2004/07/16/hexagonal-soup/">system boundaries</a>, where they test external state and input information. But I discovered recently that, even at the edges, not every conditional is necessary&#8230;</p>
<p>Here&#8217;s (a drastically simplified version of) some code I wrote a while back. (It&#8217;s from a Rails app, but the message in this post applies to any technology and any kind of system input.)</p>
<pre class="brush: ruby; title: ; notranslate">
class Users::RegistrationsController &lt; Devise::RegistrationsController
  def create
    user = create_new_user_account
    if params[:invitation_token]
      sign_in_as(user)
      redirect_to Invitation.find(params[:invitation_token]).shared_content
    else
      redirect_to new_user_session_path
    end
  end
end</pre>
<p>This route handles incoming user registrations, and it has a conditional branch to cater for the case in which the new user was invited, via email, by an existing user. If the new user arrived by clicking a link in an email we do one set of things; whereas we do a different set of things if the user got here via the <em>&#8216;Register now&#8217;</em> button on the home page. Perfectly reasonable, I thought. Sure, it&#8217;s conditional code; but it&#8217;s sitting on the system boundary and therefore it&#8217;s fine, yes? No.</p>
<p><strong>This conditional check represents duplication</strong>. Somewhere else in my application already knew, at some time in the recent past, which route should be taken through this method when it was finally invoked. When the app sent that email invitation, it was in a sense delegating to the recipient the task of supplying user account details. The conditional branch that handles the invitation is a callback from across a technology boundary, and the inviting code already knew to expect it.</p>
<p>The duplication also has a nasty side-effect: high complexity. This method is an order of magnitude harder to understand and to test(-drive) because of the conditional construct. (And you&#8217;re reading a version from which I&#8217;ve elided all manner of error handling.) Oh, and by forcing two routes into the same method I also made it violate the Single Responsibility Principle.</p>
<p>Happily the duplication is really easy to remove. The code that sends the email invitation knows which path it wants the registration code to take, so I just need to create a new input route for &#8220;invitation acceptance&#8221; and change the email invitation to link to it. As a result I&#8217;ll have two controller methods, each with simpler logic than their predecessor:</p>
<pre class="brush: ruby; title: ; notranslate">
class Users::RegistrationsController &lt; Devise::RegistrationsController
  def create
    user = create_new_user_account
    redirect_to new_user_session_path
  end
end</pre>
<p>&nbsp;</p>
<pre class="brush: ruby; title: ; notranslate">
class InvitationAcceptancesController &lt; Devise::RegistrationsController
  def create
    user = create_new_user_account
    sign_in_as(user)
    redirect_to Invitation.find(params[:invitation_token]).shared_content
    end
  end
end
</pre>
<p>So I have now removed the duplication, and the controller code is simpler as a result. These controller methods are also much easier to test(-drive) than the earlier code. And as a final bonus, I can design the user interface to this route with more knowledge, maybe adding some details of the invitation so that the incoming potential user feels a little more at home. In short, now that each code path has its own method (and class) I can pay it more attention, and it will flourish as a result.</p>
<p>Now, having refactored this code, I take away a few points for the future:</p>
<ul>
<li>Where appropriate and possible, model system outputs as delegations into a space that your code can&#8217;t reach or control; and model the resulting future input as a response.</li>
<li>Where a system input is due to the system having earlier delegated a responsibility across its boundary, make sure the return route is differentiated from all others.</li>
<li>Differentiated incoming routes have code that is easier to read and simpler to test.</li>
<li>When an input route has a single use, the user experience can be tailored and enriched.</li>
<li>Not every conditional at the system boundary tests external state.</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/silkandspinach.wordpress.com/1283/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/silkandspinach.wordpress.com/1283/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1283&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=F5PFxmylERA:HgwYvH95Ugs:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=F5PFxmylERA:HgwYvH95Ugs:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=F5PFxmylERA:HgwYvH95Ugs:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=F5PFxmylERA:HgwYvH95Ugs:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=F5PFxmylERA:HgwYvH95Ugs:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SilkAndSpinach/~4/F5PFxmylERA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://silkandspinach.net/2011/09/19/conditionals-on-the-edge/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c39ead2d025bde14f3f39b20ff7d113?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">kevinrutherford</media:title>
		</media:content>
	<feedburner:origLink>http://silkandspinach.net/2011/09/19/conditionals-on-the-edge/</feedburner:origLink></item>
		<item>
		<title>Why I don’t use spork</title>
		<link>http://feedproxy.google.com/~r/SilkAndSpinach/~3/Xjf8ndHP38Q/</link>
		<comments>http://silkandspinach.net/2011/08/08/why-i-dont-use-spork/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 20:31:02 +0000</pubDate>
		<dc:creator>Kevin Rutherford</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://silkandspinach.net/?p=1280</guid>
		<description><![CDATA[Spork is great. And so is guard and its family of plugins. Early this year I spent a while converting all of my rails projects to use spork, and we even had a team standard tmux setup that ran spork in one of the start-up screens. So every time we saved a file, guard/spork ran [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1280&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="https://rubygems.org/gems/spork">Spork</a> is great. And so is <a href="https://rubygems.org/gems/guard">guard</a> and its family of plugins. Early this year I spent a while converting all of my rails projects to use spork, and we even had a team standard tmux setup that ran spork in one of the start-up screens. So every time we saved a file, guard/spork ran some of our specs. We even had growl/notify pop up a little message telling us the result, so we didn&#8217;t have to go to the trouble of switching screen to find out. How very efficient!</p>
<p>But now I&#8217;ve turned spork off, and here&#8217;s why: <strong>Spork solves the wrong problem.</strong></p>
<p>We enabled guard/spork because our specs were slow, and during the few months we had spork, those specs became even slower. <em>But we never noticed. </em>Most of the time guard/spork guessed correctly which specs to run, and we became quite good at configuring guard to re-load Rails when critical files changed. So we were reasonably comfortable, and our specs ran quickly most of the time. <em>But our design was getting worse every day.</em></p>
<p><em></em>Slow specs are a sign of too much coupling, and in the case of a Rails app that usually means not enough separation between the <a href="http://silkandspinach.net/2005/03/22/the-middle-hexagon-should-be-independent-of-the-adapters/">domain objects and the adapters</a>. So we&#8217;ve stopped running spork, so that we can feel the pain of the 15-second Rails load time, and so that we can feel the pain of all that coupling. And slowly we are making the app&#8217;s specs faster. Many spec files now run in under a hundredth of a second, and more will follow as we gradually peel domain code out from the Rails infrastructure. We don&#8217;t need to test the Rails components, and we have a very good suite of cucumber features that act as end-to-end integration tests. So our specs should be fast, and only need to tell us that our own objects each do their thing. That&#8217;s what we are now working towards, and that&#8217;s the pot of gold that spork was hiding from us.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/silkandspinach.wordpress.com/1280/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/silkandspinach.wordpress.com/1280/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1280&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=Xjf8ndHP38Q:sThB12QHcno:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=Xjf8ndHP38Q:sThB12QHcno:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=Xjf8ndHP38Q:sThB12QHcno:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=Xjf8ndHP38Q:sThB12QHcno:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=Xjf8ndHP38Q:sThB12QHcno:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SilkAndSpinach/~4/Xjf8ndHP38Q" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://silkandspinach.net/2011/08/08/why-i-dont-use-spork/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c39ead2d025bde14f3f39b20ff7d113?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">kevinrutherford</media:title>
		</media:content>
	<feedburner:origLink>http://silkandspinach.net/2011/08/08/why-i-dont-use-spork/</feedburner:origLink></item>
		<item>
		<title>Faster Rails controller specs</title>
		<link>http://feedproxy.google.com/~r/SilkAndSpinach/~3/dT7N5gT1bGQ/</link>
		<comments>http://silkandspinach.net/2011/08/07/faster-rails-controller-specs/#comments</comments>
		<pubDate>Sun, 07 Aug 2011 20:03:30 +0000</pubDate>
		<dc:creator>Kevin Rutherford</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://silkandspinach.net/?p=1258</guid>
		<description><![CDATA[One of the Rails apps I work on has this: $ rspec spec #... Finished in 61.82 seconds 475 examples, 0 failures 61 seconds!  (And on top of that I have to wait another 15 seconds for Rails load; that&#8217;s a whole other story, and I hope to come back to that in a future [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1258&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>One of the Rails apps I work on has this:</p>
<pre>$ rspec spec
#...
Finished in 61.82 seconds
475 examples, 0 failures</pre>
<p>61 seconds!  (And on top of that I have to wait another 15 seconds for Rails load; that&#8217;s a whole other story, and I hope to come back to that in a future post.) A quick dig into the 61 seconds reveals this:</p>
<pre>$ rspec spec/controllers
#...
Finished in 22.82 seconds
114 examples, 0 failures</pre>
<p>It happens that almost every controller action in our app needs the user to be logged in, so every controller spec needs that too. We&#8217;re using <a href="https://rubygems.org/gems/devise">Devise</a>, so every spec has something equivalent to this:</p>
<pre>before :each do
  sign_in Factory(:user)
end</pre>
<p>This implies hits on the database to create the user object, record the IP address and last sign-in time, etc etc. We can do much better by using a <code>mock_model</code> for the User, and stubbing out <a href="https://rubygems.org/gems/warden">Warden</a>&#8216;s authentication completely.</p>
<p>First, in the interests of DRYness, I made some helper methods to do some of the jobs that <a href="https://rubygems.org/gems/factory_girl">factory_girl</a> does:</p>
<pre>module RandomHelpers
  def random_number() SecureRandom.random_number(10000); end
  def random_id() random_number; end
  def random_name() SecureRandom.hex(20); end
end</pre>
<p>Next, a method that creates a <a href="http://martinfowler.com/articles/mocksArentStubs.html">test double</a> representing the User who will be logged in:</p>
<pre>module ControllerHelpers
  def user_double(attrs = {})
    user_attrs = {
      :first_name =&gt; random_name,
      :last_name =&gt; random_name,
      :authenticatable_salt =&gt; 'x'
    }.merge(attrs)
    mock_model(User, user_attrs)
  end
end</pre>
<p>This is the place to fill the User double with default attribute values. Our app requires every User to have a first and last name, so I&#8217;ve stubbed these with random strings; they can be overridden by the caller if specific values are needed for any test. In your own app, change this method to set any mandatory attributes your user model needs. Note also the stubbed <code>authenticatable_salt()</code> method, which wasn&#8217;t required when mocking earlier versions of Devise.</p>
<p>I also need a method that logs a user in:</p>
<pre>module ControllerHelpers
  def stub_sign_in_with(user)
    request.env['warden'] = double(Warden,
                                   :authenticate =&gt; user,
                                   :authenticate! =&gt; user,
                                   :authenticate? =&gt; true)
    sign_in(user)
    return user
  end
end</pre>
<p>This replaces Warden with a test double, and Devise is none the wiser.<br />
I also create a method to glue these together for the simplest (and most common) case:</p>
<pre>module ControllerHelpers
  def stub_sign_in(attrs = {})
    stub_sign_in_with user_double(attrs)
  end
end</pre>
<p>(Note that this returns the User double; you&#8217;ll see why that&#8217;s useful in a moment.)</p>
<p>Finally, I configure the specs so they have access to these helper methods. To achieve that, <code>spec/spec_helper.rb</code> needs to look something like this:</p>
<pre>Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.include RandomHelpers
  config.include Devise::TestHelpers, :type =&gt; :controller
  config.include ControllerHelpers, :type =&gt; :controller
end</pre>
<p>Putting all this together allows me to write controller specs with:</p>
<pre>before do
  stub_sign_in
end</pre>
<p>or, if I need access to the user double:</p>
<pre>let(:user) { stub_sign_in }</pre>
<p>And the payoff?</p>
<pre>$ rspec spec/controllers
#...
Finished in 7.36 seconds
114 examples, 0 failures</pre>
<p>I saved as whole 15 seconds! There&#8217;s still some work to do in my specs, but stubbing out Warden has made a massive difference to every test run that involves this project&#8217;s controller microtests.</p>
<p>Note that the above has been tested only with the following gem versions:</p>
<ul>
<li>rails 3.0.3</li>
<li>devise 1.3.4</li>
<li>warden 1.0.4</li>
<li>rspec-core 2.6.4</li>
<li>rspec-mocks 2.6.0</li>
<li>rspec-rails 2.6.1</li>
<li>factory_girl 1.3.3</li>
</ul>
<p>A condensed gist for the above code is <a href="https://gist.github.com/1130290">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/silkandspinach.wordpress.com/1258/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/silkandspinach.wordpress.com/1258/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=silkandspinach.net&#038;blog=1064186&#038;post=1258&#038;subd=silkandspinach&#038;ref=&#038;feed=1" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=dT7N5gT1bGQ:-RMPvkuCdek:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=dT7N5gT1bGQ:-RMPvkuCdek:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=dT7N5gT1bGQ:-RMPvkuCdek:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SilkAndSpinach?a=dT7N5gT1bGQ:-RMPvkuCdek:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SilkAndSpinach?i=dT7N5gT1bGQ:-RMPvkuCdek:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SilkAndSpinach/~4/dT7N5gT1bGQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://silkandspinach.net/2011/08/07/faster-rails-controller-specs/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c39ead2d025bde14f3f39b20ff7d113?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">kevinrutherford</media:title>
		</media:content>
	<feedburner:origLink>http://silkandspinach.net/2011/08/07/faster-rails-controller-specs/</feedburner:origLink></item>
	</channel>
</rss>
