<?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>Extreme Enthusiasm &#187; essay</title>
	<atom:link href="http://matteo.vaccari.name/blog/archives/category/essay/feed" rel="self" type="application/rss+xml" />
	<link>http://matteo.vaccari.name/blog</link>
	<description>Extreme enthusiasm</description>
	<lastBuildDate>Sun, 22 Jan 2012 16:15:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Formalism versus Object Thinking</title>
		<link>http://matteo.vaccari.name/blog/archives/713</link>
		<comments>http://matteo.vaccari.name/blog/archives/713#comments</comments>
		<pubDate>Sun, 22 Jan 2012 16:08:48 +0000</pubDate>
		<dc:creator>matteo</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[essay]]></category>

		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=713</guid>
		<description><![CDATA[The book Object Thinking makes a very good explanation of the divide between two different, antagonistic modes of thinking. The formalist tradition (in software) values logic and mathematics as design tools. A formalist thinks that documents have objective, intrinsic meaning. Think &#8220;design documents&#8221;; think &#8220;specifications&#8221;. The empirical tradition (the book calls it hermeneutics, but I [...]]]></description>
			<content:encoded><![CDATA[<p>The book <a href="http://books.google.it/books/about/Object_thinking.html?id=-eJQAAAAMAAJ&amp;redir_esc=y" title="Object thinking - David West - Google Libri">Object Thinking</a> makes a very good explanation of the divide between two different, antagonistic modes of thinking.  The formalist tradition (in software) values logic and mathematics as design tools.  A formalist thinks that documents have objective, intrinsic meaning.  Think &#8220;design documents&#8221;; think &#8220;specifications&#8221;.</p>
<p>The empirical tradition (the book calls it hermeneutics, but I will stick to this less formal name :-) values experimentation.  Empiricists hold that the meaning of a document is a shared, temporary convention between the author and the readers.  Think &#8220;user story&#8221;; think CRC cards; think &#8220;quick design session on the whiteboard.&#8221;</p>
<p>The empiriricists brought us Lisp; the formalists brought us Haskell.  The formalists brought us Algol, Pascal, Ada.  The empiricists brought us C, Perl, Smalltalk.</p>
<p>Empiricists like to explain things with anthropomorphism: &#8220;this object <em>knows</em> this and <em>wants</em> to talk to that other object&#8230;&#8221;  The formalists detest anthropomorphism; see these quotes from <a href="http://lambda-the-ultimate.org/node/264" title="Dijkstra on analogies and anthropomorphism | Lambda the Ultimate">Dijkstra</a>.</p>
<p>As a former minor student of <a href="http://cs-exhibitions.uni-klu.ac.at/index.php?id=31" title="Burroughs Corporation">the best formalist tradition</a> there is, and a current student of the Object Thinking tradition, I think I&#8217;m qualified to comment.  Please don&#8217;t take my notes as meaning that the formalist tradition sucks; I certainly don&#8217;t think this.  I&#8217;m interested in highlighting differences.  I think a good developer should <strong>learn from both schools</strong>.</p>
<p>Formalists aim to bring clarity of thought by leveraging mathematical thinking.</p>
<p>Object thinking aims to bring clarity of thought by leveraging spatial reasoning, metaphor, intuition, and other modes of thinking.  </p>
<p>It is well known that mathematical thinking is powerful.  It&#8217;s also more difficult to learn and use.  One example that was <a href="http://www.cs.utexas.edu/~EWD/transcriptions/EWD10xx/EWD1036.html" title="E.W. Dijkstra Archive: On the cruelty of really teaching computing science (EWD 1036)">a favourite of Dijkstra</a> is the problem of <a href="http://gurmeet.net/puzzles/tiling-a-chessboard-with-dominoes/" title="Tiling a Chessboard with Dominoes">covering a chessboard with dominoes</a> when the opposite corners of the chessboards were removed.  If we try to prove that it&#8217;s impossible by &#8220;trying&#8221; to do it or simulating it, we&#8217;d quickly get bogged down.  On the other hand, there&#8217;s a very simple and nice proof that shows that it&#8217;s impossible.  Once you get that idea, you have power :-)</p>
<p>An even more striking example is in <a href="http://www.cs.utexas.edu/~EWD/transcriptions/EWD09xx/EWD980.html" title="E.W. Dijkstra Archive: The strange case of The Pigeon-hole Principle (EWD 980)">this note from Dijkstra</a> on the proof method called &#8220;pigeonhole principle&#8221;.  Dijkstra finds that the name &#8220;pigeon-hole principle&#8221; is unfortunate, as is the idea to imagine &#8220;holes&#8221; and a process of filling them with &#8220;pigeons&#8221; until you find that some pigeon has no hole.  The process is vivid and easy to understand; yet it is limiting.  Dijkstra shows in this note how to define the principle in a more simple and powerful way: </p>
<blockquote><p>
  For a non-empty, finite bag of numbers, the maximum value is at least the average value.
</p></blockquote>
<p>This formulation is simple (<a href="http://www.fcgarage.com/2009/05/the-magic-suitcase.html" title="The Magic Suitcase - FC Garage by Francesco Cirillo">but not easy</a>!)  Armed with this formulation, Dijkstra explains how he used this principle to solve on the spot a combinatorial problem about Totocalcio that a collegue of his could not solve with pen and paper.  He also explains how he used it to solve a generalization of the problem, which would not be easy to prove with the &#8220;object-oriented&#8221; version of the principle.</p>
<p>I think this note presents the contrast between formalism and empiricism vividly.  If you put in the effort to internalize the formal tool, that which was difficult becomes easy, and you can solve a whole new level of problems.</p>
<p>On the other hand, the formalists do now always win :-)  Formalists reject the idea of making tests the cornerstone of software development.  In my opinion they are squarely wrong; <em>examples</em> are the primary tools to do software development, and you can&#8217;t even understand if a specification is correct until you *test* it with examples.</p>
<p>The one thing that boths camps have in common is that they are both minority arts.  Real OOP is almost as rare as Dijkstra-style program derivation.  The common industrial practice is whateverism :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://matteo.vaccari.name/blog/archives/713/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>On knowing what you&#8217;re doing</title>
		<link>http://matteo.vaccari.name/blog/archives/645</link>
		<comments>http://matteo.vaccari.name/blog/archives/645#comments</comments>
		<pubDate>Sun, 09 Oct 2011 19:12:17 +0000</pubDate>
		<dc:creator>matteo</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[essay]]></category>

		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=645</guid>
		<description><![CDATA[I&#8217;d like to share this quote from E.W.Dijkstra: When the design of the THE Multiprogramming System neared its completion the University&#8217;s EL X8 was getting installed, but it had not been paid yet, and we hardly had access to it because the manufacturer had to put it to the disposal of an American software house [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d like to share this quote from E.W.Dijkstra:</p>
<blockquote><p>
When the design of the THE Multiprogramming System neared its completion the University&#8217;s EL X8 was getting installed, but it had not been paid yet, and we hardly had access to it because the manufacturer had to put it to the disposal of an American software house that was supposed to write a COBOL implementation for the EL X8. They were program testing all the time, and we let it be known that if occasionally we could have the virgin machine for a few minutes, we would appreciate it. They were nice guys, and a few times per week we would get an opportunity for our next test run. We would enter the machine room with a small roll of punched paper tape, and a few minutes later we would leave the machine room with the output we wanted. I remember it vividly because when they realized what we were achieving, our minimal usage of the machine became more and more frustrating for them. I don&#8217;t think their COBOL implementation was ever completed.<br />
<cite><a href="http://www.cs.utexas.edu/~EWD/index13xx.html">EWD1303</a>,  <em>My recollections of operating systems design</em></cite>
</p></blockquote>
<p>I can picture Dijkstra and his collegues working with paper and blackboards and thinking hard about how they were writing their software.  They came into the room and their software <strong>just worked</strong>.  And I can picture the Cobol crew in a furious vicious circle of code-and-fix; their growing frustration and despair. That was about 1960: no books on software design existed back then. For that matter, no books on compiler writing existed either.  </p>
<p>Could the Cobol crew have done better? Absolutely. In 1954, the FORTRAN team led by <a href="http://en.wikipedia.org/wiki/John_Backus">John Backus</a> produced a compiler that reportedly wrote code almost as good as hand-written.  How could the poor Cobol crew have done something as good or at least good enough?</p>
<p>My answer: by <em>designing</em> their software. By breaking the thing into parts (modules) and developing each one separately. By using analogies from other engineering disciplines; by using metaphors.  I know this is all obvious to us in 2011 as we all know about module decomposition and the use of metaphors and coupling and cohesion.  But is it really obvious?  Really?</p>
<p>What I see is that modern-day software teams today can *still* be divided in two kinds: the Dijkstra-crew kind and the Cobol-crew kind.  Those who are in control and produce reasonably good software within reasonable resources and whose code is reasonably clean;  and those who toil away late hours and produce late and buggy software, with no design, or perhaps with an ineffective, we-dont-really-believe-in-it design and crappy code.</p>
<p>Which do you want to be?  What will you do to become it?</p>
]]></content:encoded>
			<wfw:commentRss>http://matteo.vaccari.name/blog/archives/645/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Design by Contract vs. Test-Driven Development</title>
		<link>http://matteo.vaccari.name/blog/archives/602</link>
		<comments>http://matteo.vaccari.name/blog/archives/602#comments</comments>
		<pubDate>Sat, 28 May 2011 09:54:25 +0000</pubDate>
		<dc:creator>matteo</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[essay]]></category>

		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=602</guid>
		<description><![CDATA[A great many years ago I was fascinated by Bertrand Meyer&#8217;s book &#8220;Object Oriented Program Construction.&#8221; One of the many remarkable things in that book is the idea of &#8220;Design By Contract&#8221;, where you specify what a method does by means of a logical pre&#8211; and post&#8211;condicion. Consider the square root function: pre: x &#8805; [...]]]></description>
			<content:encoded><![CDATA[<p>A great many years ago I was fascinated by Bertrand Meyer&#8217;s book &#8220;Object Oriented Program Construction.&#8221;  One of the many remarkable things in that book is the idea of &#8220;Design By Contract&#8221;, where you specify what a method does by means of a logical pre&ndash; and post&ndash;condicion.  Consider the square root function:</p>
<pre>
  pre: x &ge; 0
  post: abs(y*y - x) &lt; epsilon
</pre>
<p>This is a very good specification:</p>
<ul>
<li>It&#8217;s efficiently executable.</li>
<li>The intent is clear.</li>
<li>Gives no hint about how to implement it, i.e., it does not contain design ideas.</li>
</ul>
<p>Now I&#8217;m reading the <a href="http://leansoftwareengineering.com/ksse/scrum-ban/" title="Scrum-ban | Lean Software Engineering">Scrumban</a> book by Corey Ladas.  One thing Corey says is that Test-Driven Development is good, but not as good as Design By Contract; in fact, he says, TDD might be a stepping stone to DBC.  </p>
<p><span id="more-602"></span></p>
<p>I have never met someone who does DBC.  This by itself does not mean a lot, as I&#8217;m not widely travelled, in working experience.  I&#8217;d be quite interested in reading experiences about this.  Anyway I suspect that there are fundamental reasons why it&#8217;s not widely practiced.  My hypothesis is that <strong>the square root is an exceptionally good DBC example, but most functions are not as easily specified by contract</strong>.</p>
<p>Consider for example the function &#8220;specified&#8221; by the following TDD-style tests:</p>
<pre>
  it "returns empty string for empty string" do
    assert_equal "", squeeze("")
  end

  it "replaces sequences of equal characters with one" do
    assert_equal "abca", squeeze("aaabbccccaa")
  end
</pre>
<p>I think it&#8217;s quite clear how we should implement this function, even though we are given just two examples of its behaviour.  How would we specify it with DBC?  I&#8217;m not sure what is the best way.  I thought about this for a long time (believe me) and this is the best I came up with:</p>
<pre>
pre: true // any string is valid input
post:
  let s be the input string.
  let s' the output string.
  (&forall; i: s'[i-1] &ne; s'[i])
  &and;
  (&forall; i,j: i &lt; j &rArr;
    (&exist; k,l: k &lt; l &and; s'[i] = s[k] &and; s'[j] = s[l] ))
  )
</pre>
<p>You read it like this: </p>
<ul>
<li>The output string does not contain consecutive equal values (that was easy!)</li>
<li>Two distinct values in the output string must have been present in the input string and in the same order.</li>
</ul>
<p>Now is this a correct specification?  Can I *prove* that this specification is correct?  Well, this is a specification, so it&#8217;s supposed to be <em>self-evidently correct</em>.  But is it?  I don&#8217;t think it is self evident at all.  I think it takes a bit of thought to understand it.  It&#8217;s not easy to find a way to prove it correct.  One way is to <em>look for counterexamples</em>, that is, find a pair (s,s&#8217;) that satisfies the specification yet contradicts my intuitive notion of what &#8220;squeeze&#8221; should do.  (In fact, I can think of at least two examples that prove that this specification has holes.  Can you find them?)  In other words, it turns out that the only way to convince myself that this specification is correct is by <strong>testing it</strong> on carefully chosen examples!</p>
<p><center><br />
  *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*<br />
  *<br />
</center></p>
<p>Well it&#8217;s not true that I can&#8217;t find a clearer specification.  Consider this other one:</p>
<pre>
  pre: true
  post: let squeeze behave like f where
    f([]) = []
    f([x]) = [x]
    f([x,x|rest]) = f([x|rest])
    f([x,y|rest]) = x + f([y|rest]) if x &ne; y
</pre>
<p>The notation [x,y|rest] means &#8220;a string that begins with character x, then character y, then 0 or more other characters.&#8221;  My observations follow.</p>
<p>This is a purely mathematical specification of function squeeze, as pure as  the previous one.    It&#8217;s arguably clearer than the previous specification.  I think this one is correct.  I could test it against a few cases just to make sure, but it seems OK to me.</p>
<p>This spec can be executed efficiently, while checking an input-output pair against the other spec requires quadratic time.</p>
<p>But there&#8217;s a problem; this spec is actually a program.  Once I have this spec I can use *this* for an implementation and work no more.  I already have my implementation.</p>
<p>In conclusion, this is my objection to DBC: I suspect that the square root is an exception; most methods are too complex to specify with pure logic, or require us to write a functional program that solves the problem.  In both cases we are left with the <strong>need to test our specification</strong> on specific examples.</p>
<p>This is why I think TDD is for most purposes more effective than DBC.  In general, <em>concrete examples</em> are by orders of magnitude simpler to write and more self-evident than universal statements.  Most universal statements will have to be tested against examples anyway, or we wouldn&#8217;t be confident in their correctness.  </p>
<p>Long live examples!</p>
]]></content:encoded>
			<wfw:commentRss>http://matteo.vaccari.name/blog/archives/602/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Anti-FOR tips from the Yahtzee Kata</title>
		<link>http://matteo.vaccari.name/blog/archives/571</link>
		<comments>http://matteo.vaccari.name/blog/archives/571#comments</comments>
		<pubDate>Sat, 14 May 2011 15:47:20 +0000</pubDate>
		<dc:creator>matteo</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Design Exercise]]></category>
		<category><![CDATA[essay]]></category>
		<category><![CDATA[Expressive Code]]></category>

		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=571</guid>
		<description><![CDATA[Again on the Kata Yahtzee, that I blogged about some time ago. If you have not solved the kata at least once, please stop reading this! Get back when you have. *&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;* * Good to see you again! Now that you solved it, you probably know that the naive solution takes many &#8220;for&#8221; loops. Let [...]]]></description>
			<content:encoded><![CDATA[<p>Again on the <a href="http://codingdojo.org/cgi-bin/wiki.pl?KataYahtzee" title="Coding Dojo Wiki: KataYahtzee">Kata Yahtzee</a>, that I <a href="http://matteo.vaccari.name/blog/archives/311" title="Extreme Enthusiasm  &raquo; Blog Archive   &raquo; Report of the first run of the OCP kata">blogged about</a> some time ago.</p>
<p>If you have not solved the kata at least once, please stop reading this!  Get back when you have.</p>
<p><center><br />
  *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*<br />
  *<br />
</center></p>
<p>Good to see you again!  Now that you solved it, you probably know that the naive solution takes many &#8220;for&#8221; loops.  Let <em>D</em> be the player dice, represented as <em>an array of die results</em>, e.g., D=(1,6,1,6,4). The naive rules for sixes would be</p>
<p><code></p>
<pre name="code" class="ruby:nogutter:nocontrols">
    def sixes_score
      sum = 0
      for d in D
        if d == 6
          sum += 6
        end
      end
      return sum
    end
  </pre>
<p></code></p>
<p>This solution involves <em>searching</em> for sixes and adding up.  Why do we need to search?  We need to search because there are many different D that are worth exactly the same for the sixes rule.  For instance, both D=(1,2,3,6,6) and D=(6,6,1,2,3) are worth 12.</p>
<p><span id="more-571"></span></p>
<p>One way to avoid the search is to represent the die rolls in a <em>canonical form</em>, that is a form where two equivalent results are represented in the same way.  The obvious way to obtain a canonical form is to sort D; but in this particular case, we&#8217;d still need to search for sixes.</p>
<p>An alternative canonical form would be to count how many results we have: for instance D=(1,1,3,6,6) would be represented as C = [2,0,1,0,0,2], that is &#8220;two times 1, one time 3, two times 6.&#8221;  The rule for sixes becomes</p>
<p><code></p>
<pre name="code" class="ruby:nogutter:nocontrols">
    def sixes_score
      return 6*C[6]
    end
  </pre>
<p></code></p>
<p>(Note: we take C to be a 1-based array.  It&#8217;s easy to make one in Java or Ruby: use an array of length 7 and ignore index 0.)</p>
<p>The straight rules are also easy, because C is a canonical form for them as well:</p>
<p><code></p>
<pre name="code" class="ruby:nogutter:nocontrols">
    def small_straight_score
      if C == [1,1,1,1,1,0] then 15 else 0
    end
    def large_straight_score
      if C == [0,1,1,1,1,1] then 20 else 0
    end
  </pre>
<p></code></p>
<p>Now, how does this help us with the other rules?  Take the Yahtzee rule, for instance.  The naive solution</p>
<p><code></p>
<pre name="code" class="ruby:nogutter:nocontrols">
    def really_naive_yahtzee_score
      for (i=1; i &lt; 5 ; i++)
        return 0 if D[i-1] &ne; D[i]
      end
      return 50
    end
  </pre>
<p></code></p>
<p>can be slightly improved by </p>
<p><code></p>
<pre name="code" class="ruby:nogutter:nocontrols">
    def slighly_less_naive_yahtzee_score
      for d in D
        return 0 if D[0] &ne; d
      end
      return 50
    end
  </pre>
<p></code></p>
<p>Using C does not improve much as we still have to search:</p>
<p><code></p>
<pre name="code" class="ruby:nogutter:nocontrols">
    def still_naive_yahtzee_score
      for c in C
        return 50 if c == 5
      end
      return 0
    end
  </pre>
<p></code></p>
<p>This is because there are many different C that are equivalent with respect to the Yahtzee rule: for instance C = [0,0,0,5,0] and C=[0,5,0,0,0].  Can we apply the same reasoning and find another canonical representation?  Why yes! If we sort C = [0,0,0,5,0] to obtain S = {5,0,0,0,0} the yahtzee rule becomes very simple:</p>
<p><code></p>
<pre name="code" class="ruby:nogutter:nocontrols">
    def cool_yahtzee_score
      if S[0] == 5 then 50 else 0
    end
  </pre>
<p></code></p>
<p>Many other rules are immediately codified this way:</p>
<p><code></p>
<pre name="code" class="ruby:nogutter:nocontrols">
    def four_of_a_kind_score
      if S[0] &ge; 4 then sum(D) else 0
    end

    def full_house_score
      if S[0] == 3 &and; S[1] == 2 then 25 else 0
    end
  </pre>
<p></code></p>
<p>The pair rule is a bit more challenging: it is not part of the &#8220;official&#8221; rules but it make for interesting coding :o).  The rule is &#8220;Pair: The player scores the sum of the two highest matching dice. For example, 3, 3, 3, 4, 4  gives 8.&#8221;  Using C requires searching.  Using S would be no good (can you see why?)</p>
<p>Again: can you find a canonical representation for the pair rule so that we don&#8217;t have to search?  Hint: remove &#8220;noise&#8221; to reveal information.</p>
<h3>Conclusion</h3>
<p>It&#8217;s important to find <a href="http://www.antiifcampaign.com/" title="The Anti-IF Campaign">ways to remove IFs</a>.  It&#8217;s also important to find ways to remove FORs!  I <a href="http://matteo.vaccari.name/blog/archives/174" title="Extreme Enthusiasm  &raquo; Blog Archive   &raquo; TDD is not finished until the code speaks">blogged about this</a> before.</p>
<p>We used two ways to remove FORs here:</p>
<ol>
<li>Use canonical representations, like C and S;</li>
<li>Hide them in well-known functions like sort and sum.</li>
</ol>
<p>Our search for canonical forms helps us develop a <em>language</em> for reasoning effectively about our problem domain.  In fact we are <a href="http://alistair.cockburn.us/ASD+book+extract%3A+%22Naur,+Ehn,+Musashi%22" title="Programming as Theory Building">building a little <em>theory</em></a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://matteo.vaccari.name/blog/archives/571/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zero is a number</title>
		<link>http://matteo.vaccari.name/blog/archives/438</link>
		<comments>http://matteo.vaccari.name/blog/archives/438#comments</comments>
		<pubDate>Fri, 06 Aug 2010 13:45:05 +0000</pubDate>
		<dc:creator>matteo</dc:creator>
				<category><![CDATA[essay]]></category>
		<category><![CDATA[Fundamentals]]></category>

		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=438</guid>
		<description><![CDATA[I won&#8217;t bore you with the story of how long it took for people to recognize that zero is a number. Without zero it would be difficult to explain what is the value of, say, 3 minus 3; we&#8217;d be forced to say that it&#8217;s a &#8220;meaningless&#8221; expression. Funny huh? Yet some developers seem to [...]]]></description>
			<content:encoded><![CDATA[<p>I won&#8217;t bore you with the story of how long it took for people to recognize that zero is a number.  Without zero it would be difficult to explain what is the value of, say, 3 minus 3; we&#8217;d be forced to say that it&#8217;s a &#8220;meaningless&#8221; expression.  Funny huh?  Yet some developers seem to be stuck to medieval thinking in this respect.  </p>
<p>Have you ever seen code like this?</p>
<p><code></p>
<pre>
public List<Employee> findAllEmployeesByDepartment(int departmentId) {
  String sql = "select * from employees where department_id = ?";
  ResultSet rs = select(sql, department_id);
  if (rs.size() == 0) {
    return null;
  } else {
    // ... convert the recordset to a List and return it
  }
}
</pre>
<p></code></p>
<p>This developer seems to think that an empty List is not a regular list, so he thinks he should return a special value like <code>null</code> to signal that the query returned no values.  This is totally unnecessary.  No, I take it back: this is totally wrong.  You are forcing all callers of <code>findAllEmployeesByDepartment</code> to check for null.  Not only that; this code seem to say that it&#8217;s a totally unnatural and unexpected thing for this query to return no rows.  Soon developers will forget to check for null, and the application will throw <code>NullPointerException</code>s.</p>
<p>A related example is:<br />
<code></p>
<pre>
Foo[] foos = ...;
if (foos.length &gt; 0) {
  for (int i=0; i &lt; foos.length; i++) {
    // do something with foo[i]
  }
}
</pre>
<p></code></p>
<p>Here the developer thinks that they have to treat the case of an empty array separately.  In fact the IF is totally unnecessary.  If the array is empty, the loop would execute zero times anyway.  Java (and C) arrays use asymmetric bounds, which make it easier to write code that does not need to treat a zero-size interval as a special case.</p>
<p>In conclusion: empty collections are perfectly valid collections, and empty arrays are perfectly valid arrays.  It&#8217;s a good idea to write code that doesn&#8217;t treat &#8220;zero&#8221; as a special case.  </p>
<p>This post is part of a series on <a href="/blog/archives/category/fundamentals" title="Extreme Enthusiasm   &raquo; Fundamentals">development fundamentals</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://matteo.vaccari.name/blog/archives/438/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>TDD is no substitute for knowing what you are doing</title>
		<link>http://matteo.vaccari.name/blog/archives/416</link>
		<comments>http://matteo.vaccari.name/blog/archives/416#comments</comments>
		<pubDate>Tue, 29 Jun 2010 11:26:53 +0000</pubDate>
		<dc:creator>matteo</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[essay]]></category>
		<category><![CDATA[Expressive Code]]></category>

		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=416</guid>
		<description><![CDATA[Know your stuff A while ago we had a fun evening at the Milano XPUG writing a Sudoku solver. I blogged about my solution. I&#8217;m not particularly proud of it, in retrospect. The code and the tests are not obvious. I can&#8217;t read any of it and be certain that it works. It does not [...]]]></description>
			<content:encoded><![CDATA[<h4>Know your stuff</h4>
<p>A while ago we had a fun evening at the <a href="http://milano-xpug.pbworks.com/" title="milano-xpug / FrontPage">Milano XPUG</a> writing a Sudoku solver.  I blogged about <a href="http://matteo.vaccari.name/blog/archives/43" title="Extreme Enthusiasm  &raquo; Blog Archive   &raquo; Sudoku anch&#8217;io">my solution</a>.  I&#8217;m not particularly proud of it, in retrospect.  The code and the tests are not obvious.  I can&#8217;t read any of it and be certain that it works.  It does not speak.</p>
<p>It is true that solving puzzles like Sudoku is quite different from what application programmers do everyday at work.  Why is it that?  The problems that we solve in business applications do not have that mathematical crispness that puzzles have.  Perhaps it&#8217;s because we&#8217;re not good enough at analyzing them and expressing them abstractly.  That would explain why business code is so long, convoluted and expensive.</p>
<p>Anyway, the point I want to make is that it is not satisfying to use the tests in TDD as a crutch for constructing hapazard code that, with a kick here and a few hammer blows there seem to work.  The point of TDD is to *design* code; and a good design shows how and why a solution works.  </p>
<p>I often see people doing katas that involve problems with well-known solutions.  We usually disregard, forget, or ignore the well-known solution! And we keep writing tests and production code until we rig together something that passes the tests.  It&#8217;s painful.  I know.  I too did some of that.</p>
<p>TDD does not work well when we don&#8217;t know what we&#8217;re doing.  Some high-profile XPers failed to ship when TDDing their way with <a href="http://thinkingbox.wordpress.com/2008/09/11/the-consequences-of-shipping/" title="The consequences of shipping &laquo; The thinking box">unfamiliar APIs</a> or <a href="http://ravimohan.blogspot.com/2007/04/learning-from-sudoku-solvers.html" title="One Man Hacking: Learning From Sudoku Solvers">disregarding known solutions</a>.  TDD is no substitute for analyzing a problem, and finding abstractions that make it easy to solve.  TDD without thinking and analyzing and abstracting <em>is not fun!</em>.</p>
<p>It&#8217;s for this reason that there is the XP practice of &#8220;spiking solutions&#8221;, that is, take time to learn how to do something, make experiments, then apply what you learned.  If you know how to do things, you will not spend hours discussing with your pair; you and your pair will grab the keyboard from each other, as <a href="http://cirillosscrapbook.wordpress.com/2008/05/27/mamma-programming/#more-53" title="Mamma Programming &laquo; Cirillo&#039;s Scrapbook">Francesco Cirillo often says</a>.</p>
<h4>A better way</h4>
<p>Consider Sudoku again.  Peter Norvig <a href="http://norvig.com/sudoku.html" title="Solving Every Sudoku Puzzle">solves it</a> in two different ways by using known techniques.  The first solution is depth-first search, which is gueranteed to terminate as the graph of Sudoku states is acyclic.  The other is by constraint propagation.  If I were to do the exercise again, I would try to make the analysis apparent from the code.</p>
<p>Say we want to solve it by depth-first search.  That entails two sub-problems:</p>
<ul>
<li style="list-style-type:none">a) writing a depth-first algorithm</li>
<li style="list-style-type:none">b) writing something that enumerates legal moves in a given Sudoku board</li>
</ul>
<p>I would start by testing the depth-first search algorithm.  I would drive it with an abstract &#8220;tree&#8221; implementation.  This means I could concentrate on the search algorithm without being distracted by the complex details of the Sudoku rules.  </p>
<p>Then I would test-drive the generation of next-moves from a Sudoku position.  That could also be done incrementally.  Can we imagine a simplified Sudoku board?  The full Sudoku is too complex for the first test!  A year ago I would have started by defining a 9 by 9 array of numbers, but now the sheer boredom of defining it would stop me.  Is there a better way?  </p>
<p>Relax.  Think.  Dream.</p>
<p>Think about the game terminology.  As <a href="http://norvig.com/sudoku.html" title="Solving Every Sudoku Puzzle">Norvig says</a>, the game is about <em>units</em> (either a row, a column or a box).  A unit with no empty spaces has no descendant moves.  A unit where a number is missing has the full unit as a descendant move.  A unit where two numbers are missing&#8230; You get the point.</p>
<p>Then work out how to combine descendant moves of two units that share a square.  Think a row and a column.  If the common square is empty, than valid solutions for that square must be valid for both units&#8230;  </p>
<p>The point is to work the problem incrementally.  Try smaller scales first.  Try 2&#215;2 boards.  Make the size of units and the board parametric.  Add the constraint rules one by one, so that you can test them separately.</p>
<h4>Conclusions</h4>
<p>One important principle to apply is &#8220;separation of concerns&#8221;.  Enumerating moves is a problem, and search strategy is another.  By solving them separately, our tests become more clear and to the point.  We gain confidence that we know how and why our code works.</p>
<p>Another way to see this is to decompose a problem in smaller problems; prove with tests that you can solve the subproblems, then prove with tests that you can solve the composition of the subproblems.</p>
<p>When you have a problem that is of fixed size 42, turn that constant into a parameter and solve the problem for N=1, N=2, &#8230; Imagine if the Sudoku board was 100&#215;100 instead of 9&#215;9; would you define a 100&#215;100 matrix in your tests?  Turning these constants into parameters make your code more general, your tests more clear, while making the problem *easier* to solve!</p>
<p>To summarize, what I think is important is</p>
<ul>
<li>Learn data structures, algorithms, known solutions, the proper way of doing things.</li>
<li>Apply separation of concerns.  </li>
<li>Solving a slightly more general problem sometimes is much easier than solving the actual problem</li>
<li>It&#8217;s more fun to work when you know what you&#8217;re doing!</li>
</ul>
<h4 style="margin-top: 6em">Update</h4>
<p>Charlie Poole <a href="http://tech.groups.yahoo.com/group/testdrivendevelopment/message/33042" title="Yahoo! Groups">recently posted this</a> on the TDD mailing list (Emphasis is mine):</p>
<blockquote><p>
I&#8217;ve written elsewhere that I believe attempting to get TDD to &#8220;drive&#8221; the invention of a new algorithm reflects an incorrect understanding of what TDD is for.</p>
<p>TDD allows us to express intent (i.e. design) in a testable manner and to move from intention to implementation very smoothly &#8211; I know of no better way.</p>
<p>OTOH, you <em><strong>have to start out with an intent</strong></em>. In this context, I think that means you need to have some idea of the algorithm you want to implement. TDD will help you implement it and even refine the details of the idea. Writing tests may also inspire you to have further ideas, to deepen the ones you started with or to abandon ideas that are not working out.
</p></blockquote>
<p>Vlad Levin <a href="http://vladimirlevin.blogspot.com/2007/04/tdd-is-not-algorithm-generator.html" title="Vlad's Agile Software Development Blog: TDD Is Not An Algorithm Generator!">blogs thoughtfully</a>:</p>
<blockquote>
<p>  one of the first rules I teach my students when I am doing a TDD workshop or teaching a course is precisely that <strong>TDD is not an algorithm generator</strong>! Solving sudoku is just the kind of problem you want to find an algorithm for first, then implement that algorithm</p>
<p>  [...]</p>
<p>  So what is the purpose of TDD then? One goal of TDD is to reduce the need to determine ahead of time which classes and methods you&#8217;re going to implement for an entire story. There&#8217;s a large body of shared experience in the developer community that trying to anticipate such things tends to lead to paralysis where nothing useful gets done and/or produces bloated, over-designed code. Instead, you can develop one aspect of the story at a time, using each test to keep yourself moving forward and refactoring the design as you go along
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://matteo.vaccari.name/blog/archives/416/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A semi-forgotten design principle</title>
		<link>http://matteo.vaccari.name/blog/archives/182</link>
		<comments>http://matteo.vaccari.name/blog/archives/182#comments</comments>
		<pubDate>Fri, 11 Sep 2009 08:41:36 +0000</pubDate>
		<dc:creator>matteo</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[essay]]></category>

		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=182</guid>
		<description><![CDATA[The common wisdom is that Ruby is slow and Java is fast. In general, it&#8217;s true. But is it always? Look at this simple test. $ cat hello.rb puts "Hello world!" $ ruby hello.rb Hello world! $ time ruby hello.rb Hello world! real 0m0.008s user 0m0.004s sys 0m0.003s $ So it looks like it takes [...]]]></description>
			<content:encoded><![CDATA[<p>The common wisdom is that Ruby is slow and Java is fast.  In general, it&#8217;s true.  But is it always?  Look at this simple test.<br />
<code>
<pre>
$ cat hello.rb
puts "Hello world!"
$ ruby hello.rb
Hello world!
$ time ruby hello.rb
Hello world!

real	0m0.008s
user	0m0.004s
sys	0m0.003s
$
</pre>
<p></code><br />
So it looks like it takes 8ms to run a simple &#8220;hello, world&#8221; in Ruby.  How does Java compare to this?</p>
<p><code>
<pre>
$ cat Hello.java
public class Hello {
    public static void main(String ... args) {
        System.out.println("Hello, world!");
    }
}
$
$ javac Hello.java
$ java Hello
Hello, world!
$ time java Hello
Hello, world!

real	0m0.122s
user	0m0.061s
sys	0m0.028s
$
</pre>
<p></code><br />
Even if we ignore the time it takes to <em>compile</em> the Java program, it looks like running the &#8220;Hello, world&#8221; in Java takes <strong>15 times longer</strong> than Ruby.  This is due to the long startup time of the Java Virtual Machine.  The times you see here are taken on my MacBook Pro; they will be different on other operating systems, but not much different. </p>
<p>So what, you will say?  &#8220;The startup time is not important!  As soon as the JVM is up and running, Java can run circles around Ruby.&#8221;  </p>
<p>I don&#8217;t agree that startup times are not important.  The startup time for Java becomes much worse when you run complex applications.  A vanilla Tomcat with no web applications installed takes about <strong>one minute</strong> to start up.  Compare with Webrick, the Ruby web server, that is up and running with my web application in <strong>3&nbsp;seconds</strong>.  The difference in startup times makes all the difference in the world when you&#8217;re developing software.  It takes at least one minute, often much longer, to start up a Java application so that I can try it.  There are times when you&#8217;re developing an application when you need to test it after each tiny change.  It&#8217;s very difficult to do that in Java.  The problem is made much worse by the fact that in general Java &#8220;containers&#8221; can&#8217;t reload changed classes without a restart.  (Webrick can do that.)</p>
<p>What, you will say?  &#8220;Matteo gave up TDD!  He tests applications manually by clicking around like a monkey!&#8221;  No, really, it&#8217;s not like this.  I always write production code with TDD.  That does not mean that you *never* test your stuff manually on the live application.  Quite the opposite: there is a danger, with new converts to unit testing, that we trust our tests too much.  I&#8217;ve seen people declare a story &#8220;finished&#8221; when all the unit tests are green, without ever checking if it <strong>really works</strong>!  And of course, if you never test it manually, it will not work.  There is a need for manual testing (some call it exploratory testing), even if you&#8217;re Kent Beck or Misko Every.</p>
<p>So I hope you&#8217;ll agree with me that short startup times are important for developers.  But there are other implications.  The fact that it takes a lot of time to startup a Java application means that Java developers are trained to <strong>write applications in a single JVM process</strong>.  For instance, we often see dozens of web applications running in a single Tomcat.  If you need concurrent operations, the solution is always to run more threads within the same process.  And there is a big problem with this.</p>
<p>The operating system&#8217;s concept of a &#8220;process&#8221; is a very useful one.  A &#8220;process&#8221; is a bundle of threads and resources: memory, open files, network connections, and the like.  A process in Unix or Windows is a watertight compartment.  When a process terminates, *all* of its resources are released.  A process cannot easily corrupt the state of another process.  A process can be given limits on how much memory or CPU it can take.  It&#8217;s very useful to organize a concurrent application as a set of cooperating operating system processes.  That&#8217;s the way the Apache Http server works, and that is a remarkably reliable software.  It&#8217;s also one of the smart ideas in the Chrome browser, to run each tab in a separate process.</p>
<p>It&#8217;s a good design principle to have many small modules communicating with well-defined interfaces, rather than a single monolith where all the threads can interact in unforeseen ways.  It&#8217;s also the way of Unix to design applications as collections of small communicating processes.  Which makes me wonder how could Sun ever get us to believe that it&#8217;s a good idea to put all of our eggs in a single, huge process.  Should not Sun be the champion of the Unix way?  But I digress.</p>
<p>In conclusion, I claim that designing applications with small cooperating operating system processes is a good principle.  Java current practice runs against this, but it need not be.</p>
]]></content:encoded>
			<wfw:commentRss>http://matteo.vaccari.name/blog/archives/182/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TDD is not finished until the code speaks</title>
		<link>http://matteo.vaccari.name/blog/archives/174</link>
		<comments>http://matteo.vaccari.name/blog/archives/174#comments</comments>
		<pubDate>Sat, 25 Apr 2009 21:55:32 +0000</pubDate>
		<dc:creator>matteo</dc:creator>
				<category><![CDATA[essay]]></category>
		<category><![CDATA[Expressive Code]]></category>

		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=174</guid>
		<description><![CDATA[A problem, and solutions that don&#8217;t seem right I recently asked a few people to solve a little programming problem. In this problem, a number of towns are connected by one-way roads that have different distances. The programmer must write code that answers questions such as: What is the distance of the path A-B-C-D? How [...]]]></description>
			<content:encoded><![CDATA[<h3>A problem, and solutions that don&#8217;t seem right</h3>
<p>I recently asked a few people to solve a little programming problem. In this problem, a number of towns are connected by one-way roads that have different distances. The programmer must write code that answers questions such as:</p>
<ul>
<li>What is the distance of the path A-B-C-D?</li>
<li>How many paths are there from A to C that are exactly 4 steps long?</li>
<li>What is the distance of the shortest path from A to D?</li>
</ul>
<p><img src="http://matteo.vaccari.name/blog/wp-content/uploads/2009/04/graph1.png" alt="The graph"  class="alignnone size-full wp-image-175" /></a></p>
<p>I reviewed three different solutions; they were all valid, reasonably well written. Two had proper unit tests, while the third was checked by prints in a &quot;main&quot;. The authors tried hard to write a &quot;good&quot; solution. There were no long methods; all the logic was broken down in small methods. And yet, I was not pleased with the results. </p>
<p><span id="more-174"></span></p>
<p>The common pattern in all three solution was the presence of an object named RouteFinder, or similar. Other objects were Town, or Path, but they were data structures with little behaviour; in other words, anemic objects. When I tried to understand the implementation logic, all I could see was loops, with many variations of the following:</p>
<p><code class='java'>
<pre>
private void calculateRoutesWithMaximumStops(List<Route> routes, int stops) {
    int stop = 0;
    while (stop < stops) {
        for (Route route : getUniqueRoutes(routes)) {
            routes.addAll(cloneRoute(route));
        }
        stop++;
    }
}
</pre>
<p></code></p>
<p>The problem is that this code does not speak about the problem domain. It's clear that the author took pains to extract methods, to reduce the complexity of the code; but in fact all he did was to decompose a procedure in subprocedures. That is not bad in itself; but the resulting code is <em>not readable</em>. Dude, where's my <strong>domain</strong>?</p>
<h3>My solution</h3>
<p>To me, readable code must speak about the domain. It must allow me to express the questions above directly in the code. Yes, I know this is a lofty goal, but how would I go about solving this problem, you may ask? Well, I did a bit of thinking on it, and I came up with the following.</p>
<p>Suppose we have an object that represents a set of paths. Let's call it a PathSet. I want to start with the set of 1-step paths.</p>
<p><code class='java'>
<pre>
PathSet oneStepPaths = new PathSet()
  .withPath("A", "C", 10)
  .withPath("B", "D", 1)
  .withPath("C", "A", 6)
  .withPath("D", "A", 2)
  .withPath("E", "C", 3)
  .withPath("D", "A", 2)
  .withPath("E", "D", 10)
  .withPath("B", "C", 4)
  .withPath("A", "B", 6)
  .withPath("A", "E", 3)
  ;
</pre>
<p></code></p>
<p>We can define an operation to restrict a PathSet by start or by destination:</p>
<p><code class='java'>
<pre>
PathSet startingFromA = oneStepPaths.from("A");
  // [AC, AB, AE]
PathSet goingToC = oneStepPaths.to("C");
  // [AC, EC, BC]
</pre>
<p></code></p>
<p>That was just looping through all the paths in the set, filtering all those that don't satisfy the property of &quot;starting with A&quot;, or &quot;ending with C&quot;. A more interesting operation is to take two paths into a longer one: for instance, composing &quot;AB&quot; (length 6) with BC (length 4), we get path ABC (length 10). This operation is only defined when the second path starts with the last town of the first path.</p>
<p>We can &quot;lift&quot; the composition of two paths to the composition of pathSets by taking all possible compositions of one path from the first set with a path from the second set:</p>
<p><code class='java'>
<pre>
PathSet twoStepPaths = oneStepPaths.compose(oneStepPaths);
// [ABC, ABD, ACA, AEC, AED, BCA, BDA, CAB, ...
</pre>
<p></code></p>
<p>This composition can be iterated one more time to obtain the set of all three-steps paths. The next step (sorry) is to define a generalized operation to compose a set with itself <em>n</em> times.</p>
<p><code class='java'>
<pre>
PathSet fourStepPaths = oneStepPaths.exp(4);
// same as oneStepPaths.compose(oneStepPaths).compose(oneStepPaths)
//                     .compose(oneStepPaths);
// [ABCAB, ABCAC, ..., EDAEC, EDAED]
</pre>
<p></code></p>
<p>Now it's easy to answer the question &quot;how many paths of 4 steps are there from A to C: </p>
<p><code class='java'>
<pre>
PathSet fourStepTripsFrom_A_to_C = oneStepPaths.exp(4).from("A").to("C");
</pre>
<p></code></p>
<p>To answer questions about the shortest path, we need to define another operation, that takes the union of two PathSets. This will be the subject of another story.</p>
<h3>Discussion</h3>
<p>It seems to me this is a key characteristic of a good object-oriented domain: it defines objects that can be composed together in various ways, to express questions or statements about the problem domain. </p>
<p>They say a domain model should be <em>rich</em>, in the sense that objects should have behaviour attached to them. We should avoid service objects that contain all the behaviour (those are usually called something like FooHandler or FooManager), and anemic objects with data but no behaviour. I think this guidance is correct, but it's not enough; the mark of a successful domain model is when you can compose objects together in useful ways. I would call that an <strong>expressive</strong> domain model.</p>
<p>Eric Evans led a renaissance of true object-oriented thinking with his book <em>Domain Driven Design</em>. This book recalled the programmer's attention on the key importance of objects as a way to model the problem domain. Too much attention is wasted on infrastructure, which also benefits from being programmed with objects, but should not be the central focus of development.</p>
<p>So Evans' book manages to set an important goal in front of the developers; but I think it does not give enough guidance on how to reach it. It's interesting to look into the example library that Evans developed; it's the &quot;Time and Money&quot; library. This library allows the developer to express <em>in code</em> things like &quot;'thanksgiving' is celebrated on the fourth Thursday in November; when is the next occurrence?&quot;. </p>
<p><code>
<pre>
public void testDeriveThanksgiving() {
    //Calculate Thanksgiving, the 4th Thursday in November, for the
    // year 2005
    DateSpecification thanksgiving
        = DateSpecification.nthOccuranceOfWeekdayInMonth(
                11, Calendar.THURSDAY, 4);
    // With the specification, you can do checks like
    assertTrue(thanksgiving.isSatisfiedBy(CalendarDate.date(2005, 11, 24)));
    assertFalse(thanksgiving.isSatisfiedBy(CalendarDate.date(2005, 11, 25)));
    // Derive the date(s) for an interval
    assertEquals(CalendarDate.date(2005, 11, 24),
        thanksgiving.firstOccurrenceIn(CalendarInterval.year(2005)));
...
}
</pre>
<p></code></p>
<p>This code shows the same kind of expressiveness that I think is the mark of a good object-oriented domain. </p>
<p>This library deals with a kind of subject (calendar and dates) that is relevant for, I think, the majority of applications. Yet few calendrical APIs are as expressive as this one. Why is it so? The current thinking in agile circles is that you get to a good design incrementally, by applying TDD and refactoring. The value of TDD is that it helps you to stop and think about your code. Thinking is certainly a valuable thing. But it's not enough to sit and ponder, if you don't know where to direct your thoughts. If you don't have a rich enough vocabulary of design elements in your head that you can mull over.</p>
<p>In fact, TDD could be a crutch that allows you to give up finding a good design and settle for a mediocre one. The tests make sure your code does what it needs to do. You apply some refactoring, removing the obvious smells. Then you can move on and code the next feature. Don't get me wrong: I have the highest regard for code that works; and it's even more impressive when it does not show common smells like &quot;long method&quot; and &quot;bad choice of names&quot;. </p>
<p>But it may be not enough. I want my code (and my team) to be great. I want my velocity to increase over time, as long as the kind of features that the customer wants are similar to old ones. This can <em>only</em> happen if the concepts I built into the domain enable me to code up features in less code; this is when my domain model evolves toward a <em>domain specific language</em> that talks about the problem domain.</p>
<h3>How do I get there?</h3>
<p>This is all good, you will say, but how do I do that? Where is the inspiration, the examples, the procedure I should follow, the book I should read? I don't know. For the moment, let me explain the thinking that went into my solution of the problem above. </p>
<p>You know that functions are a very important idea in mathematics. A function is a <em>correspondence</em> between elements of a set (the <em>domain</em>) to elements of another set (the <em>range</em>). For instance, this is how we say that <em>f</em> is a function from domain <em>X</em> to range <em>Y</em></p>
<div style='padding-left: 2em'>
<p><em>f</em>: <em>X</em> &rarr; <em>Y</em></p>
</div>
<p>The key property of a function is that for every input, there is exactly one output; given <em>x</em> in <em>X</em>, you get <em>f</em>(<em>x</em>), which exists and is unique. If you relax the restriction that <em>f</em>(<em>x</em>) exists and is unique, what you get is a <em>binary relation</em>. A binary relation associates any element of the domain with zero, one, or more than one elements of the range.</p>
<p>We can view it as a set of pairs (<em>x</em>,<em>y</em>) where <em>x</em> is in <em>X</em> and <em>y</em> is in <em>Y</em>. We say that a relation is a subset of the cartesian product of <em>X</em> and <em>Y</em>. </p>
<div style='padding-left: 2em'>
<p><em>R</em> &sube; <em>X</em> &times; <em>Y</em></p>
</div>
<p>If (<em>x</em>,<em>y</em>) is in <em>R</em>, we write <em>x</em>(<em>R</em>)<em>y</em> or even <em>xRy</em> for short. We can't use the <em>f</em>(<em>x</em>) notation anymore, because it implies that <em>f</em> transforms <em>x</em> in some unique element. </p>
<p>Now binary relations are a very useful thing to know, because they come up in many different problems. For instance, our PathSet looks a lot like a binary relation: it tells me which towns are reachable from any given town. </p>
<p>And binary relations are nice because they come with a number of useful <em>operations</em>. Relational composition, for instance. When I compose two relations <em>R</em> and <em>S</em> what I get is a new relation <em>R</em>&#x2218;<em>S</em>. Now, if you want to know if <em>x</em>(<em>R</em>&#x2218;<em>S</em>)<em>y</em>, you must find some intermediate element <em>z</em> such that <em>xRz</em> and <em>zSy</em>. If there is one, then it's true that <em>x</em>(<em>R</em>&#x2218;<em>S</em>)<em>y</em>. </p>
<p>This <em>composition</em> operation is very similar to the composition that we defined above: remember, the operation returned the PathSet with all the paths that could be composed, that is the paths where the destination town of the first is the same as the start town of the second. There are many other useful operations defined on relations: two other operations that I used are domain restriction and range restriction, which are called &quot;from&quot; and &quot;to&quot; above.</p>
<p>The PathSet is a bit more complex than a binary relation, because every element has a numeric distance associated. But it's easy to extend the definition of relational composition to take care of that. We just say that the distance of each composed path is the sum of the distances of the two paths it's composed from.</p>
<p>So this is what I was thinking while I was designing the code to solve this problem <em>right</em>. I remembered that whenever you have something that looks like a graph, you can see it as a binary relation. The advantage of seeing it that way is that relations have a language of standard operations. This language is what I was looking for: some way to define an <em>expressive language</em> for the problem domain.</p>
<h3>Conclusion</h3>
<p>When I was a Ph.D. student, I <a href="/tesi.pdf">spent a lot of time</a> learning and doing mathematics for <em>program construction</em>. The idea was to calculate programs from specifications, much like we calculate the solution for an equation. When I got back to working as a professional programmer, I found I could not use much of what I learned. It was too complicated, too difficult. It's true that all the things I learned made me a better programmer, but that is mostly because I learned and a greater sense of beauty, precision and economy in programming. </p>
<p>Lately I found that while the proof techniques I used were too cumbersome, the <em>language</em> of mathematics itself could be a what is needed to write domain models that are really <em>expressive</em>. A key ingredient for me is that the domain objects should be composable with useful operations. Extracting methods and giving them a nice name is good, but it's not enough if the method I get is not a generally useful operation.</p>
<p>Stay tuned, let's see where this train of thought leads. For the moment, gentle reader, your comments are most welcome.</p>
<h3>References</h3>
<p>An easy introduction to binary relations is in <a href="http://www.usingz.com/text/online/page-83.html">chapter 7</a> of <em>Using Z</em> by Woodcock and Davies.</p>
]]></content:encoded>
			<wfw:commentRss>http://matteo.vaccari.name/blog/archives/174/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Another look at the anti-IF campaign</title>
		<link>http://matteo.vaccari.name/blog/archives/127</link>
		<comments>http://matteo.vaccari.name/blog/archives/127#comments</comments>
		<pubDate>Fri, 19 Sep 2008 11:16:48 +0000</pubDate>
		<dc:creator>matteo</dc:creator>
				<category><![CDATA[essay]]></category>

		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=127</guid>
		<description><![CDATA[Last year, the Italian XP pioneer Francesco Cirillo launched his famous &#8220;anti-IF&#8221; campaign. The idea is bring down the complexity of programs by using object-oriented design; the most important trick in that bag is to replace case analysis by polymorphism. For instance, to replace if (shape.type == SQUARE) area = shape.getSide() * shape.getSide(); else if [...]]]></description>
			<content:encoded><![CDATA[<p>Last year, the Italian XP pioneer Francesco Cirillo launched his famous <a href="http://www.metodiagili.it/campagna-anti-if.html">&#8220;anti-IF&#8221; campaign</a>.  The idea is bring down the complexity of programs by using object-oriented design; the most important trick in that bag is to replace case analysis by polymorphism.  For instance, to replace</p>
<pre>
  if (shape.type == SQUARE)
    area = shape.getSide() * shape.getSide();
  else if (shape.type == TRIANGLE)
    area = (shape.getBase() * shape.getHeight())/2.0;
  else if (shape.type == CIRCLE)
    ...
</pre>
<p>by the much nicer</p>
<pre>
  area = shape.getArea();
</pre>
<p>There are many other useful tricks from OO design.  But I&#8217;d like to talk about another set of tricks for eliminating IFs, namely the bag of tricks of <em>algorithm design</em>.</p>
<p><!--MORE--></p>
<h4>Boolean values are values too.</h4>
<p>One simple trick is to replace</p>
<pre>
  if (foo and bar or baz)
    return true;
  else
    return false;
</pre>
<p>by</p>
<pre>
  return foo and bar or baz;
</pre>
<p>Now, some people prefer the first form, because they prefer operational reasoning over reasoning over boolean values (see the <a href="http://matteo.vaccari.name/blog/archives/50">comments to a previous post on this subject</a>).  I much prefer the <del datetime="2008-09-19T16:08:58+00:00">first</del>second, for one thing because it&#8217;s shorter. Concision is power.  And I try to avoid operational reasoning as much as I can.  If we push operational reasoning to the extreme we get:</p>
<pre>
  if (foo) {
    if (bar) {
      return true;
    }
  }
  if (baz) {
    return true;
  } else {
    return false;
  }
</pre>
<p>This <em>should</em> be equivalent to the previous two examples.  But it&#8217;s not immediately obvious that it is.  It requires much more <em>thinking</em>, and thinking is something I must save for important things.</p>
<h4>Encapsulate IFs in well-known functions</h4>
<p>Suppose you must find the maximum value of two numbers.  Please don&#8217;t write</p>
<pre>
  if (x > y)
    return x;
  return y;
</pre>
<p>It&#8217;s much better to write</p>
<pre>
  return max(x, y);
</pre>
<p>Try to extend these two examples to the maximum of <em>three</em> numbers and see which one turns out clearer.  The fact is that while we stay in the realm of <em>expressions</em>, we can exploit many nice properties, such that <code>max</code> is associative; in the realm of IFs it&#8217;s much more difficult to reason.  You must simulate execution in your head, and that takes energy away from you; energy you might spend on other, more important problems.</p>
<p>Another example: replace </p>
<pre>
  if (x < 0) return -x;
  return x;
</pre>
<p>by</p>
<pre>
  return abs(x);
</pre>
<p>If your language does not provide an <code>abs</code> function, you can define it nicely as <code>max(x, -x)</code>.</p>
<h4>Zero is a perfectly good number</h4>
<p>This code returns the sum of the elements of an array.  </p>
<pre>
// ugly and wrong
int arraySum(int[] array) {
  int sum = array[0];
  for (int i=1; i < array.length; i++) {
    sum += array[i];
  }
  return sum;
}
</pre>
<p>Can you spot the error?  </p>
<div align="center">
*<br />*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*
</div>
<p>Yes, it breaks for empty arrays.  If we keep it this way we'll be forced to pull ifs in the calling code, in all places where we call this method:</p>
<pre>
  // very ugly
  if (x.length > 0) {
    s = arraySum(x);
  } else {
    ???
  }
</pre>
<p>This is ugly, and verbose. And it's not entirely clear what should happen when the length is 0.  Should we give up and throw an exception?  It's much better to stop treating 0 elements as an error.  An empty array is a perfectly good array, and the sum of 0 elements is 0.</p>
<pre>
  // still ugly
  int arraySum(int[] array) {
    if (array.length == 0) {
      return 0;
    }
    int sum = array[0];
    for (int i=1; i < array.length; i++) {
      sum += array[i];
    }
    return sum;
  }
</pre>
<p>Now it's correct, but there's no need to treat an empty array as a special case:</p>
<pre>
  // good
  int arraySum(int[] array) {
    int sum = 0;
    for (int i=0; i < array.length; i++) {
      sum += array[i];
    }
    return sum;
  }
</pre>
<p>Now if the array is empty, the loop will be executed 0 times, since <code>0 &lt; 0</code> is false.  The result will be correct for all N &ge; 0.</p>
<p>Whenever you have to apply a binary operator such as "+" to a set of values, you should ask yourself what should be the answer for 0 elements.  If the binary operator has a unit element, that is an element that leaves the other operand unchanged:</p>
<pre>
  0 + n = n + 0 = n, for all n
  1 * n = n * 1 = n, for all n
  max(&#8722;&#8734;, n) = max(n, &#8722;&#8734;) = n, for all n
</pre>
<p>the result of applying the operator to a set of 0 operands should be the unit element.  The sum of 0 numbers is 0, the product of 0 numbers is 1, the maximum of 0 numbers is <code>&#8722;&#8734;</code>.  Why is that?  Because it's the only logical answer.  Think about this: if you split array A with 10 elements in two parts, A0 and A1, with 5 elements each, you expect that sumArray(A) be equal to sumArray(A0) + sumArray(A1).  You expect exactly the same result if you split A so that A0 has 3 elements and A1 has 7.  It's only natural to expect the same result if you split A such that A0 has 0 elements.  </p>
]]></content:encoded>
			<wfw:commentRss>http://matteo.vaccari.name/blog/archives/127/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Calcolare gli assegnamenti</title>
		<link>http://matteo.vaccari.name/blog/archives/126</link>
		<comments>http://matteo.vaccari.name/blog/archives/126#comments</comments>
		<pubDate>Thu, 18 Sep 2008 09:49:36 +0000</pubDate>
		<dc:creator>matteo</dc:creator>
				<category><![CDATA[essay]]></category>

		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=126</guid>
		<description><![CDATA[Ieri sera alla riunione del Milano XP User Group abbiamo parlato di come &#8220;calcolare&#8221; gli assegnamenti, ovvero, come fare in modo che uno statement conservi un invariante e allo stesso tempo faccia progresso verso la fine del loop. Ad esempio, supponiamo di voler calcolare una tabella di quadrati, con la limitazione che non possiamo usare [...]]]></description>
			<content:encoded><![CDATA[<p>Ieri sera alla riunione del <a href="http://milano-xpug.pbwiki.com">Milano XP User Group</a> abbiamo parlato di come &#8220;calcolare&#8221; gli assegnamenti, ovvero, come fare in modo che uno statement conservi un invariante e allo stesso tempo faccia progresso verso la fine del loop.  Ad esempio, supponiamo di voler calcolare una tabella di quadrati, con la limitazione che non possiamo usare la moltiplicazione.  (Perché no? Magari perché sul nostro processore la moltiplicazione è lenta).  Abbiamo due variabili s e n che soddisfano l&#8217;invariante s = n<sup>2</sup>.  Vogliamo incrementare n, e conservare l&#8217;invariante.  In altre parole, vogliamo trovare un&#8217;espressione E che soddisfi</p>
<p>  { s = n<sup>2</sup> } s, n := E, n+1 { s = n<sup>2</sup> }</p>
<p>In pratica questa è un&#8217;equazione in cui l&#8217;incognita E è l&#8217;espressione che manca per completare il programma </p>
<pre>
  while n != N do
     s, n := E, n+1
     println "il quadrato di " n " è " s
  end
</pre>
<p>L&#8217;obiettivo è essere in grado di calcolare l&#8217;espressione E che mi serve, senza doverci &#8220;pensare&#8221;.  Qual&#8217;è allora la procedura?  Per dimostrare </p>
<p>   { P } x := E { Q }</p>
<p>devo dimostrare che P implica Q[x:=E], dove Q[x:=E] significa sostituire x con E all&#8217;interno di Q.  Nel nostro caso:</p>
<p>&nbsp;&nbsp;  s = n<sup>2</sup>[s, n := E, n+1]<br />
sse  // applico la sostituzione<br />
&nbsp;&nbsp;   E = (n+1)<sup>2</sup><br />
sse  // svolgo il quadrato<br />
&nbsp;&nbsp;   E = n<sup>2</sup> + 2n + 1<br />
sse  // sfrutto l&#8217;invariante<br />
&nbsp;&nbsp;   E = s + 2n + 1<br />
sse  // le moltiplicazioni sono vietate<br />
&nbsp;&nbsp;   E = s + n + n + 1</p>
<p>Voilà: ho dimostrato che s=n<sup>2</sup> implica s=n<sup>2</sup>[s, n := E, n+1] se scelgo E=s+n+n+1.  Il programma desiderato è dunque</p>
<pre>
  while n != N do
     s, n := s+n+n+1, n+1
     println "il quadrato di " n " è " s
  end
</pre>
<p>Per saperne di più:</p>
<ul>
<li>Owen Astrachan, <a href="http://www.cs.duke.edu/~ola/papers/invariant.pdf">Pictures as Invariants</a></li>
<li>Roland Backhouse, <a href="http://www.freetechbooks.com/algorithmic-problem-solving-t373.html">Algorithmic Problem Solving</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://matteo.vaccari.name/blog/archives/126/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

