<?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>Bagonca &#187; software design</title>
	<atom:link href="http://www.bagonca.com/blog/tag/software-design/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bagonca.com/blog</link>
	<description>Yet another developer blog</description>
	<lastBuildDate>Fri, 18 Jun 2010 09:28:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Rule 3: Wrap All Primitives and Strings</title>
		<link>http://www.bagonca.com/blog/2010/01/25/rule-3-wrap-all-primitives-and-strings/</link>
		<comments>http://www.bagonca.com/blog/2010/01/25/rule-3-wrap-all-primitives-and-strings/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 13:11:00 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Developer]]></category>
		<category><![CDATA[Values]]></category>
		<category><![CDATA[software design]]></category>
		<category><![CDATA[ThoughtWorks]]></category>

		<guid isPermaLink="false">http://www.bagonca.com/blog/?p=804</guid>
		<description><![CDATA[Wrapping primitives and strings is a good habit. It increases the architectural value, and potential reuse of your code.]]></description>
			<content:encoded><![CDATA[<p>This is the third part of <a href="http://www.bagonca.com/blog/2010/01/21/nine-steps-to-better-software-design-today-thoughtworks-exercise/" target="_self">a series of articles starting here</a>.</p>
<p>Wrapping primitives (ints, floats etc) and strings is an easy way to increase maintainability and readability of your code. Also, as <strong id="commentauthor-272">Abinesh TD </strong><span id="commentauthor-272">has commented below, wrapping expresses intent of data, preventing misuse. </span>Consider for example the following pseudo-javacode:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Contact <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> emailAddress<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setEmail<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> emailAddress<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> InvalidEmailException <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> validateEmail<span style="color: #009900;">&#40;</span>emailAddress<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> InvalidEmailException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Uhm, that's not a valid mail address...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">emailAddress</span> <span style="color: #339933;">=</span> emailAddress<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getEmail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> emailAddress<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Boolean</span> validateEmail<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> emailAddress<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// TODO: validation regexp etc</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A very simple class, containing a single field intended to hold an email address. It looks good enough, but imagine that you have other places where you would need to validate and store email addresses. You would have a tendency for code duplication right there.</p>
<p>Worst case would be that you would have a validation function in every email container class. It would be better to wrap the string intended to hold the email address in its own class, even if it only holds one field, the potential for code reuse increases exponentially.</p>
<p>A better implementation could look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Contact <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> EmailAddress emailAddress<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setEmail<span style="color: #009900;">&#40;</span>EmailAddress emailAddress<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">emailAddress</span> <span style="color: #339933;">=</span> emailAddress<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setEmail<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> emailAddress<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">emailAddress</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EmailAddress<span style="color: #009900;">&#40;</span>emailAddress<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>InvalidEmailException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Oh lord something went wrong here... :)</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getEmail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> emailAddress<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The benefits should be immediately clear. Suddenly the class EmailAddress requires the email address string to be valid to be instantiated at all. This would make it much easier to quickly zero in on bugs and also increases the potential reuse of your code.</p>
<p>Pretty much any variable you want to store, whether it is a primitive or a string &#8211; WILL need some sort of validation. This is a major reason for wrapping them. But other than that, consistently wrapping gives your object oriented code a higher degree of encapsulation.</p>
<p>Previous « <a href="http://www.bagonca.com/blog/2010/01/22/rule-2-dont-use-the-else-keyword/" target="_self">Rule 2: Don&#8217;t Use the Else Keyword</a><br />
Next » <a href="http://www.bagonca.com/blog/2010/01/27/rule-4-use-only-one-dot-per-line/" target="_self">Rule 4: Only Use One Dot Per Line</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bagonca.com/blog/2010/01/25/rule-3-wrap-all-primitives-and-strings/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Rule 2: Don&#8217;t Use the Else Keyword</title>
		<link>http://www.bagonca.com/blog/2010/01/22/rule-2-dont-use-the-else-keyword/</link>
		<comments>http://www.bagonca.com/blog/2010/01/22/rule-2-dont-use-the-else-keyword/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 00:26:55 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Developer]]></category>
		<category><![CDATA[Values]]></category>
		<category><![CDATA[software design]]></category>
		<category><![CDATA[ThoughtWorks]]></category>

		<guid isPermaLink="false">http://www.bagonca.com/blog/?p=773</guid>
		<description><![CDATA[This is the second part of a series of articles starting here.
The second rule of the &#8220;Nine Ways to Better Software Today&#8221; is to stop using

else

This builds on top of rule 1, to decrease the level on indentation by again focusing on minimizing complex conditionals. Within the object oriented paradigm, polymorphism is a powerful model [...]]]></description>
			<content:encoded><![CDATA[<p>This is the second part of <a href="http://www.bagonca.com/blog/2010/01/21/nine-steps-to-better-software-design-today-thoughtworks-exercise/" target="_self">a series of articles starting here</a>.</p>
<p>The second rule of the &#8220;Nine Ways to Better Software Today&#8221; is to stop using</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">else</span></pre></div></div>

<p>This builds on top of <a title="Rule 1. Only ONE level of indentation" href="http://www.bagonca.com/blog/2010/01/21/nine-steps-to-better-software-design-today-thoughtworks-exercise/" target="_self">rule 1, to decrease the level on indentation</a> by again focusing on minimizing complex conditionals. Within the object oriented paradigm, polymorphism is a powerful model to handle this. Regardless of programming model,  this exercise forces you as developer to find alternatives to using the else keyword and thus considerably beautifying your code.</p>
<p>Consider the following Python code</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> set_member_status<span style="color: black;">&#40;</span>member, status<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot; Spot the else keyword &quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> is_active<span style="color: black;">&#40;</span>member<span style="color: black;">&#41;</span>:
        do_something<span style="color: black;">&#40;</span>member, status<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        do_something_else<span style="color: black;">&#40;</span>member, status<span style="color: black;">&#41;</span></pre></div></div>

<p>There are several ways to simplify this method. For simple cases use early returns, as below. Early returns only work in methods which are short and lacking in complexity (keeping down the level of indentation helps with this). Once a method is long and contains many levels of abstraction, early returns can of course not be used. But by then you&#8217;ve failed at this exercise anyway.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> set_member_status<span style="color: black;">&#40;</span>member, status<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot; Example of using early returns &quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> is_active<span style="color: black;">&#40;</span>member<span style="color: black;">&#41;</span>:
        do_something<span style="color: black;">&#40;</span>member, status<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #808080; font-style: italic;"># here is the early return :)</span>
&nbsp;
    do_something_else<span style="color: black;">&#40;</span>member, status<span style="color: black;">&#41;</span></pre></div></div>

<p>In many languages it is possible to use the ternary operator. That would look something like this (in javascript):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/** Wrong */</span>
<span style="color: #003366; font-weight: bold;">function</span> drowned<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>timeInWater <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> SURVIVABLE_TIME<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009966; font-style: italic;">/** Right */</span>
<span style="color: #003366; font-weight: bold;">function</span> drowned<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">return</span> timeInWater <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #CC0000;">10</span> <span style="color: #339933;">?</span> <span style="color: #003366; font-weight: bold;">false</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Guard clauses is another way of increasing readability. The following example is <a href="http://www.refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html" target="_blank">taken from here</a></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">double</span> getPayAmount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">double</span> result<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>_isDead<span style="color: #009900;">&#41;</span> result <span style="color: #339933;">=</span> deadAmount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>_isSeparated<span style="color: #009900;">&#41;</span> result <span style="color: #339933;">=</span> separatedAmount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>_isRetired<span style="color: #009900;">&#41;</span> result <span style="color: #339933;">=</span> retiredAmount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #b1b100;">else</span> result <span style="color: #339933;">=</span> normalPayAmount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> result<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>	
&nbsp;
<span style="color: #993333;">double</span> getPayAmount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>_isDead<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> deadAmount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>_isSeparated<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> separatedAmount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>_isRetired<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> retiredAmount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> normalPayAmount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Previous « <a href="http://www.bagonca.com/blog/2010/01/21/nine-steps-to-better-software-design-today-thoughtworks-exercise/">Rule 1: Only Use One Level of Indentation</a><br />
Next » <a href="http://www.bagonca.com/blog/2010/01/25/rule-3-wrap-all-primitives-and-strings/" target="_self">Rule 3: Wrap All Primitives and Strings</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bagonca.com/blog/2010/01/22/rule-2-dont-use-the-else-keyword/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Nine Steps to Better Software Design Today: Thoughtworks Exercise</title>
		<link>http://www.bagonca.com/blog/2010/01/21/nine-steps-to-better-software-design-today-thoughtworks-exercise/</link>
		<comments>http://www.bagonca.com/blog/2010/01/21/nine-steps-to-better-software-design-today-thoughtworks-exercise/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 12:51:04 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Developer]]></category>
		<category><![CDATA[Values]]></category>
		<category><![CDATA[software design]]></category>
		<category><![CDATA[ThoughtWorks]]></category>

		<guid isPermaLink="false">http://www.bagonca.com/blog/?p=757</guid>
		<description><![CDATA[ThoughtWorks is a really cool company. And their book the ThoughtWorks Anthology has a really cool hands on exercise which I think every programmer could benefit greatly on. This series of articles is my take on the exercise.]]></description>
			<content:encoded><![CDATA[<p>There are few companies out there more exiting to me than Thoughtworks. They sport a fantastic set of values, and have some of todays most brilliant programmers and software thinkers on board. But what strikes me as most wonderful about them is the way they communicate with programmers around the world. They really have an interest in good software, whether it is their own, or somebody else&#8217;s.</p>
<p>On reading their book, the <a title="Check out the book!" href="http://pragprog.com/titles/twa/thoughtworks-anthology" target="_blank">Thoughtworks Anthology</a> there was one chapter (Object Calisthenics by Jeff Bay)	 which struck me as extremely fun to read, it contained an exercise called &#8220;Nine Steps to Better Software Design Today&#8221;. An exercise completely focused on object oriented design quality with all that that entails, like <a title="Encapsulation @ Wikipedia" href="http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)" target="_blank">encapsulation</a> and appropriate use of <a title="Polymorphism @ Wikipedia" href="http://en.wikipedia.org/wiki/Polymorphism_(computer_science)">polymorphism</a>.</p>
<p>It is really a very simple exercise in many aspects, it simply provides nine rules, and asks me as a developer to do my absolute best to abide by those rules. What Thoughtworks does is so ingenious. Instead of just preaching the holy grail of object oriented programming, they ask me to be a part of the sermon. On reading the whole exercise I was instantly revving my engine to actually do the exercise. So I started up a small GWT project and forced myself to actually try and comply with the nine rules.</p>
<p>I&#8217;ve never had so much fun programming! And the process truly changed the way I will write code from now, whether it is in a purely object oriented language or not.</p>
<p>Said and done. I will start with the first rule:</p>
<h2>Rule 1 : Use only one level of indentation per method</h2>
<p>No matter which language you are working with, this is something that is very important. Check out the class, that thoughtworks uses as an example, below. It contains one function with several levels of indentation.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Board <span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">String</span> board<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">StringBuffer</span> buf <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
        buf.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      buf.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">return</span> buf.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now consider that after some refactoring the code could look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Board <span style="color: #009900;">&#123;</span>
 <span style="color: #003399;">String</span> board<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">StringBuffer</span> buf <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  collectRows<span style="color: #009900;">&#40;</span>buf<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">return</span> buf.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #000066; font-weight: bold;">void</span> collectRows<span style="color: #009900;">&#40;</span><span style="color: #003399;">StringBuffer</span> buf<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
   collectRow<span style="color: #009900;">&#40;</span>buf, i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #000066; font-weight: bold;">void</span> collectRow<span style="color: #009900;">&#40;</span><span style="color: #003399;">StringBuffer</span> buf, <span style="color: #000066; font-weight: bold;">int</span> row<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
   buf.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#91;</span>row<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  buf.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>What basically has happened here is that methods have been extracted for each level of indentation. The whole class is so much more readable, unit testable, understandable&#8230; you name it. Or as the authors of the chapter puts it:</p>
<blockquote><p>Try to ensure that each method does exactly one thing. One control structure, or one block of statements per method. [Heavy indentation] in a method [is] a signal that you&#8217;re working at multiple level of abstraction, and that means you&#8217;re doing more than one thing.<br />
&#8230;<br />
Working with methods that do exactly one thing, and classes doing exactly one thing, your code begins to change, [...] increasing exponentially the level of reuse.</p></blockquote>
<p>Next time I will talk about <a href="http://www.bagonca.com/blog/2010/01/22/rule-2-dont-use-the-else-keyword/" target="_self">rule 2: <strong>Don’t use the else keyword</a></strong.> <img src='http://www.bagonca.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.bagonca.com/blog/2010/01/21/nine-steps-to-better-software-design-today-thoughtworks-exercise/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
