<?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>Mark Needham &#187; Reading Code</title>
	<atom:link href="http://www.markhneedham.com/blog/category/reading-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markhneedham.com/blog</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Mon, 13 Feb 2012 21:25:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Reading Code: boilerpipe</title>
		<link>http://www.markhneedham.com/blog/2012/02/13/reading-code-boilerpipe/</link>
		<comments>http://www.markhneedham.com/blog/2012/02/13/reading-code-boilerpipe/#comments</comments>
		<pubDate>Mon, 13 Feb 2012 21:16:24 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Reading Code]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=4002</guid>
		<description><![CDATA[I&#8217;m a big fan of the iPad application Flipboard, especially it&#8217;s ability to filter out the non important content on web pages and just show me the main content so I&#8217;ve been looking around at open source libraries which provide that facility. I came across a quora page where someone had asked how this was [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a big fan of the iPad application <a href="http://flipboard.com/">Flipboard</a>, especially it&#8217;s ability to filter out the non important content on web pages and just show me the main content so I&#8217;ve been looking around at open source libraries which provide that facility.</p>
<p>I came across <a href="http://www.quora.com/How-do-Read-It-Later-Instapaper-Flipboard-etc-extract-articles-from-a-page">a quora page where someone had asked how this was done</a> and the suggested libraries were <a href="http://code.google.com/p/arc90labs-readability/downloads/detail?name=readability.js&#038;can=2&#038;q=">readability</a>, <a href="https://github.com/jiminoc/goose">Goose</a> and <a href="http://code.google.com/p/boilerpipe/">boilerpipe</a>. </p>
<p>boilerpipe was written by Christian Kohlschütter and has a corresponding <a href="http://www.l3s.de/~kohlschuetter/boilerplate/">paper</a> and <a href="http://videolectures.net/wsdm2010_kohlschutter_bdu/">video</a> as well.</p>
<p>At a very high level this is my understanding of what the code is doing:</p>
<div align="left">
<img src="http://www.markhneedham.com/blog/wp-content/uploads/2012/02/boilerpipe_highlevel.gif" alt="Boilerpipe highlevel" title="boilerpipe_highlevel.gif" border="0" width="467" height="58" />
</div>
<p>It is based around a pipes/filters architectural style whereby a <cite>TextDocument</cite> is passed through filters which perform  transformations on it. After they&#8217;ve all been applied we can retrieve the main content of the article via a method call.</p>
<p>I&#8217;ve used the pipes/filters approach when playing around with clojure/F# but the problems I was working on were much smaller than this. </p>
<p>In the code there around about 7 or 8 fields being manipulated so I did sometimes find it difficult to know how fields could end up with certain values which often involved looking at other filters and seeing what they did to the document.</p>
<p>I always thought it should be possible to <strong>view each filter completely independently but when there&#8217;s state manipulation involved that doesn&#8217;t seem to be the case</strong>. </p>
<p>Luckily Christian has comments in his code which explain how you might compose the different filters again and why certain filters don&#8217;t make sense on their own, only if they&#8217;re combined with others.</p>
<p>For example the <cite>BlockProximityFusion</cite> class, which is used to merge together adjacent text blocks, contains the following comment:</p>
<blockquote><p>
Fuses adjacent blocks if their distance (in blocks) does not exceed a certain limit. This probably makes sense only in cases where an upstream filter already has removed some blocks.
</p></blockquote>
<p>I suppose the same thing could also have been achieved with some automated tests showing scenarios where different filters are composed.</p>
<p>Christian makes use of the logical OR (&#8220;|&#8221;) operator throughout the code base to ensure that all the filters get executed even if a previous one has successfully made changes to the document.</p>
<p>For example the main entry point into the code is <cite>ArticleExtractor</cite> which reads 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;">final</span> <span style="color: #000000; font-weight: bold;">class</span> ArticleExtractor <span style="color: #000000; font-weight: bold;">extends</span> ExtractorBase <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> ArticleExtractor INSTANCE <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArticleExtractor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> ArticleExtractor getInstance<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> INSTANCE<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;">boolean</span> process<span style="color: #009900;">&#40;</span>TextDocument doc<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> BoilerpipeProcessingException <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> TerminatingBlocksFinder.<span style="color: #006633;">INSTANCE</span>.<span style="color: #006633;">process</span><span style="color: #009900;">&#40;</span>doc<span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">|</span> <span style="color: #000000; font-weight: bold;">new</span> DocumentTitleMatchClassifier<span style="color: #009900;">&#40;</span>doc.<span style="color: #006633;">getTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">process</span><span style="color: #009900;">&#40;</span>doc<span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">|</span> NumWordsRulesClassifier.<span style="color: #006633;">INSTANCE</span>.<span style="color: #006633;">process</span><span style="color: #009900;">&#40;</span>doc<span style="color: #009900;">&#41;</span>
                <span style="color: #666666; font-style: italic;">// cut for brevity</span>
                <span style="color: #339933;">|</span> ExpandTitleToContentFilter.<span style="color: #006633;">INSTANCE</span>.<span style="color: #006633;">process</span><span style="color: #009900;">&#40;</span>doc<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>I <a href="http://www.markhneedham.com/blog/2010/03/28/reading-code-underscore-js/">noticed a similar thing in the underscore.js code</a> but in that case the &#8216;&#038;&#038;&#8217; operator was used to execute code on the right hand side only if the expression on the left had been successful.</p>
<p>If we&#8217;re not using any libraries that simulate first class collections in Java (<a href="http://code.google.com/p/totallylazy/">totallylazy</a>/<a href="http://code.google.com/p/guava-libraries/">Guava</a> for example) then something like this could also work:</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;">final</span> <span style="color: #000000; font-weight: bold;">class</span> ArticleExtractor <span style="color: #000000; font-weight: bold;">extends</span> ExtractorBase <span style="color: #009900;">&#123;</span>
    ...
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> process<span style="color: #009900;">&#40;</span>TextDocument doc<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> BoilerpipeProcessingException <span style="color: #009900;">&#123;</span>
        List<span style="color: #339933;">&lt;</span>BoilerpipeFilter<span style="color: #339933;">&gt;</span> filters <span style="color: #339933;">=</span> asList<span style="color: #009900;">&#40;</span>TerminatingBlocksFinder.<span style="color: #006633;">INSTANCE</span>, <span style="color: #000000; font-weight: bold;">new</span> DocumentTitleMatchClassifier<span style="color: #009900;">&#40;</span>doc.<span style="color: #006633;">getTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>, ExpandTitleToContentFilter.<span style="color: #006633;">INSTANCE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">boolean</span> result <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>BoilerpipeFilter filter <span style="color: #339933;">:</span> filters<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            result <span style="color: #339933;">=</span> result <span style="color: #339933;">|</span> filter.<span style="color: #006633;">process</span><span style="color: #009900;">&#40;</span>doc<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;">return</span> result<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I originally started just browsing the code and thought I roughly understood it before realising I couldn&#8217;t explain what it actually did. I therefore changed my approach and started <a href="https://github.com/mneedham/boilerpipe">writing some unit tests around it</a> to see what the current behaviour was.</p>
<p>From what I can tell the main algorithm in the code is contained inside <cite>NumWordsRulesClassifier</cite> where each text block in the document is classified as being either content or non content.</p>
<p>I wrote tests covering all the scenarios in this class and then refactored the code to see if I could make it a bit more expressive. I ended up with <a href="https://github.com/mneedham/boilerpipe/blob/master/src/main/de/l3s/boilerpipe/filters/english/NumWordsRulesClassifier.java">this</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> currentBlockHasContent<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> TextBlock prev, <span style="color: #000000; font-weight: bold;">final</span> TextBlock curr, <span style="color: #000000; font-weight: bold;">final</span> TextBlock next<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fewLinksInCurrentBlock<span style="color: #009900;">&#40;</span>curr<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;">if</span> <span style="color: #009900;">&#40;</span>fewLinksInPreviousBlock<span style="color: #009900;">&#40;</span>prev<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;">return</span> curr.<span style="color: #006633;">getNumWords</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">16</span> <span style="color: #339933;">||</span> next.<span style="color: #006633;">getNumWords</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">15</span> <span style="color: #339933;">||</span> prev.<span style="color: #006633;">getNumWords</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> curr.<span style="color: #006633;">getNumWords</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">40</span> <span style="color: #339933;">||</span> next.<span style="color: #006633;">getNumWords</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">17</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> fewLinksInCurrentBlock<span style="color: #009900;">&#40;</span>TextBlock curr<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> curr.<span style="color: #006633;">getLinkDensity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">0.333333</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> fewLinksInPreviousBlock<span style="color: #009900;">&#40;</span>TextBlock prev<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> prev.<span style="color: #006633;">getLinkDensity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">0.555556</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The logic is all based around examining the text blocks immediately before and after the current one to work out whether or not it&#8217;s likely to be boiler plate content. </p>
<p>The logic around the next/previous text blocks is written quite imperatively and feels like it could be made more concise by using something like <a href="http://www.markhneedham.com/blog/2010/01/14/f-refactoring-to-sequencefor-expressions/">F#&#8217;s &#8216;Seq.windowed&#8217; over the collection</a> but I can&#8217;t quite see how at the moment!</p>
<p>You can read more about the algorithm on pages 4-7 of <a href="http://www.l3s.de/~kohlschuetter/publications/wsdm187-kohlschuetter.pdf">the paper</a>.</p>
<p>From running the code against a few articles I&#8217;ve got saved to <a href="http://readitlaterlist.com/l">ReadItLater</a> it does seem to work reasonably well. </p>
<h4>Overall&#8230;</h4>
<p>I haven&#8217;t read every single bit of the code base but from what I have read I think boilerpipe is a pretty cool library and the approach to filtering content is neat. </p>
<p>I found it especially useful to be able to read parts of the paper and then go and look at the corresponding code. Often that type of thing remains up to the imagination of the reader from my experience!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2012/02/13/reading-code-boilerpipe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading Code: Know what you&#8217;re looking for</title>
		<link>http://www.markhneedham.com/blog/2011/12/29/reading-code-know-what-youre-looking-for/</link>
		<comments>http://www.markhneedham.com/blog/2011/12/29/reading-code-know-what-youre-looking-for/#comments</comments>
		<pubDate>Thu, 29 Dec 2011 02:43:34 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Reading Code]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3865</guid>
		<description><![CDATA[In the last week or so before Christmas I got the chance to spend some time pairing with my colleague Alex Harin while trying to understand how an existing application which we were investigating was written. We knew from watching a demo of the application that the user was able to send some processing off [...]]]></description>
			<content:encoded><![CDATA[<p>In the last week or so before Christmas I got the chance to spend some time pairing with my colleague <a href="http://www.linkedin.com/pub/alex-harin/13/40b/716">Alex Harin</a> while trying to understand how an existing application which we were investigating was written.</p>
<p>We knew from watching a demo of the application that the user was able to send some processing off to be done in the background and that they would be emailed once that had happened.</p>
<p>Our starting point was therefore to work backwards from the labels on the UI and finding which code got executed when the user submitted the task.</p>
<p>My initial approach was to find the entry point and then follow the method calls line by line, slowly building my knowledge of how the application actually worked.</p>
<p>Alex used a much quicker approach whereby he <strong>thought about how the code would be designed</strong> and then looked for the bit of code which proved his belief.</p>
<p>In this case we knew that the application had a 2 tier architecture and that it didn&#8217;t use any middleware which meant that it would more than likely be using the database as a queue to store the tasks to be processed.</p>
<p>Another colleague and I were then able to make use of this approach when looking at another piece of code.</p>
<p>It was being used to do pricing and we knew that there were different algorithms depending on which country you were in, which meant that we needed to look for anything country related to answer our questions.</p>
<p>Effectively we&#8217;re looking at the code with a hypothesis in hand and then trying to see whether or not what we see proves or disproves that hypotheses.</p>
<p>I need to practice a bit more but it seems to let you navigate code much more quickly and makes you much less likely to dive down a rabbit hole.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/12/29/reading-code-know-what-youre-looking-for/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Reading Code: underscore.js</title>
		<link>http://www.markhneedham.com/blog/2010/03/28/reading-code-underscore-js/</link>
		<comments>http://www.markhneedham.com/blog/2010/03/28/reading-code-underscore-js/#comments</comments>
		<pubDate>Sun, 28 Mar 2010 20:02:10 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Reading Code]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2302</guid>
		<description><![CDATA[I&#8217;ve been spending a bit of time reading through the source code of underscore.js, a JavaScript library that provides lots of functional programming support which my colleague Dave Yeung pointed out to me after reading my post about building a small application with node.js. I&#8217;m still getting used to the way that JavaScript libraries are [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been spending a bit of time reading through the source code of <a href="http://documentcloud.github.com/underscore">underscore.js</a>, a JavaScript library that provides lots of functional programming support which my colleague Dave Yeung pointed out to me after reading <a href="http://www.markhneedham.com/blog/2010/03/21/node-js-a-little-application-with-twitter-couchdb/">my post about building a small application with node.js</a>.</p>
<p>I&#8217;m still getting used to the way that JavaScript libraries are written but these were some of the interesting things that I got from reading the code:</p>
<ul>
<li>There are a couple of places in the code where the author has some <strong>code which runs conditionally and this is achieved by including that expression on the right hand side of an &#8216;&#038;&#038;&#8217;</strong>.
<p>For example on line 129 in the &#8216;filter&#8217; function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>123
124
125
126
127
128
129
130
131
132
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// Return all the elements that pass a truth test.</span>
  <span style="color: #006600; font-style: italic;">// Delegates to JavaScript 1.6's native filter if available.</span>
  _.<span style="color: #660066;">filter</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> iterator<span style="color: #339933;">,</span> context<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>nativeFilter <span style="color: #339933;">&amp;&amp;</span> obj.<span style="color: #660066;">filter</span> <span style="color: #339933;">===</span> nativeFilter<span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> obj.<span style="color: #660066;">filter</span><span style="color: #009900;">&#40;</span>iterator<span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> results <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    each<span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>value<span style="color: #339933;">,</span> index<span style="color: #339933;">,</span> list<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      iterator.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>context<span style="color: #339933;">,</span> value<span style="color: #339933;">,</span> index<span style="color: #339933;">,</span> list<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> results.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> results<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>I would probably have used an if statement to check the result from calling &#8216;iterator&#8217; but this way is more concise and pretty neat.</p>
<p>The same type of thing is done on line 150 in the &#8216;every&#8217; function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>145
146
147
148
149
150
151
152
153
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">  _.<span style="color: #660066;">every</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> iterator<span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    iterator <span style="color: #339933;">=</span> iterator <span style="color: #339933;">||</span> _.<span style="color: #660066;">identity</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>nativeEvery <span style="color: #339933;">&amp;&amp;</span> obj.<span style="color: #660066;">every</span> <span style="color: #339933;">===</span> nativeEvery<span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> obj.<span style="color: #660066;">every</span><span style="color: #009900;">&#40;</span>iterator<span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    each<span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>value<span style="color: #339933;">,</span> index<span style="color: #339933;">,</span> list<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><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>result <span style="color: #339933;">=</span> result <span style="color: #339933;">&amp;&amp;</span> iterator.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>context<span style="color: #339933;">,</span> value<span style="color: #339933;">,</span> index<span style="color: #339933;">,</span> list<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> _.<span style="color: #660066;">breakLoop</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;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The result is collected and the loop will also exit if the value of &#8216;result&#8217; is ever false which is again a cool way to organise code.
</li>
<li>
It&#8217;s also quite cool that you can <strong>assign a value to a variable from within a conditional</strong> &#8211; this isn&#8217;t possible in any of the other languages that I&#8217;ve used previously as far as I&#8217;m aware.</p>
<p>It&#8217;s even more evident in the &#8216;max&#8217; function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>191
192
193
194
195
196
197
198
199
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">  _.<span style="color: #660066;">max</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> iterator<span style="color: #339933;">,</span> context<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><span style="color: #339933;">!</span>iterator <span style="color: #339933;">&amp;&amp;</span> _.<span style="color: #660066;">isArray</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> Math.<span style="color: #660066;">max</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>Math<span style="color: #339933;">,</span> obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>computed <span style="color: #339933;">:</span> <span style="color: #339933;">-</span>Infinity<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    each<span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>value<span style="color: #339933;">,</span> index<span style="color: #339933;">,</span> list<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> computed <span style="color: #339933;">=</span> iterator <span style="color: #339933;">?</span> iterator.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>context<span style="color: #339933;">,</span> value<span style="color: #339933;">,</span> index<span style="color: #339933;">,</span> list<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> value<span style="color: #339933;">;</span>
      computed <span style="color: #339933;">&gt;=</span> result.<span style="color: #660066;">computed</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>result <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>value <span style="color: #339933;">:</span> value<span style="color: #339933;">,</span> computed <span style="color: #339933;">:</span> computed<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;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> result.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&#8216;result&#8217; is conditionally assigned on line 196 but only if the computed value is greater than the current computed value. Again an if statement is avoided.</p>
<p>Another interesting thing about this function is that it specifically checks the type of the &#8216;obj&#8217; passed in which reminded me about the <a href="http://blog.obiefernandez.com/content/2009/04/my-reasoned-response-about-scala-at-twitter.html">discussion around Twitter having those sorts of checks in their Ruby code around a year ago</a>. I guess that type of thing would be more prevalent in library code than in an application though.
</li>
<li>I hadn&#8217;t come across the <strong>!! construct</strong> which is used to turn a JavaScript expression into its boolean equivalent:

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>506
507
508
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">  _.<span style="color: #660066;">isArray</span> <span style="color: #339933;">=</span> nativeIsArray <span style="color: #339933;">||</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">!!</span><span style="color: #009900;">&#40;</span>obj <span style="color: #339933;">&amp;&amp;</span> obj.<span style="color: #660066;">concat</span> <span style="color: #339933;">&amp;&amp;</span> obj.<span style="color: #660066;">unshift</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>obj.<span style="color: #660066;">callee</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Without using &#8216;!!&#8221; the expression would return &#8216;undefined&#8217; in the case that one of those functions on &#8216;obj&#8217; was not set. &#8216;!!&#8217; forces the return value to be &#8216;false&#8217;.
</li>
<li>Another technique used in this code base is that of <strong>dynamically adding methods to the prototype of an object</strong>:

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>666
667
668
669
670
671
672
673
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// Add all mutator Array functions to the wrapper.</span>
  each<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'pop'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'push'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'reverse'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'shift'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'sort'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'splice'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'unshift'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> method <span style="color: #339933;">=</span> ArrayProto<span style="color: #009900;">&#91;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    wrapper.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</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>
      method.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._wrapped<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span> result<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._wrapped<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>._chain<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: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This is quite a cool use of meta programming although it isn&#8217;t initially obvious how those functions end up on the object unless you know what to look for. It does significantly reduce the code needed to add these functions to the &#8216;wrapper&#8217; object&#8217;s prototype, avoiding the &#8216;<a href="http://www.ibm.com/developerworks/java/library/j-eaed9/index.html">same whitespace, different values</a>&#8216; problem that Neal Ford outlines in his article about harvesting idiomatic patterns in our code.
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/03/28/reading-code-underscore-js/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Reading Code: Unity</title>
		<link>http://www.markhneedham.com/blog/2009/11/04/reading-code-unity/</link>
		<comments>http://www.markhneedham.com/blog/2009/11/04/reading-code-unity/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 15:22:56 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Reading Code]]></category>
		<category><![CDATA[unity]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1785</guid>
		<description><![CDATA[I spent a bit of time reading some of the Unity code base recently and I decided to try out a variation of Michael Feathers &#8216;Effect Sketching&#8217; which my colleague Dave Cameron showed me. &#8216;Effect Sketching&#8217; is a technique Feathers describes in &#8216;Working Effectively With Legacy Code&#8216; and the idea is that we sketch a [...]]]></description>
			<content:encoded><![CDATA[<p>I spent a bit of time reading some of the <a href="http://www.codeplex.com/unity/">Unity</a> code base recently and I decided to try out a variation of Michael Feathers &#8216;Effect Sketching&#8217; which my colleague <a href="http://intwoplacesatonce.com/">Dave Cameron</a> showed me.</p>
<p>&#8216;Effect Sketching&#8217; is a technique Feathers describes in &#8216;<a href="http://www.amazon.co.uk/Working-Effectively-Legacy-Robert-Martin/dp/0131177052/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1256852443&#038;sr=8-1">Working Effectively With Legacy Code</a>&#8216; and the idea is that we  sketch a diagram showing the interactions between the fields and methods in a specific class while browsing through the code.</p>
<p>Dave suggested doing a similar thing but with methods and classes instead while stepping through the code with the debugger.</p>
<p>I set up this code to step my way through:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var container <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UnityContainer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
container<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewExtension</span><span style="color: #008000;">&lt;</span>Interception<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
container<span style="color: #008000;">.</span><span style="color: #0000FF;">RegisterType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span> <span style="color: #008000;">&#40;</span>IIDProvider<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span> <span style="color: #008000;">&#40;</span>HttpContextIDProvider<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
container<span style="color: #008000;">.</span><span style="color: #0000FF;">Configure</span><span style="color: #008000;">&lt;</span>Interception<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SetDefaultInterceptorFor</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span> <span style="color: #008000;">&#40;</span>GetPaymentBreakdownsService<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">new</span> VirtualMethodInterceptor<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">object</span> resolve <span style="color: #008000;">=</span> container<span style="color: #008000;">.</span><span style="color: #0000FF;">Resolve</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span> <span style="color: #008000;">&#40;</span>GetPaymentBreakdownsService<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>These are some of my observations from this exercise:</p>
<ul>
<li>I found it was <strong>much easier for me to remember where I was in the call stack</strong> then normal.
<p>I often get quite engrossed in the individual method calls on objects that I forget where the code actually started before it ended up in the current location. Whenever this happened I was able to refer to my sketch to remind myself of where the code had started from. </li>
<li>Despite having the drawing I still got a bit lost when the <a href="http://unity.codeplex.com/sourcecontrol/changeset/view/38085?projectName=unity#616462">PolicyInjectionBehaviourDescriptor</a> made a call back to the container&#8217;s Resolve method which meant that the code went through the same path that I&#8217;d just followed:

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> IInterceptionBehavior GetInterceptionBehavior<span style="color: #008000;">&#40;</span>
            IInterceptor interceptor,
            Type interceptedType,
            Type implementationType,
            IUnityContainer container<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            InjectionPolicy<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> policies <span style="color: #008000;">=</span> container<span style="color: #008000;">.</span><span style="color: #0000FF;">Resolve</span><span style="color: #008000;">&lt;</span>InjectionPolicy<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>I was getting really confused watching various different injection policies being resolved instead of the type I was trying to resolve!</li>
<li>I&#8217;ve previously tried drawing diagrams which just contained the classes in a code base but I&#8217;ve found that including the methods that connect them makes it easier for me to understand what&#8217;s going on.
<p>I&#8217;ve been drawing these diagrams out by hand but I thought I&#8217;d translate some of it into dot notation so that I could create a version using <a href="http://www.graphviz.org/">graphviz</a> to show on here.</p>
<p><img src="http://www.markhneedham.com/blog/wp-content/uploads/2009/11/unity.png" alt="unity.png" border="0" width="653" height="613" /></p>
<p>The nodes in orange represent classes and the dotted line represents where an event was fired.
</li>
<li>I realised that I still need to spend more time reading code so that I know <strong>when to dive into an object and when the details are probably not important</strong>. At the moment I&#8217;m too prone to getting distracted by wanting to see how a specific method works instead of looking at those details later on when I actually need to know. </li>
<li>I felt as I was reading the code that in a lot of places the <a href="http://www.markhneedham.com/blog/2009/01/02/f-option-types/">option type</a> from functional programming would have come in quite useful. There is often code similar to this bit from LifeTimeStrategy:

<div class="wp_syntax"><div class="code"><pre class="class" style="font-family:monospace;">object existing = lifetimePolicy.GetValue();
if (existing != null)
{
    context.Existing = existing;
    context.BuildComplete = true;
}</pre></div></div>

<p>Instead of existing returning a null it could return &#8216;None&#8217; to indicate it hasn&#8217;t been resolved yet.</li>
<li>I&#8217;ve read about the &#8216;<a href="http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx">yield</a>&#8216; construct before but I&#8217;ve never seen a need to use it yet in any code I&#8217;ve written so it was interesting to see it being used quite frequently inside <a href="http://unity.codeplex.com/sourcecontrol/changeset/view/38085?projectName=unity#616459">PolicySet</a>:

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> IEnumerable<span style="color: #008000;">&lt;</span>InjectionPolicy<span style="color: #008000;">&gt;</span> GetPoliciesFor<span style="color: #008000;">&#40;</span>MethodImplementationInfo member<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>InjectionPolicy policy <span style="color: #0600FF; font-weight: bold;">in</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>policy<span style="color: #008000;">.</span><span style="color: #0000FF;">Matches</span><span style="color: #008000;">&#40;</span>member<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> policy<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>From my understanding of this construct it seems like it acts in a similar way to a stream i.e. it&#8217;s only evaluated when it&#8217;s actually needed. </li>
<li>In the &#8216;<a href="http://www.amazon.co.uk/Fundamentals-Object-oriented-Design-Object-Technology/dp/020169946X/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1257261364&#038;sr=8-1">Fundamentals of Object Oriented Design in Uml</a>&#8216; Meilir Page Jones suggests that we might want to avoid replicated behaviour in our public APIs since it leads to greater complexity.
<p>This would therefore seem to suggest that having overloads of methods on an object is something to be avoided. Interestingly in this code base the overloads for &#8216;UnityContainer&#8217; are actually defined as extension methods which then delegate back to &#8216;UnityContainer&#8217; and pass in default values for unspecified parameters. </p>
<p>This seems like quite a neat way of getting around the problem since we keep the API clean while also providing users of the code an easy way to do so.</li>
</ul>
<p>On the subject of reading code I recently came across an <a href="http://designbygravity.wordpress.com/2009/10/23/how-to-read-other-peoples-code-and-why/">interesting post by designbygravity which describes some approaches for reading code more effectively</a>.</p>
<p>In particular I really liked the section about not hating the code :</p>
<blockquote><p>
You can get sucked into hating the code, merely because it is not yours. Software people tend to be equipped with ample egos, and other people’s code can offend. But realize, their working code is better than your imagined code, because their working code exists right now. So put your ego aside and learn the code in front of you.
</p></blockquote>
<p>It&#8217;s so easy to drift into this mindset but it&#8217;s rarely helpful so if we can stop ourselves it&#8217;s almost certainly beneficial.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/11/04/reading-code-unity/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Reading Code: Rhino Mocks</title>
		<link>http://www.markhneedham.com/blog/2009/07/28/reading-code-rhino-mocks/</link>
		<comments>http://www.markhneedham.com/blog/2009/07/28/reading-code-rhino-mocks/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 14:05:11 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Reading Code]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1495</guid>
		<description><![CDATA[I spent a bit of time recently reading through some of the Rhino Mocks to get a basic understanding of how some features work under the hood. As well as just getting some practice at reading unfamiliar code I also wanted to know the following: How does the &#8216;VerifyAllExpectations&#8217; extension method work? What&#8217;s the difference [...]]]></description>
			<content:encoded><![CDATA[<p>I spent a bit of time recently reading through some of the <a href="http://ayende.com/projects/rhino-mocks.aspx">Rhino Mocks</a> to get a basic understanding of how some features work under the hood.</p>
<p>As well as just getting some practice at reading unfamiliar code I also wanted to know the following:</p>
<ul>
<li>How does the &#8216;VerifyAllExpectations&#8217; extension method work?</li>
<li>What&#8217;s the difference between the &#8216;GenerateMock&#8217; and &#8216;GenerateStub&#8217; methods on MockRepository?</li>
<li>How does the &#8216;AssertWasNotCalled&#8217; extension method actually work?</li>
</ul>
<p>These are some of the things I learnt from my exploration:</p>
<ul>
<li>I&#8217;m using a Mac so I originally started out trying to read the code in <a href="http://macromates.com/">TextMate</a> with the <a href="http://forum.unity3d.com/viewtopic.php?t=1601">C# plugin</a> before eventually realising that I couldn&#8217;t really tell the difference between a delegate being passed around the code and a method call so I wanted an editor that would help me out with this.
<p>I decided to try out <a href="http://monodevelop.com/">MonoDevelop</a> to see if I could keep on reading the code outside my VM. Unfortunately I kept making that crash every time I tried to move between classes so back to Visual Studio it was! MonoDevelop looks like quite a nice tool but it just isn&#8217;t for me at the moment.</li>
<li>I&#8217;ve been playing around with an idea adapted from Michael Feathers&#8217; <a href="http://www.amazon.com/Working-Effectively-Legacy-Robert-Martin/dp/0131177052">Working Effectively With Legacy Code</a> by drawing out the different classes and how they interact with each other. Where I could see a grouping between classes I&#8217;ve been drawing that into the diagram as well.
<p><a href="http://twitter.com/madgn0me/statuses/2869130380">Some</a> <a href="http://twitter.com/jagregory/statuses/2869105962">of</a> <a href="http://twitter.com/peter_c_william/statuses/2869092943">the guys</a> on Twitter showed me a cool web based tool at <a href="http://yuml.me/diagram/scruffy/class/draw">yuml.me</a> that allows you to easily draw class diagrams and then grab the png/jpeg file and do whatever you want with it.</p>
<p><img src="http://www.markhneedham.com/blog/wp-content/uploads/2009/07/rhino-mocks.png" alt="rhino-mocks.png" border="0"  /></p>
<p>Although these diagrams are quite simple I find them more useful than I had expected and I&#8217;ve started drawing more diagrams at work to help understand bits of code that I&#8217;m not very familiar with.</p>
<p>I realised a couple of years ago when reading one of <a href="http://www.scotthyoung.com/blog/2006/12/10/using-diagrams/">Scott Young&#8217;s posts about drawing diagrams</a> that I seem to understand ideas more quickly if I&#8217;m able to draw them out so I should probably look to do it more frequently!
</li>
<li>The way that <a href="http://www.markhneedham.com/blog/2009/07/14/test-doubles-my-current-approach/">stubs and mocks</a> are generated is essentially the same which I&#8217;m told is also the case with <a href="http://mockito.org/">Mockito</a> although I haven&#8217;t read Mockito&#8217;s code yet.
<p>&#8216;GenerateMock&#8217; eventually calls this bit of code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> T DynamicMock<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">params</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> argumentsForConstructor<span style="color: #008000;">&#41;</span>
    <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ShouldUseRemotingProxy<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>, argumentsForConstructor<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>RemotingMock<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>, CreateDynamicRecordState<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>CreateMockObject<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>, CreateDynamicRecordState, <span style="color: #008000;">new</span> Type<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span>, argumentsForConstructor<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>&#8216;GenerateStub&#8217; eventually calls this bit of code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> Stub<span style="color: #008000;">&#40;</span>Type type, <span style="color: #0600FF; font-weight: bold;">params</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> argumentsForConstructor<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    CreateMockState createStub <span style="color: #008000;">=</span> mockedObject <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span> StubRecordMockState<span style="color: #008000;">&#40;</span>mockedObject, <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ShouldUseRemotingProxy<span style="color: #008000;">&#40;</span>type, argumentsForConstructor<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> RemotingMock<span style="color: #008000;">&#40;</span>type, createStub<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> CreateMockObject<span style="color: #008000;">&#40;</span>type, createStub, <span style="color: #008000;">new</span> Type<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span>, argumentsForConstructor<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The main difference is that when the &#8216;Verify&#8217; method is called on stubs (which would call the &#8216;StubReplayMockState&#8217; class) we don&#8217;t do anything whereas with mocks a check is done to ensure that all the expectations setup in the test are met. </p>
<p>I found it quite interesting that the new &#8216;Arrange-Act-Assert&#8217; style syntax has been written to make use of the older &#8216;Record-Replay&#8217; syntax which I guess is quite a nice metaphor for the two states that you use the framework. </p>
<p>I haven&#8217;t looked at the <a href="http://code.google.com/p/moq/">Moq</a> code yet but it would be interesting to see how the code for that differs as it was built from the ground up with the &#8216;Arrange-Act-Assert&#8217; syntax.</li>
<li>The &#8216;AssertWasNotCalled&#8217; method works fairly similarly to how I had imagined at a high level in that it goes and gets all the expectations for the mock for that method call and then checks that they aren&#8217;t called although I found the implementation of that first bit quite interesting.

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> ExpectationVerificationInformation GetExpectationsToVerify<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>T mock, Action<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> action, Action<span style="color: #008000;">&lt;</span>IMethodOptions<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&gt;&gt;</span> setupConstraints<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// ...	</span>
	var mockToRecordExpectation <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>mocks<span style="color: #008000;">.</span><span style="color: #0000FF;">DynamicMock</span><span style="color: #008000;">&#40;</span>FindAppropriteType<span style="color: #008000;">&#40;</span>mockedObject<span style="color: #008000;">&#41;</span>, mockedObject<span style="color: #008000;">.</span><span style="color: #0000FF;">ConstructorArguments</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	action<span style="color: #008000;">&#40;</span>mockToRecordExpectation<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">// .. </span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The code actually creates a mock in order to record the expectation that we are checking does not happen before it goes on to check which methods were called against that method. We therefore need to ensure that any calls to &#8216;AssertWasNotCalled&#8217; are made after the call to the system under test otherwise it will always return true which may not be accurate.</li>
<li>I didn&#8217;t realise quite how much code there is in Rhino Mocks and I&#8217;ve only read a very small part of it. A few of the interesting parts of the code seem to be making calls to <a href="http://www.castleproject.org/dynamicproxy/index.html">Castle DynamicProxy</a> which is being used to do the intercepting of method calls.
<p>I&#8217;m never sure what the best way to approach a new code base is but this time I had a couple of starting points that I wanted to investigate and starting from those points seemed to work out quite well. I still find that I struggle to know when to stop digging down into how specific code works and when to just have a general understanding of that bit and move on to the next bit of code. I often find that I click all the way through to deeper methods and then I can&#8217;t remember why I did it in the first place.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/07/28/reading-code-rhino-mocks/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Oxite: Some Thoughts</title>
		<link>http://www.markhneedham.com/blog/2008/12/31/oxite-some-thoughts/</link>
		<comments>http://www.markhneedham.com/blog/2008/12/31/oxite-some-thoughts/#comments</comments>
		<pubDate>Tue, 30 Dec 2008 15:26:37 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Reading Code]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[oxite]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=785</guid>
		<description><![CDATA[The recently released Oxite code base has taken a bit of a hammering in the blogosphere for a variety of reasons &#8211; the general feeling being that it doesn&#8217;t really serve as a particularly good example of an ASP.NET MVC application. I was intrigued to read the code though &#8211; you can always learn something [...]]]></description>
			<content:encoded><![CDATA[<p>The recently released <a href="http://www.codeplex.com/oxite">Oxite</a> code base has taken a <a href="http://www.lostechies.com/blogs/chad_myers/archive/2008/12/20/oxite-review.aspx">bit</a> <a href="http://codebetter.com/blogs/glenn.block/archive/2008/12/19/on-oxite.aspx">of</a> <a href="http://www.hanselman.com/blog/ASPNETMVCSamplesOxiteAndCommunity.aspx">a</a> <a href="http://blog.wekeroad.com/blog/some-thoughts-on-oxite/">hammering</a> in the blogosphere for a variety of reasons &#8211; the general feeling being that it doesn&#8217;t really serve as a particularly good example of an ASP.NET MVC application.</p>
<p>I was intrigued to read the code though &#8211; you can always learn something by doing so and reading code is one of the ares that I want to improve in.</p>
<p>So in a style similar to that of a <a href="http://www.markhneedham.com/blog/2008/11/12/technicalcode-base-retrospective/">Technical Retrospective</a> these are my thoughts.</p>
<h3>Things I like</h3>
<ul>
<li>The <strong>Repository</strong> pattern from <a href="http://domaindrivendesign.org/">Domain Driven Design</a> is used for providing access to the entities and provides encapsulation around the LINQ to SQL code.</li>
<li>There is some quite clever use of extension methods on FormCollection in the AdminController to create Posts and Tags from the data collected from the page. There are others as well &#8211; the developers really seem to grok extension methods in a way that I currently don&#8217;t. It will be interesting to see if I start using them more as I code more with C# 3.0.
</ul>
<h3>Things I&#8217;d change</h3>
<ul>
<li>Testing wise there are some tests covering the MetaWeblogService class although it feels like the setup in each of the tests is a bit heavy. I&#8217;d refactor the tests using <a href="http://www.industriallogic.com/xp/refactoring/composeMethod.html">compose method</a> to try and remove some of the noise and rename the tests so that they follow the <a href="http://www.markhneedham.com/blog/2008/09/04/bdd-style-unit-test-names/">BDD style</a> more closely.</li>
<li>When time is used in the code it is done by directly using the DateTime class. Testing time based code is made much easier when we can control the time in our tests, so have a <a href="http://www.markhneedham.com/blog/2008/09/24/testing-with-joda-time/">Time Provider</a> or System Clock class can be a very useful approach here.</li>
<li>A <a href="http://martinfowler.com/articles/mocksArentStubs.html">classicist</a> approach has been taken towards testing with no mocks in sight! It feels like a lot of work went into creating the fake versions of the classes which could maybe have been done much more easily using a stub in <a href="http://ayende.com/projects/rhino-mocks.aspx">Rhino Mocks</a> for example. </li>
<li>A lot of the ViewData code is weakly typed by putting values into an array. We started with this approach on a project I worked on and it&#8217;s so easy to misspell something somewhere that we came to the conclusion that <strong>if we can get strong typing we should strive for that</strong>. Jeremy Miller&#8217;s <a href="http://codebetter.com/blogs/jeremy.miller/archive/2008/10/23/our-opinions-on-the-asp-net-mvc-introducing-the-thunderdome-principle.aspx">Thunderdome Principle</a> works well here. </li>
<li>AdminController seems to have a massive number of responsibilities which is perhaps not surprising given the name. I think it&#8217;d be better to have the administrative tasks handled from the individual controllers for those tasks. This would also help lead the design towards a more <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm">RESTful</a> approach</li>
<li>The <strong>Domain model is very driven from the definition of the database tables</strong> from my understanding. The domain objects are effectively representations of database tables. If we want to create a richer domain model then it would make more sense to design the classes in terms of what makes sense in the domain rather than what makes sense at a database level. An ORM tool could then be used to map the database tables to the code.</li>
<li>A lot of the <strong>domain objects implement an interface</strong> as well which I don&#8217;t think is really necessary. Although there are exceptions, in general when we refer to domain objects we care about the implementation rather than a contract describing what it does.</li>
<li>The <strong>Routes collection is passed around</strong> quite a few of the controllers, seemingly so that URLs can be generated inside other pages. It seems a bit overkill to pass this around to achieve such a simple goal and my thinking is that maybe a wrapper class which generated the URLs might be more intention revealing.</li>
<li>There is a bit more logic than I&#8217;m comfortable with in some of the views &#8211; I think it would be good to move this logic into ViewModel classes. This will have the added benefit of allowing that logic to be tested more easily.</li>
</ul>
<h3>Want to know more about</h3>
<ul>
<li>I&#8217;m curious as to <strong>why LINQ to SQL is being used</strong> as the interface to the database as I was under the impression that it is being <a href="http://codebetter.com/blogs/david.hayden/archive/2008/10/31/linq-to-sql-is-dead-read-between-the-lines.aspx">phased out</a> by Microsoft. The syntax seemed quite readable but I think the problem of interacting between code and the database in a clean way has been largely solved by <a href="http://www.hibernate.org/343.html">NHibernate</a> although the <a href="http://weblogs.asp.net/fbouma/archive/2008/05/19/why-use-the-entity-framework-yeah-why-exactly.aspx">Entity Framework</a> is a newish addition in this area.</li>
<li>One interesting thing I noticed was a <strong>lot of Background Services</strong> running in this codebase &#8211; I&#8217;ve not come across this in a web application before. They are actually being used for creating trackbacks, sending trackbacks and sending email messages.</li>
</ul>
<h3>My learning from reading the code</h3>
<ul>
<li>I asked for some <a href="http://twitter.com/markhneedham/status/1063734022">advice on the best way to read code</a> on Twitter and the most popular advice was to <strong>debug through the code</strong>. Unfortunately I couldn&#8217;t seem to do this without having a database in place so another approach was necessary. Instead I started reading from the tests that were available and then clicked through to areas of interest from there. I think it worked reasonably well but it wasn&#8217;t as focused as if I had debugged and I couldn&#8217;t see the state of the program as it executed.</li>
<li>I wanted to find a way to read the Oxite code and navigate to areas of the ASP.NET MVC source using <a href="http://www.red-gate.com/products/reflector/">Reflector</a> without having to do so manually. <a href="http://www.testdriven.net/">TestDriven.NET</a> was recommended to me and it worked really well. Clicking the &#8216;Go to Reflector&#8217; option from the menu takes you to the current class in the Reflector window. Impressive.</li>
<li>Changing the Resharper find usages menu to show &#8216;Namespace + Type&#8217; makes it much easier to try and work out what the code is doing rather than the default setting of just &#8216;Namespace&#8217;.</li>
<li>From looking at some of the ASP.NET MVC code I realised that a lot of data is stored in static variables in order to make the data globally accessible. It&#8217;s something I had never considered this before and it makes sense in a way but feels a little nasty</li>
<li>I found that I was getting <strong>side tracked quite a lot</strong> by irrelevant details in the code. I&#8217;m used to having a pair guide me through a new code base so looking at this one alone was a bit different for me. Separating noise/signal when reading code and identifying common patterns to allow me to do this is something I am working on.</li>
</ul>
<h3>Overall</h3>
<p>I think it&#8217;s really cool that the Oxite team put their code out there for people to look at and learn from. A number of highly experienced developers have made suggestions for improvement so clearly this is quite a useful way to get feedback and code better in the future.</p>
<p>From what I understand, Rob Conery is working on some <a href="http://blog.wekeroad.com/blog/oxite-refactor-take-1/">refactorings for this code base</a> so it will be interesting to see what it looks like when this is done. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2008/12/31/oxite-some-thoughts/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

