<?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; Developer</title>
	<atom:link href="http://www.bagonca.com/blog/category/developer/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>Use tap events instead of click events in iPhone browser</title>
		<link>http://www.bagonca.com/blog/2010/06/18/use-tap-events-instead-of-click-events-in-iphone-browser/</link>
		<comments>http://www.bagonca.com/blog/2010/06/18/use-tap-events-instead-of-click-events-in-iphone-browser/#comments</comments>
		<pubDate>Fri, 18 Jun 2010 09:17:28 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[projectplace]]></category>

		<guid isPermaLink="false">http://www.bagonca.com/blog/?p=851</guid>
		<description><![CDATA[When working on a web app for embedding in a iPhone application we came across jQTouch, a really nifty little jQuery plugin which makes it easy to create a web application UI that really resembles that of a native iPhone application.
We soon encountered a problem due to the fact that click events are rather slow [...]]]></description>
			<content:encoded><![CDATA[<p>When working on a web app for embedding in a iPhone application we came across <a title="jQTouch" href="http://jqtouch.com/">jQTouch</a>, a really nifty little jQuery plugin which makes it easy to create a web application UI that really resembles that of a native iPhone application.</p>
<p>We soon encountered a problem due to the fact that click events are rather slow to fire on the iPhone. There is a delay due to the fact that the iPhone waits for the user to complete a gesture before deciding that the intended gesture was in fact a click.</p>
<p>JQTouch uses divs to navigate between screens, so for example if you have the div</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;primary-view&quot;</span><span style="color: #339933;">&gt;&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>

<p>you can simply navigate to it through a normal anchor link, and the transition will thanks to jQTouch be really &#8220;iPhone like&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;#primary-view&quot;</span><span style="color: #339933;">&gt;</span>Primary view<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Our main problem is that we load our content dynamically through Ajax. So our links looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>a onclick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;fetchContent();&quot;</span> href<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;#primary-view&quot;</span><span style="color: #339933;">&gt;</span>Primary view<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span></pre></div></div>

<p>This resulted in a race condition. The anchor link listens to the tap event which is fired much earlier than the click event, and therefore we mostly ended up in empty views. So we needed to replace all onclicks to ontap listeners. The problem then is that we could no longer test our webapp in a normal desktop based Safari browser.</p>
<p>So, we decided to happily go along and write inline onclick events on our links and other interaction elements. But while in the iPhone we see to it that the onClicks are removed and replaced with tap listeners.</p>
<p>That function looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> rebindClicks<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> userAgent <span style="color: #339933;">=</span> navigator.<span style="color: #660066;">userAgent</span>.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> isIphone <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>userAgent.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'iphone'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #003366; font-weight: bold;">true</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isIphone<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// For each event with an inline onclick</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'[onclick]'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> onclick <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onclick'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeAttr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onclick'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Remove the onclick attribute</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> preventClickEvent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// See to it that clicks never happen</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tap'</span><span style="color: #339933;">,</span> onclick<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Point taps to the onclick</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> preventClickEvent<span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span>
    event.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We now call this little function everytime the DOM is changed. If something turns up with an inline onclick. It is will be rerouted by jQuery to a tap listener instead.</p>
<p>Again, we felt we needed to do this because many of our developers do not have a Mac (and hence do not have the iPhone simulator). And testing on the iPhone can be really time consuming. So for testing during development we wanted to use onClick, and when testing on iPhone we wanted to use taps instead.</p>
<p>But the biggest bonus was without a doubt that things are overall <em>much faster</em> in the web application. Taps fire much faster than clicks in the iPhone.</p>
<p>Here&#8217;s how it looks running the same app both in desktop safari and embedded into the iPhone simulator.</p>
<p><object id="scPlayer" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="862" height="746" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="quality" value="high" /><param name="bgcolor" value="#FFFFFF" /><param name="flashVars" value="thumb=http://content.screencast.com/users/omgwtflolomg/folders/Jing/media/4df1e2fe-264d-4de7-b72c-4e83d2338601/FirstFrame.jpg&amp;containerwidth=862&amp;containerheight=746&amp;content=http://content.screencast.com/users/omgwtflolomg/folders/Jing/media/4df1e2fe-264d-4de7-b72c-4e83d2338601/00000050.swf" /><param name="allowFullScreen" value="true" /><param name="scale" value="showall" /><param name="allowScriptAccess" value="always" /><param name="base" value="http://content.screencast.com/users/omgwtflolomg/folders/Jing/media/4df1e2fe-264d-4de7-b72c-4e83d2338601/" /><param name="src" value="http://content.screencast.com/users/omgwtflolomg/folders/Jing/media/4df1e2fe-264d-4de7-b72c-4e83d2338601/jingswfplayer.swf" /><param name="flashvars" value="thumb=http://content.screencast.com/users/omgwtflolomg/folders/Jing/media/4df1e2fe-264d-4de7-b72c-4e83d2338601/FirstFrame.jpg&amp;containerwidth=862&amp;containerheight=746&amp;content=http://content.screencast.com/users/omgwtflolomg/folders/Jing/media/4df1e2fe-264d-4de7-b72c-4e83d2338601/00000050.swf" /><param name="allowfullscreen" value="true" /><embed id="scPlayer" type="application/x-shockwave-flash" width="862" height="746" src="http://content.screencast.com/users/omgwtflolomg/folders/Jing/media/4df1e2fe-264d-4de7-b72c-4e83d2338601/jingswfplayer.swf" base="http://content.screencast.com/users/omgwtflolomg/folders/Jing/media/4df1e2fe-264d-4de7-b72c-4e83d2338601/" allowscriptaccess="always" scale="showall" allowfullscreen="true" flashvars="thumb=http://content.screencast.com/users/omgwtflolomg/folders/Jing/media/4df1e2fe-264d-4de7-b72c-4e83d2338601/FirstFrame.jpg&amp;containerwidth=862&amp;containerheight=746&amp;content=http://content.screencast.com/users/omgwtflolomg/folders/Jing/media/4df1e2fe-264d-4de7-b72c-4e83d2338601/00000050.swf" bgcolor="#FFFFFF" quality="high"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bagonca.com/blog/2010/06/18/use-tap-events-instead-of-click-events-in-iphone-browser/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rule 4: Use Only One Dot Per Line</title>
		<link>http://www.bagonca.com/blog/2010/01/27/rule-4-use-only-one-dot-per-line/</link>
		<comments>http://www.bagonca.com/blog/2010/01/27/rule-4-use-only-one-dot-per-line/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 11:08:16 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Developer]]></category>
		<category><![CDATA[Values]]></category>

		<guid isPermaLink="false">http://www.bagonca.com/blog/?p=823</guid>
		<description><![CDATA[Using only one dot per line? With all the help you get from IDE:s to complete long lines, why? Well because great software architecture, especiall]]></description>
			<content:encoded><![CDATA[<p>This is the fourth 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 fourth rule of the exercise is to only use one dot per line. Why is that important? Well apart from the apparent readability increase, many dots one one line can signify that an <strong>activity is happening in the wrong place</strong>. Or maybe your object is dealing with <strong>two other objects at the same time</strong>.</p>
<p>In both these cases you are not fully implementing the object oriented paradigm. Objects are supposed to know something about their immediate &#8220;friends&#8221; but not about their friend&#8217;s friends.</p>
<p>This is an example of a quite broken implementation.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> TimeLineObject extends UIComponent <span style="color: #000000;">&#123;</span>
    ....
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> redraw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
        TimeLineDrawer.theInstance.drawTimeLine<span style="color: #000000;">&#40;</span>TimeLineDrawer.theInstance.timeLineCanvas<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>To follow the rule of this exercise you might be tempted to remove the number of dots per line by doing something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> TimeLineObject extends UIComponent <span style="color: #000000;">&#123;</span>
    ....
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> redraw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #6699cc; font-weight: bold;">var</span> timeLineDrawerSingleton<span style="color: #000000; font-weight: bold;">:</span>TimeLineDrawer = TimeLineDrawer.theInstance;
        <span style="color: #6699cc; font-weight: bold;">var</span> canvas<span style="color: #000000; font-weight: bold;">:</span>Canvas = timeLineDrawerSingleton.timeLineCanvas;
        timeLineDrawerSingleton.drawTimeLine<span style="color: #000000;">&#40;</span>canvas<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>But this is hardly the point. It is very apparent in the above example that the activity as a whole is not in the correct place. In the <code>TimeLineObject</code> we are suddenly telling some remote singleton static object to carry out things for us. Our object knows to much and the architecture has in fact failed at some earlier point.</p>
<p>The example from the Thoughtworks book is excellent. It shows the correct way to think about the exercise, namely to put activities in its right place. I will quote it below. First the &#8220;erroneous&#8221; example:</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: #000000; font-weight: bold;">class</span> Piece <span style="color: #009900;">&#123;</span>
     ...
     <span style="color: #003399;">String</span> representation<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">class</span> Location <span style="color: #009900;">&#123;</span>
      ...
      <span style="color: #006633;">Piece</span> current<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #003399;">String</span> boardRepresentation<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>Location l <span style="color: #339933;">:</span> squares<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
          <span style="color: #666666; font-style: italic;">// Oh lord lots of dots here... :)</span>
          buf.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>l.<span style="color: #006633;">current</span>.<span style="color: #006633;">representation</span>.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</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>Applying the rules of having one dot per line here actually means REAL refactoring. I.e putting stuff where it belongs. And that is usually in other classes. Here&#8217;s the refactored example:</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: #000000; font-weight: bold;">class</span> Piece <span style="color: #009900;">&#123;</span>
      ...
      <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> representation<span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #003399;">String</span> character<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> representation.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000066; font-weight: bold;">void</span> addTo<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>
          buf.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>character<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">class</span> Location <span style="color: #009900;">&#123;</span>
      ...
      <span style="color: #000000; font-weight: bold;">private</span> Piece current<span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">void</span> addTo<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>
          current.<span style="color: #006633;">addTo</span><span style="color: #009900;">&#40;</span>buf<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #003399;">String</span> boardRepresentation<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>Location l <span style="color: #339933;">:</span> squares<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
           l.<span style="color: #006633;">addTo</span><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>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So, the lesson from this exercise is that you should put activities where they belong. Avoid using static utility classes and other &#8220;middleman&#8221; objects. And make sure that the object you are working in does not now to much about other object&#8217;s activities. Couple this with the previous rules and you have gone far in a sturdy, nice looking design.</p>
<p>Previous « <a href="http://www.bagonca.com/blog/2010/01/25/rule-3-wrap-all-primitives-and-strings/">Rule 3: Wrap All Primitives and Strings</a><br />
Next &raquo; Rule 5: Don&#8217;t Abbreviate</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bagonca.com/blog/2010/01/27/rule-4-use-only-one-dot-per-line/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<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>
		<item>
		<title>A search on search widget</title>
		<link>http://www.bagonca.com/blog/2010/01/14/search-on-search-widget/</link>
		<comments>http://www.bagonca.com/blog/2010/01/14/search-on-search-widget/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 13:15:03 +0000</pubDate>
		<dc:creator>PEZ</dc:creator>
				<category><![CDATA[Developer]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[Widget]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://www.bagonca.com/blog/?p=749</guid>
		<description><![CDATA[A search on search widget. Please use on your own Wordpress blog.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been <a href="http://en.forums.wordpress.com/topic/widget-search-on-search?replies=6">wishing for</a> a Wordpress widget that would help visitors find more blog content relevant to the search engine terms that got them to the blog in the first place. Maybe I&#8217;m just bad at googling or something, because I haven&#8217;t found one.</p>
<p>Now I&#8217;m writing my own widget, called &#8220;Search on search&#8221;. If you arrive at this blog from a search engine link you might see a section for it in the sidebar. If, not, and you&#8217;re curious you could force it by trying this search:</p>
<ul>
<li><a href="http://www.google.com/search?q=hire+the+best+and+set+them+free">http://www.google.com/search?q=hire+the+best+and+set+them+free</a></li>
</ul>
<p>And then in the Google results find a link going to this site. (The widget doesn&#8217;t appear if it can&#8217;t figure out the search engine terms.)</p>
<p>You can find the widget code as a gist on Github, namely here:</p>
<ul>
<li><a href="http://gist.github.com/275734">http://gist.github.com/275734</a></li>
</ul>
<p>It&#8217;s not rocket surgery. The main job is about figuring out what search engine terms, if any, that the user used to find the blog. But I  didn&#8217;t need to solve that one either. <a href="http://www.istanto.net/phpcatching-keyword-from-search-engine.html">Istanto had already solved it</a> for me. =)</p>
<p>I still have a bug to iron out: The links presented doesn&#8217;t get styled like the other lists. I can&#8217;t figure out why&#8230; Feel invited to help me!</p>
<p><em>Update 17 January</em>: The plugin is now available in the Wordpress.org plugin directory:</p>
<ul>
<li><a href="http://wordpress.org/extend/plugins/search-on-search/">http://wordpress.org/extend/plugins/search-on-search/</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.bagonca.com/blog/2010/01/14/search-on-search-widget/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Flickr Searches in 3D</title>
		<link>http://www.bagonca.com/blog/2009/11/03/flickr-searches-in-3d/</link>
		<comments>http://www.bagonca.com/blog/2009/11/03/flickr-searches-in-3d/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 20:56:13 +0000</pubDate>
		<dc:creator>PEZ</dc:creator>
				<category><![CDATA[Flex/AS]]></category>
		<category><![CDATA[Innovation]]></category>

		<guid isPermaLink="false">http://www.bagonca.com/blog/?p=744</guid>
		<description><![CDATA[I&#8217;m experimenting with tying that 3D Album I mentioned earlier to Flickr Searches. Looks like this for now
ThreeDeeGallery.swf
Work in progress, both the app and this blog post.
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m experimenting with tying that 3D Album I mentioned earlier to Flickr Searches. Looks like this for now<br />
<a href="http://www.bagonca.com/blog/wp-content/uploads/2009/11/ThreeDeeGallery.swf">ThreeDeeGallery.swf</a><br />
Work in progress, both the app and this blog post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bagonca.com/blog/2009/11/03/flickr-searches-in-3d/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>So you wanna be a front end developer?</title>
		<link>http://www.bagonca.com/blog/2009/05/13/so-you-wanna-be-a-front-end-developer/</link>
		<comments>http://www.bagonca.com/blog/2009/05/13/so-you-wanna-be-a-front-end-developer/#comments</comments>
		<pubDate>Wed, 13 May 2009 06:37:01 +0000</pubDate>
		<dc:creator>Olle</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.bagonca.com/blog/?p=398</guid>
		<description><![CDATA[Some tips on how to become a great front end developer. Recipe: will to learn!]]></description>
			<content:encoded><![CDATA[<p>Back in the day when I was a young boy in the big IT-industry I wanted to become the greatest front end developer the web had seen. Due to circumstances, out of my hands of course, I did not reach my goal, but I did get to &#8220;know&#8221; some great ones along the way!</p>
<p>So I guess you possibly have some schooling to bring to the table, or you have been working with back-end stuff stuff for quite some time, you know what object oriented programming is or at least want to learn, and most importantly you have the will to learn!</p>
<p>When I started to become really interested in front end development back in the late 90&#8217;s, front end development was not looked upon as real programming, real programming was C++, Java and the likes. But now, we have job titles like Senior Front End Developer at for instance Yahoo and Google, so I guess that the understanding of the importance of good front end code has really elevated.</p>
<p>So my way of learning the craftsmanship, was to study the masters below, downloaded their code, read it, and then write my own! If it worked for me, I&#8217;m sure it will for you!</p>
<p><strong>Scott Andrew LePera</strong><br />
What really got me started was a guy named Scott Andrew LePera(<a href="http://www.scottandrew.com/">http://www.scottandrew.com/</a>). He got me all warm and fuzzy talking about for instance <a href="http://www.scottandrew.com/weblog/articles/cbs-events">event handlers in javascript</a> and how event bubbles</p>
<p><strong>Aaron Boodman</strong><br />
After that I stumbled over <a href="http://www.aaronboodman.com/">Aaron Boodman,</a> the <a href="http://youngpup.net/">youngpup</a> (and creator of <a href="http://www.youtube.com/watch?hl=sv&amp;v=cQyha30nm6k&amp;gl=SE">Greasemonky</a> among things) showed me all about how to create the menus that we love to have, how to make the Javascripts run smooth in <a href="http://youngpup.net/projects/2027/demo.html">animations</a>, and how <a href="http://youngpup.net/projects/dom-drag/demo.html">drag of elements</a> shall be done. Back in the beginning of the 2000 he had the coolest UI on his blog, and nothing I have seen since has turned me on as much as viewing youngpup.net for the first time(except possibly 13thparallel)!</p>
<p><strong>13thparallel</strong><br />
<a href="http://13thparallel.com/archive/">13thparallel</a> made me think about <a href="http://13thparallel.com/archive/coding-for-portability1/">coding</a> for <a href="http://13thparallel.com/archive/coding-for-portability2/">portability</a>, introduced me to the concept of the <a href="http://13thparallel.com/archive/viewport/">Viewport</a> among other things.</p>
<p><strong>WebFX</strong><br />
Emil A Eklund and Erik Arvidsson at <a href="http://webfx.eae.net/">WebFX</a> showed me that it was possible to create a <a href="http://webfx.eae.net/webboard/">forum</a> on the web with outstanding functionality and speed(unfortunately it only looks good in MS IE), how to work with <a href="http://webfx.eae.net/dhtml/xloadtree/demo.html">XML in advanced Javascript.</a></p>
<p><strong>Peter-Paul Koch @Quirksmode</strong><br />
<a href="http://www.quirksmode.org">Quirksmode</a> helped me really understand the differences between browsers and what do do about it. He also really introduced me to <a href="http://en.wikipedia.org/wiki/Unobtrusive_JavaScript">Unobtrusive JavaScript</a>.</p>
<p>Remember that most of the examples above have been written back in 2001-2002, and they still work..What does this tell us, well to use the standards, and no browser specific hack, because browsers change and you will end up chasing your own tail to make things work in new browsers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bagonca.com/blog/2009/05/13/so-you-wanna-be-a-front-end-developer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails deploy using sqlite3</title>
		<link>http://www.bagonca.com/blog/2009/05/09/rails-deploy-using-sqlite3/</link>
		<comments>http://www.bagonca.com/blog/2009/05/09/rails-deploy-using-sqlite3/#comments</comments>
		<pubDate>Sat, 09 May 2009 12:22:09 +0000</pubDate>
		<dc:creator>PEZ</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[capistrano]]></category>
		<category><![CDATA[deploy]]></category>
		<category><![CDATA[sqlite3]]></category>

		<guid isPermaLink="false">http://www.bagonca.com/blog/?p=394</guid>
		<description><![CDATA[I had a hard time figuring out why, when reading up on capistrano, I couldn&#8217;t find any info on how to deal with the database file. That was until I realized most people don&#8217;t deploy on sqlite3. With mysql and other databases you have a server and it&#8217;s automatically &#8220;shared&#8221; then.
The only information I found [...]]]></description>
			<content:encoded><![CDATA[<p>I had a hard time figuring out why, when reading up on <a href="http://rubyforge.org/projects/capistrano/">capistrano</a>, I couldn&#8217;t find any info on how to deal with the database file. That was until I realized most people don&#8217;t deploy on <a href="http://www.sqlite.org/">sqlite3</a>. With mysql and other databases you have a server and it&#8217;s automatically &#8220;shared&#8221; then.</p>
<p>The only information I found on deployment on sqlite3 was an excellent deploy script in <a href="http://blog.ninjahideout.com/posts/busting-a-cap-in-yo-ass">this blog article</a>. Basically, using sqlite3 you have to make sure the database is in a shared directory across releases. But the sqlite3 parts of that deploy script didn&#8217;t work for me as they were. I had to make sure I referenced the shared database path all the way or I risked overwriting my database with a symlink. Below are the sqlite3 parts of the resulting capistrano script.</p>
<p>Setting up the shared database path. NB: Lazy binding. Important if you&#8217;re using multistaging from capistrano-ext.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">set<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:shared_database_path</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;#{shared_path}/databases&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Sqlite3 tasks.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">namespace <span style="color:#ff3333; font-weight:bold;">:sqlite3</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  desc <span style="color:#996600;">&quot;Generate a database configuration file&quot;</span>
  task <span style="color:#ff3333; font-weight:bold;">:build_configuration</span>, <span style="color:#ff3333; font-weight:bold;">:roles</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:db</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    db_options = <span style="color:#006600; font-weight:bold;">&#123;</span>
      <span style="color:#996600;">&quot;adapter&quot;</span>  <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;sqlite3&quot;</span>,
      <span style="color:#996600;">&quot;database&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;#{shared_database_path}/production.sqlite3&quot;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>
    config_options = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;production&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> db_options<span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">to_yaml</span>
    put config_options, <span style="color:#996600;">&quot;#{shared_config_path}/sqlite_config.yml&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  desc <span style="color:#996600;">&quot;Links the configuration file&quot;</span>
  task <span style="color:#ff3333; font-weight:bold;">:link_configuration_file</span>, <span style="color:#ff3333; font-weight:bold;">:roles</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:db</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    run <span style="color:#996600;">&quot;ln -nsf #{shared_config_path}/sqlite_config.yml #{release_path}/config/database.yml&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  desc <span style="color:#996600;">&quot;Make a shared database folder&quot;</span>
  task <span style="color:#ff3333; font-weight:bold;">:make_shared_folder</span>, <span style="color:#ff3333; font-weight:bold;">:roles</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:db</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    run <span style="color:#996600;">&quot;mkdir -p #{shared_database_path}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Hooks (or whatever they&#8217;re called, I&#8217;m new to all this).</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">after <span style="color:#996600;">&quot;deploy:setup&quot;</span>, <span style="color:#996600;">&quot;sqlite3:make_shared_folder&quot;</span>
after <span style="color:#996600;">&quot;deploy:setup&quot;</span>, <span style="color:#996600;">&quot;sqlite3:build_configuration&quot;</span>
&nbsp;
before <span style="color:#996600;">&quot;deploy:migrate&quot;</span>, <span style="color:#996600;">&quot;sqlite3:link_configuration_file&quot;</span></pre></div></div>

<p>Hope this helps someone. Please don&#8217;t hesitate to ask should something need more explaining. And of course suggest improvements. This is all the makings of a capistrano newbie.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bagonca.com/blog/2009/05/09/rails-deploy-using-sqlite3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Submitting iPhone app to App Store</title>
		<link>http://www.bagonca.com/blog/2009/05/08/submitting-iphone-app-to-app-store/</link>
		<comments>http://www.bagonca.com/blog/2009/05/08/submitting-iphone-app-to-app-store/#comments</comments>
		<pubDate>Thu, 07 May 2009 23:38:55 +0000</pubDate>
		<dc:creator>Samuel</dc:creator>
				<category><![CDATA[Innovation]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[inspiration]]></category>
		<category><![CDATA[project management]]></category>
		<category><![CDATA[projectplace]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.bagonca.com/blog/?p=374</guid>
		<description><![CDATA[Finally, after two bounces and three weeks of waiting, our first app on App Store is available!
I&#8217;ll keep this post really short!
Pros
Great testing by the App Store review team.
Good and thorough feedback.
Cons
Time consuming! But, reasonable considering the amount of apps that is submitted to the App Store.
iTunes Connect
Some good to knows!
It takes approximately 5 working [...]]]></description>
			<content:encoded><![CDATA[<p>Finally, after two bounces and three weeks of waiting, our first app on App Store is available!</p>
<p>I&#8217;ll keep this post really short!</p>
<blockquote><p>Pros</blockquote >
<li>Great testing by the App Store review team.</li>
<li>Good and thorough feedback.</li>
<blockquote><p>Cons</blockquote >
<li>Time consuming! But, reasonable considering the amount of apps that is submitted to the App Store.</li>
<li>iTunes Connect</li>
<blockquote><p>Some good to knows!</blockquote >
<li>It takes approximately 5 working days before you hear anything from the review team.</li>
<li>If you reject (developer reject) your application and post a new version when an app is undergoing a review, then you will have to wait another 5 more days.</li>
<blockquote><p>Conclusion</blockquote >
The App Store review process: 4 stars out of 5</p>
<blockquote><p>Projectplace for iPhone</blockquote >
If you are curious about our iPhone application, please <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=312388930&amp;mt=8">visit our app section @ App Store</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bagonca.com/blog/2009/05/08/submitting-iphone-app-to-app-store/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
