<?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>Justin Leitgeb &#187; rake</title>
	<atom:link href="http://justinleitgeb.com/tag/rake/feed/" rel="self" type="application/rss+xml" />
	<link>http://justinleitgeb.com</link>
	<description>Perspectives on Ruby and OOP from a developer in NY</description>
	<lastBuildDate>Sun, 05 Sep 2010 04:43:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Rails tip: keep rake tasks short</title>
		<link>http://justinleitgeb.com/2009/09/rails-tip-keep-rake-tasks-short/</link>
		<comments>http://justinleitgeb.com/2009/09/rails-tip-keep-rake-tasks-short/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 12:00:49 +0000</pubDate>
		<dc:creator>Justin Leitgeb</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[rake]]></category>

		<guid isPermaLink="false">http://justinleitgeb.com/?p=51</guid>
		<description><![CDATA[Writing short Rake tasks that operate on objects in your codebase rather than defining long task bodies with supporting procedural methods often requires a shift in thinking about your Rake tasks and how it relates to the objects in your codebase.  The effort is worthwhile, however, as you add new classes and more precisely define the interfaces of existing classes in your system in order to achieve a more maintainable system.]]></description>
			<content:encoded><![CDATA[<p><a href="http://rake.rubyforge.org/" target="_blank">Rake (Ruby Make)</a> is often used in Rails projects for tasks that don&#8217;t need to be invoked through a web interface.  Examples of when it may come in handy include running data import tasks manually by administrators or automating tasks through cron.  Since it is so easy to start adding piles of code to a rake task to get a particular chore accomplished, two things often happen which decrease the quality of a project&#8217;s code: 1) tests are left by the wayside and 2) opportunities to expand the flexibility of classes and the project code base itself are lost as code is added to the body of the rake task.</p>
<p>In other words, software engineering best-practices often go out the window as Rake tasks are built, leading to missed opportunities for software reusability and increased maintenance costs.  In my experience, it&#8217;s only a bit more effort to turn the effort of writing a one-off Rake task into an opportunity to expand on the reusable components already in your system.</p>
<p>When I&#8217;ve coded maintenance scripts in Rake, two of the signs that a task needs refactoring are length of the task itself and the number methods that have to be defined in the namespace to support the method.  A task, just like any other method, should do just one thing.  If the Rake task is so long that it scrolls off the screen, opportunities for error are high and the next person working on the code is much more likely to start from scratch rather than sort things out in the method body.</p>
<p>The same applies to Rake tasks that require the presence of lots of other methods defined loosely inside of the namespace.  Defining methods in the Rake namespace for munging data or doing other task setup usually means that these methods aren&#8217;t formally tested anywhere.  In many cases, it also indicates that opportunities to hide complexity by using an Object-Oriented interface are being lost.</p>
<p>I&#8217;ve found that Rake tasks often descend into a mass of procedural code to manage data.  Part of the strength of Ruby is the set of tools it provides for defining objects that we can use to hide complexity, leading to more maintainable code.  It&#8217;s a shame to not use these tools when it only takes a short amount of time to think about how your task could be decomposed into a set of methods to be invoked on new or existing objects.</p>
<p>It takes some practice to think about writing good object-oriented code in short Rake tasks.  One of the hurdles is thinking of your task in terms of the existing objects in your codebase.  These may be existing models, in which case you have a pre-existing place to put code that would be more <a href="http://en.wikipedia.org/wiki/Cohesion_(computer_science)" target="_blank">cohesive</a> than just including it in the namespace of your Rakefile.  In other cases, you may have to define new classes (often under the &#8220;lib&#8221; folder of your project) to best encapsulate the data that you have to act upon.</p>
<p>Writing short Rake tasks that operate on objects in your codebase rather than defining long task bodies with supporting procedural methods often requires a shift in thinking about your Rake tasks and how it relates to the objects in your codebase.  The effort is worthwhile, however, as you add new classes and more precisely define the interfaces of existing classes in your system in order to achieve a more maintainable system.</p>
]]></content:encoded>
			<wfw:commentRss>http://justinleitgeb.com/2009/09/rails-tip-keep-rake-tasks-short/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
