<?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>Alexandre Martins &#187; Ruby</title>
	<atom:link href="http://blog.m.artins.net/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.m.artins.net</link>
	<description>On Agile Software Development</description>
	<lastBuildDate>Thu, 12 Aug 2010 20:09:21 +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>TDD: Listen to the tests&#8230; they tell smells in your code!</title>
		<link>http://blog.m.artins.net/tdd-listen-to-the-tests-they-tell-smells-in-your-code/</link>
		<comments>http://blog.m.artins.net/tdd-listen-to-the-tests-they-tell-smells-in-your-code/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 02:30:11 +0000</pubDate>
		<dc:creator>Alexandre Martins</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Object-Oriented Programming]]></category>
		<category><![CDATA[Test-Driven Development]]></category>

		<guid isPermaLink="false">http://blog.m.artins.net/?p=305</guid>
		<description><![CDATA[These days, reading the Goos book, by Steve Freeman and Nat Pryce, it reminded me of a project I worked on a while ago. It was a one year old system, poorly tested, integrating to a handful of other systems, and the code-base... well I prefer not to remember. Despite this scenario, I joined the [...]]]></description>
			<content:encoded><![CDATA[<p>These days, reading the <strong><a href="http://www.amazon.com/Growing-Object-Oriented-Software-Guided-Tests/dp/0321503627">Goos</a></strong> book, by <a href="http://www.m3p.co.uk/blog/">Steve Freeman</a> and <a href="http://www.natpryce.com/">Nat Pryce</a>, it reminded me of a project I worked on a while ago. It was a one year old system, poorly tested, integrating to a handful of other systems, and the code-base... well I prefer not to remember. Despite this scenario, I joined the team to help them implement some new functionalities.</p>
<p>I remember sometimes it was difficult to write tests, the classes were tightly coupled, with no clear responsibilities, several attributes, bloated constructors, etc. And despite our best effort, working around the bits that were preventing us from writing the tests, we felt we were getting down the wrong road, trying to do it in such a crappy code-base. As a result some of our tests were massive! A bunch of lines of mocks, stubs, and expectations, making it impossible to understand their purpose.</p>
<h2>What have I learned?</h2>
<p>Reading one of the book chapters I learned that the same qualities that makes an object easy to test also makes the code responsive to change. In my situation, the tests were telling me how clumsy the code was and how difficult it would be to extend it.</p>
<p>I also learned that when we come across a functionality that is difficult to test,   asking ourselves <strong><em>how</em></strong> to test it is not enough, we also have to ask <strong><em>why</em></strong> is it difficult to test, and check whether it's an opportunity to improve our code. The trick is to do it driven by tests, so we can get rapid feedback on code's internal qualities and on whether it's doing what it's supposed to do.</p>
<p>So they introduced a variation for the well-known TDD cycle— <strong>"<em>Write a failing test</em>" =&gt; "<em>Make the test pass</em>" =&gt; "<em>Refactor</em>"</strong>. As described on the figure below (extracted from the book), if we're finding it hard to write the next failing test for our application, we should look again at the design of the production code and often refactor it before moving on, until we get to the point that we can write tests that reads well.</p>
<div style="text-align: center;"><img style="border: 0px none;" src="http://blog.m.artins.net/wp-content/uploads/2010/07/listen-to-tests.png" alt="" width="306" height="226" /></div>
<p><cite>Extracted from Growing Object-Oriented Software, Guided By Tests— Steve Freeman and Nat Pryce</cite></p>
<h2>An Example of a Smell Tests Might Be Telling You</h2>
<h3>Reference data rather than behavior</h3>
<p>When applying "<a href="http://pragprog.com/articles/tell-dont-ask">Tell Don't Ask</a>" or "<a href="http://pragprog.com/articles/tell-dont-ask">Law of Demeter</a>" consistently, we end up with a coding style where we tend to pass behavior into the system instead of pulling values up through the stack. So picking up the famous <em><strong>Paperboy</strong></em> example, before refactoring the code applying the "Law of Demeter" the code and test would look something along the lines of the snippet showed below.</p>
<pre style="background-color: #ffffe3; border-left: solid gray 4px; padding: 8px; margin-bottom: 10px; font-family: Courier New; font-size: 12px;">class Paperboy
  def collect_money(customer, due_amount)
    if due_amount &gt; customer.wallet.cash
      raise InsuficientFundsError
    else
      customer.wallet.cash -= due_amount
      @total_collected += due_amount
    end
  end
end
</pre>
<pre style="background-color: #ffffe3; border-left: solid gray 4px; padding: 8px; margin-bottom: 10px; font-family: Courier New; font-size: 12px;">it "should collect money from customer" do
  customer = Customer.new :wallet =&gt; Wallet.new(:amount =&gt; 200)
  paperboy = Paperboy.new
  paperboy.total_collected.should == 0
  paperboy.collect_money(customer, 50)
  customer.wallet.cash.should == 150
  paperboy.total_collected.should == 50
end
</pre>
<p>We can easily see that the test is telling us it knows too much detail about <span style="font-family: Courier New; font-size: 12px;">Customer</span> class implementation. We can see its internals, which objects it's related to, and even worse, we're also exposing implementation details of its peers. So it's clear for me that it needs some design improvement. My main goal here is to hide <span style="font-family: Courier New; font-size: 12px;">Customer</span> implementation details from the users of the <span style="font-family: Courier New; font-size: 12px;">Paperboy</span> class. Which means that I don't wanna see anything but <span style="font-family: Courier New; font-size: 12px;">Customer</span> and <span style="font-family: Courier New; font-size: 12px;">Money</span> classes referenced on the test!</p>
<pre style="background-color: #ffffe3; border-left: solid gray 4px; padding: 8px; margin-bottom: 10px; font-family: Courier New; font-size: 12px;">class Paperboy
  def collect_money(customer, due_amount)
    @collected_amount += customer.pay(due_amount)
  end
end
</pre>
<pre style="background-color: #ffffe3; border-left: solid gray 4px; padding: 8px; margin-bottom: 10px; font-family: Courier New; font-size: 12px;">it "should collect money from customer" do
  customer = Customer.new :total_cash =&gt; 200
  paperboy = Paperboy.new
  paperboy.total_collected.should == 0
  paperboy.collect_money(customer, 50)
  customer.total_cash.should == 150
  paperboy.total_collected.should == 50
end
</pre>
<p>The method <strong>customer.pay(due_amount)</strong> wraps all the implementation detail up behind a single call. The client of paperboy no longer needs to know anything about the types in the chain. We've reduced the risk that a design change might cause ripples in remote parts of the codebase.</p>
<p>As well as hiding information, there's a more subtle benefit from "Tell, Don't Ask." It forces us to make explicit and so name the interactions between objects, rather than leaving them implicit in the chain of getters. The shorter version above is much clearer about <em><strong>what</strong></em> it's for, not just <em><strong>how</strong></em> it happens to be implemented.</p>
<p>All the logic necessary to collect the money is inside the <span style="font-family: Courier New; font-size: 12px;">Customer</span> object, so it doesn't have to expose its state to its peers.</p>
<p>Now it's safer to continue writing new failing tests to our objects.<br />
Remember, listen to the tests!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.m.artins.net/tdd-listen-to-the-tests-they-tell-smells-in-your-code/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Value Objects &amp; RoR</title>
		<link>http://blog.m.artins.net/value-objects-ror/</link>
		<comments>http://blog.m.artins.net/value-objects-ror/#comments</comments>
		<pubDate>Tue, 12 Jun 2007 01:14:28 +0000</pubDate>
		<dc:creator>Alexandre Martins</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Domain-Driven Design]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.m.artins.net/?p=9</guid>
		<description><![CDATA[Currently I'm working on my personal project, aiming to increase my Ruby On Rails skills. It's quite a simple system for my dad's company and one of the features is to register his clients. I started it out writing the Specs and as I'm new to the Ruby world, doubts started to pop inside my [...]]]></description>
			<content:encoded><![CDATA[<p>Currently I'm working on my personal project, aiming to increase my Ruby On Rails skills. It's quite a simple system for my dad's company and one of the features is to register his clients. I started it out writing the <a href="http://rspec.rubyforge.org/">Specs</a> and as I'm new to the Ruby world, doubts started to pop inside my head. I was wondering how to use <a href="http://www.darrenhobbs.com/archives/2007/04/strong_typing_a.html">Value Objects</a> ( Eric Evans definition, please! ) integrated with RoR, something I can do with Java as shown below:</p>
<pre name="code" class="java">
public class Phone() {
    private int code;
    private int number;
    ....
}
</pre>
<pre name="code" class="java">
public class Client() {
    private String name;
    private Phone home;
    private Phone mobile;
    ....
}
</pre>
<p>Chatting with <a href="http://lixo.org">Carlos Villela</a>, he advised me to take a look at <code>composed_of</code> feature, that it would help me get the solution for my problem. The final result is shown below. I'm definitely not used to the Ruby syntax yet, for me it's still a bit confusing.</p>
<pre name="code" class="ruby">
class Phone
  attr_reader :code, :number
  def initialize(code, number)
      @code, @number = code, number
  end

  def ==(other)
      code==other.code &#038;& number==other.number
  end
end
</pre>
<pre name="code" class="ruby">
class Client < ActiveRecord::Base
  composed_of :home, :class_name => "Phone",
        :mapping => [ %w(home_code code), %w(home_number number) ]
  composed_of :mobile, :class_name => "Phone",
        :mapping => [ %w(mob_code code), %w(mob_number number) ]
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.m.artins.net/value-objects-ror/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
