<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Western Devs</title>
  
  <link href="/feeds/amir_barylko" rel="self" type="application/atom+xml"/>
  <link href="https://westerndevs.com" rel="alternate" type="application/atom+xml"/>
  
  <updated>2025-11-05T18:40:02.086Z</updated>
  <id>https://westerndevs.com/</id>
  
  <author>
    <name>Western Devs</name>
	<uri>https://westerndevs.com</uri>
    <email>info@westerndevs.com</email>
  </author>
  
  <generator uri="http://hexo.io/">Hexo</generator>
  
  <entry>
    <title type="html">Acceptance Testing With Legacy Databases</title>
    <link href="https://westerndevs.com/Testing/Acceptance-Testing-With-Legacy-Databases/" rel="alternate" type="text/html"/>
    <id>https://westerndevs.com/Testing/Acceptance-Testing-With-Legacy-Databases/</id>
    <published>2017-01-22T15:04:33.000Z</published>
    <updated>2025-11-05T18:40:02.086Z</updated>
	<author>
	
	  
	  <name>Amir Barylko</name>
	  <email>amir@barylko.com</email>
	
	  <uri>https://westerndevs.com</uri>
	</author>
    
    <content type="html"><![CDATA[<p>One of the most common <em>pain points</em> of implementing automated acceptance testing is the interaction with the database.</p><p>For greenfield projects you can plan from day one how to setup the test to easily include the database interaction but with legacy projects it is not always that easy.</p><a id="more"></a><h2>Dealing with legacy code</h2><p>Let’s face it: <strong>Testing is hard</strong>.</p><p>I do not mean it is hard to understand. The complexity is not inherently attached to the concept of testing but (I found in most cases) a by-product of <code>tooling + environment + database</code>.</p><p>That is the reason why I think adding testing to a legacy system can be quite challenging. We did not choose the tooling, nor the database nor did we set up the environment to be test friendly.</p><p>So where to start with testing?</p><p>One option is to start by adding <em>unit tests</em> into the codebase, but that could be a paramount effort considering that quite often the legacy code was not written with testability in mind. A lot of change, a lot of risk.</p><p>On the other hand <em>acceptance testing</em> is the perfect candidate.</p><p>Why? <em>Acceptance Testing</em> puts the focus on testing end to end. Given a certain input, run it through the system and make sure the output is what we expect to see.</p><p>It works for web applications, web apis, libraries, desktop applications, you name it. And also, in many cases we will not need to modify the code behaviour at all.</p><p>All that is fine and dandy, but what about the database? We may be able to create a local copy of the database to test, but what are we going to do with data generation, logic stored in the database, etc?</p><h2>A perfect world</h2><p>Let’s pause and imagine for just a few paragraphs that instead of using a database the <em>system under test</em> uses an HTTP API to get all the information it needs.</p><p>If that was the case then we could implement acceptance testing very easily by doing something like the following pseudo algorithm:</p><ul><li>Launch a fake HTTP server listening on the URI expected by the system.</li><li>Create some data that will work for my test case.</li><li>When the application does the call, return that data.</li><li>Validate the case worked as expected.</li><li>Shutdown the server.</li></ul><p>Neat right? This approach has many benefits.</p><p><em>First</em>, we keep modifications of the system under test to the bare minimum.</p><p><em>Second</em>, there are lots of tools in multiple languages that can help us with such a task. We can choose the same environment or one that is completely different. Whichever works better for our needs.</p><p><em>Third</em>, these steps can be easily automated and ran when it is convenient and useful.</p><p>Ok, the break is over.</p><h2>Back to reality</h2><p>To change all the database related code to start using some kind of web API could be a huge risk and effort.</p><p>Such amount of refactoring may cripple your project for a long time, and not even produce a positive result.</p><p>Having said that, what if we use the same idea but with a small twist?.</p><h2>Leave the database code alone</h2><p>Well, not alone alone, but let’s hide it behind a very thin wrapper.</p><p>The goal is that instead of directly hitting the database (or whichever function or class is being used) we are going to call a proxy that is sole job is to forward the call to the same code we were using before.</p><p>The main difference is that the <em>Proxy</em> talks about the domain. If we were fetching some <code>Customer</code> object from the database, then the proxy will have a way to do so and return a <code>Customer</code> collection.</p><p>So the database interaction, <em>ORM</em> mapping, etc, stays hidden.</p><p>To illustrate the idea with a bit of code, let’s imagine a class in charge of finding customers in order to show them:</p><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">CustomersController</span> &#123;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> CustomerView <span class="title">index</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line"></span><br><span class="line">    String query = <span class="string">"select NAME, ADDRESS, BIRTH_DATE from CUSTOMERS"</span>;</span><br><span class="line"></span><br><span class="line">    ResultSet rs = dbConnection.createStatement().executeQuery(query);</span><br><span class="line"></span><br><span class="line">    List&lt;Customer&gt; customers = <span class="keyword">new</span> ArrayList&lt;Customer&gt;();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span>(rs.next()) &#123; customers.<span class="keyword">add</span>(loadCustomer(rs)); &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> CustomerView(customers);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">private</span> Customer <span class="title">loadCustomer</span>(<span class="params">ResultSet rs</span>)</span> &#123; ...... &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>The first step would be to create an <em>interface</em> and abstract the query to the database:</p><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title">CustomersQuery</span> &#123;</span><br><span class="line">  <span class="function">List&lt;Customer&gt; <span class="title">getCustomers</span>(<span class="params"></span>)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>And a default implementation that does the database query:</p><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">DatabaseCustomerQuery</span> <span class="title">implements</span> <span class="title">CustomersQuery</span> &#123;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> List&lt;Customer&gt; <span class="title">getCustomers</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line"></span><br><span class="line">    String query = <span class="string">"select NAME, ADDRESS, BIRTH_DATE from CUSTOMERS"</span>;</span><br><span class="line"></span><br><span class="line">    ResultSet rs = dbConnection.createStatement().executeQuery(query);</span><br><span class="line"></span><br><span class="line">    List&lt;Customer&gt; customers = <span class="keyword">new</span> ArrayList&lt;Customer&gt;();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span>(rs.next()) &#123; customers.<span class="keyword">add</span>(loadCustomer(rs)); &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> customers;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">private</span> Customer <span class="title">loadCustomer</span>(<span class="params">ResultSet rs</span>)</span> &#123; ...... &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>And the original class now it uses the interface:</p><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">CustomersController</span> &#123;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> CustomerView <span class="title">index</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> CustomerView(<span class="keyword">this</span>.customersQuery.getCustomers());</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure><h2>Mock the proxy</h2><p>When testing the system, the library in charge of <em>proxying</em> the interaction to the database could be switched to a different one that does an HTTP call to a URI and returns the result based on the response.</p><p>By using an HTTP call, then the test will pose as the expected source of data and respond based on the needs of each case.</p><p>Following the previous example, we could implement a class that gets the customers data using an HTTP call to the test URI.</p><p>The example uses <a href="https://github.com/FasterXML/jackson" target="_blank" rel="noopener">Jackson</a> to load the json content.</p><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">HttpCustomersQuery</span> <span class="title">implements</span> <span class="title">CustomerQuery</span> &#123;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> List&lt;Customer&gt; <span class="title">getCustomers</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    HttpGet httpGet = <span class="keyword">new</span> HttpGet(testUrl + <span class="string">"/customers"</span>);</span><br><span class="line">    HttpResponse response = httpclient.execute(httpGet);</span><br><span class="line"></span><br><span class="line">    ObjectMapper mapper = <span class="keyword">new</span> ObjectMapper();</span><br><span class="line">    List&lt;Customer&gt; customers = mapper.readValue(response.getEntity().getContent(), <span class="keyword">new</span> TypeReference&lt;List&lt;Customer&gt;&gt;()&#123;&#125;);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> customers;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>The last step, when running the tests we will launch the HTTP server to serve the JSON customers:</p><p>Here I am using <a href="http://wiremock.org/" target="_blank" rel="noopener">WireMock</a> to set up the response.</p><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testCustomersView</span>(<span class="params"></span>)</span>  &#123;</span><br><span class="line"></span><br><span class="line">  List&lt;Customer&gt; expected = createSomeFakeCustomers();</span><br><span class="line"></span><br><span class="line">  String serializedCustomers = JSON.write(expeted);</span><br><span class="line"></span><br><span class="line">  stubFor(<span class="keyword">get</span>(urlEqualTo(<span class="string">"/customers"</span>))</span><br><span class="line">            .willReturn(aResponse()</span><br><span class="line">                .withStatus(<span class="number">200</span>)</span><br><span class="line">                .withHeader(<span class="string">"Content-Type"</span>, <span class="string">"application/json"</span>)</span><br><span class="line">                .withBody(serializedCustomers)));</span><br><span class="line"></span><br><span class="line">  <span class="comment">// run the system under test here</span></span><br><span class="line">  runSystem();</span><br><span class="line"></span><br><span class="line">  List&lt;Customer&gt; actual = getCustomersShown() ; <span class="comment">// get the customers that are being shown</span></span><br><span class="line"></span><br><span class="line">  assertThat(expected, <span class="keyword">is</span>(actual));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3>Why bother with HTTP?</h3><p>We could implement the “fake” version of the library as:</p><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">HttpCustomersQuery</span> <span class="title">implements</span> <span class="title">CustomerQuery</span> &#123;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> List&lt;Customer&gt; <span class="title">getCustomers</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    String jsonContent = loadResourceFrom(<span class="string">"/resources/customers.json"</span>);</span><br><span class="line"></span><br><span class="line">    ObjectMapper mapper = <span class="keyword">new</span> ObjectMapper();</span><br><span class="line">    List&lt;Customer&gt; customers = mapper.readValue(jsonContent, <span class="keyword">new</span> TypeReference&lt;List&lt;Customer&gt;&gt;()&#123;&#125;);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> customers;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>However this approach may limit our ability to separate completely the acceptance test implementation from the system we want to test.</p><p>Having an external server to pose as data source provides flexibility and could simplify quite a bit the test implementation because it gives us the freedom to choose any tool that we may see fit to do the actual implementation.</p><p>This technique could simplify manual testing as well. The test scenario data could be setup, then the system launched and wait for manual confirmation to ensure it works as expected.</p><figure class="highlight gherkin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">Given</span> the customers are loaded             <span class="comment"># All the customers in the JSON file are loaded</span></span><br><span class="line"><span class="keyword">When</span> listing the customers                 <span class="comment"># Launch the system to show the customers</span></span><br><span class="line"><span class="keyword">Then</span> every customer name shows in the list <span class="comment"># Ensure all customers are shown</span></span><br></pre></td></tr></table></figure><h2>Change impact</h2><p>The change will be localized. Modifying a particular functionality of the system does not affect how other parts of the system work nor major refactoring effort is required.</p><p>Of course there will be some code change, but hopefully very small and just to hide the database related code behind a very thin wrapper.</p><p>Once the acceptance tests start to roll, each new test will be easier and easier.</p><p>Not only the system will have a new safety net that becomes larger and larger with every test, but the quality will grow as well.</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;One of the most common &lt;em&gt;pain points&lt;/em&gt; of implementing automated acceptance testing is the interaction with the database.&lt;/p&gt;
&lt;p&gt;For greenfield projects you can plan from day one how to setup the test to easily include the database interaction but with legacy projects it is not always that easy.&lt;/p&gt;
    
    </summary>
    
      <category term="Testing" scheme="https://westerndevs.com/categories/Testing/"/>
    
    
      <category term="Acceptance Testing" scheme="https://westerndevs.com/tags/Acceptance-Testing/"/>
    
      <category term="Unit Testing" scheme="https://westerndevs.com/tags/Unit-Testing/"/>
    
      <category term="Legacy code" scheme="https://westerndevs.com/tags/Legacy-code/"/>
    
      <category term="jackson" scheme="https://westerndevs.com/tags/jackson/"/>
    
      <category term="WireMock" scheme="https://westerndevs.com/tags/WireMock/"/>
    
  </entry>
  
  <entry>
    <title type="html">Maybe null is not an Option</title>
    <link href="https://westerndevs.com/Fsharp/Functional-programming/maybe-null-is-not-an-option/" rel="alternate" type="text/html"/>
    <id>https://westerndevs.com/Fsharp/Functional-programming/maybe-null-is-not-an-option/</id>
    <published>2016-06-09T02:28:25.000Z</published>
    <updated>2025-11-05T18:40:02.084Z</updated>
	<author>
	
	  
	  <name>Amir Barylko</name>
	  <email>amir@barylko.com</email>
	
	  <uri>https://westerndevs.com</uri>
	</author>
    
    <content type="html"><![CDATA[<p><a href="http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare" target="_blank" rel="noopener">Tony Hoare</a> calls <em>null references</em> his <strong>billion dollar mistake</strong>. Using <code>null</code> values (<code>NULL</code>, <code>Null</code>, <code>nil</code>, etc) makes code harder to maintain and to understand.</p><p>But what can we do about it? To start let's review the meaning of <code>null</code> values ...</p><a id="more"></a><h2>Modeling optional results</h2><h3>Finding Customers</h3><p>Finding operations are very common. Given a <code>Customer</code> class, a <code>Find</code> method could look like this:</p><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> Customer <span class="title">Find</span>(<span class="params">Query query</span>)</span> ;</span><br></pre></td></tr></table></figure><p>Now what happens when the customer can not be found? Possible options are:</p><ul><li><p>Throw an exception: Why though? Where is the exceptional case? Trying to find a <code>Customer</code> has the possible scenario of not beign found.</p></li><li><p>Return <code>null</code> to mean nothing was found. But are we positive we are getting a <code>null</code> for that reason and not other? And what can we do with the <code>null</code> value after?</p></li><li><p>Return a <code>NullObject</code> that represents <code>Customer</code> null value. That could work to show some of the customer's data, if we expect strings or something similar. But for most cases this won't be enough.</p></li></ul><h3>Parsing Integers</h3><p>In <em>C#</em> you can use <code>Int32.parse</code> or <code>Int32.tryParse</code> to do the job. The former throws an <code>Exception</code> when the string can not be parsed into an int and the later returns a <code>Bool</code> indicating if the operation succeeded using an <code>out</code> parameter for the value.</p><p>The first approach is not that intuitive. I want to get a result, not to catch an exception.</p><p>The second one with the boolean result seems to go in the right direction but having an <code>out</code> parameter complicates things, and makes it hard to understand and hard to pass to another function, etc.</p><h3>Optional values</h3><p><em>F#</em> has a very simple way to deal with this by creating a <a href="https://fsharpforfunandprofit.com/posts/discriminated-unions/" target="_blank" rel="noopener">Discriminated Union</a> with only two values:</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">type</span> <span class="title">Option</span>&lt;<span class="title">'T</span>&gt; </span>= </span><br><span class="line">  | Some <span class="keyword">of</span> <span class="symbol">'T</span> </span><br><span class="line">  | None</span><br></pre></td></tr></table></figure><p>Now finding customers has a very clear interface:</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> tryFindCustomer (q: query) : Option&lt;Customer&gt;</span><br></pre></td></tr></table></figure><p>Parse clearly can succeed giving the parsed result or fail, therefore the result is <code>Optional</code>.</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> tryParseInt (s:string) =</span><br><span class="line">  <span class="keyword">match</span> Int32.TryParse(s) <span class="keyword">with</span></span><br><span class="line">  | <span class="keyword">true</span>, result -&gt; Some result</span><br><span class="line">  | <span class="keyword">false</span>, _     -&gt; None</span><br></pre></td></tr></table></figure><p>NOTE: out parameters are converted tuples in F#.</p><p>As a convention is common to call functions <code>trySomeAction</code> when the action can fail and return an <em>optional</em> value.</p><h2>Working with optional values</h2><p>Modeling optional values is a great start. Using an optional value makes very clear the fact that the caller has the <em>responsibility</em> to handle the possiblity of having <code>None</code> as a result.</p><p>Having clear meaning improves clarity, intent, error handling, and there is no <code>null</code> that can cause problems.</p><p>However, in terms of handling the result, are we that much better than before?</p><p>Of course we could check always for <code>Some</code> or <code>None</code> and handle the result, but where is the <code>fun</code> in that?</p><p>The key to create a great abstraction is <em>usage</em>. After seeing the same bit of code used again and again we could be confident that <em>abstracting</em> the behaviour is going to be really useful.</p><p>To avoid doing a <code>match</code> on <code>Option</code> types luckily we have a series of helper functions that address most common scenarios.</p><p>Many of these functions live in the <a href="https://github.com/fsprojects/FSharpx.Extras" target="_blank" rel="noopener">FSharpx</a> library.</p><h3>Using defaults</h3><p>To obtain the value from an <code>Option</code> we can use <code>Option.get</code>:</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> get = </span><br><span class="line">  <span class="keyword">function</span></span><br><span class="line">  | Some a -&gt; a</span><br><span class="line">  | None   -&gt; failwith <span class="string">"Nothing to get!"</span></span><br></pre></td></tr></table></figure><p>(what's that <code>function</code>? Here is an <a href="http://fsharpforfunandprofit.com/posts/match-expression/" target="_blank" rel="noopener">explanation about pattern maching function</a>)</p><p>Notice that <code>get</code> throws an exception when there is nothing to get.</p><p>Throwing exception (and catching it) is ok, but makes hard to compose the result or transform it.</p><p>Instead why not use a default value when there is <code>None</code>? (Taken from <a href="https://github.com/fsprojects/FSharpx.Extras/blob/master/src/FSharpx.Extras/ComputationExpressions/Monad.fs" target="_blank" rel="noopener">FSharpx</a>):</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> getOrElse v = </span><br><span class="line">  <span class="keyword">function</span></span><br><span class="line">  | Some a -&gt; a</span><br><span class="line">  | None   -&gt; v</span><br></pre></td></tr></table></figure><p>Knowing that it can fail, and having a default value help us write code that can use a default value and keep going:</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> doWebRequest countStr =</span><br><span class="line">  countStr</span><br><span class="line">  |&gt; tryParse</span><br><span class="line">  |&gt; Option.getOrElse <span class="number">10</span></span><br><span class="line">  |&gt; processRequest</span><br></pre></td></tr></table></figure><h3>Applying functions</h3><p>Another common scenario is to apply a function when we get a result or just do nothing otherwise:</p><p>For example, the following code will only print the message when <code>tryParse</code> returns <code>Some</code>:</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> doWebRequest param =</span><br><span class="line">  param</span><br><span class="line">  |&gt; tryParse</span><br><span class="line">  |&gt; Option.iter (printf <span class="string">"The result is %d"</span>)</span><br></pre></td></tr></table></figure><p>And the implementation:</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> iter f = </span><br><span class="line">  <span class="keyword">function</span></span><br><span class="line">  | Some a -&gt; f a</span><br><span class="line">  | None   -&gt; unit</span><br></pre></td></tr></table></figure><h3>Shortcircuit <code>None</code></h3><p><code>iter</code> is useful to execute a function when there is <code>Some</code> value returned, but is common to use the value somehow and transform it into something else.</p><p>For that we can use the <code>map</code> function:</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> map f = </span><br><span class="line">  <span class="keyword">function</span></span><br><span class="line">  | Some a -&gt; f a |&gt; Some</span><br><span class="line">  | None   -&gt; None</span><br></pre></td></tr></table></figure><p>This is a bit different. Not only the function passed as parameter is applied after <em>unboxing</em> the value, but the result is <em>boxed</em> back into an <code>Option</code>. Once an <code>Option</code> always an <code>Option</code>.</p><p>Imagine a <a href="http://fsharpforfunandprofit.com/rop/" target="_blank" rel="noopener">railway</a> and a train that once hits the value <code>None</code> switches to a <code>None</code> railway and bypasses any operation that comes after. Thus the <em>shortcircuit</em>.</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> queryCustomers quantity = [] <span class="comment">// silly implementation</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> doWebRequest req =</span><br><span class="line">  req.tryGetParam <span class="string">"customers"</span></span><br><span class="line">  |&gt; Option.map queryCustomers         <span class="comment">// Shortcircuit with None</span></span><br><span class="line">  |&gt; Option.getOrElse invalidRequest   <span class="comment">// uses the default</span></span><br></pre></td></tr></table></figure><h3>Mapping to <code>Option</code></h3><p>Another common case, very similar to <code>map</code>, is to use a function that transforms the boxed value and returns an <code>Option</code>.</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> bind f = </span><br><span class="line">  <span class="keyword">function</span></span><br><span class="line">  | Some a -&gt; f a </span><br><span class="line">  | None   -&gt; None</span><br></pre></td></tr></table></figure><p>For example the parameter that represents the <em>customer id</em> is a string, and we need to parse it into an int. Getting the parameter can return a <code>None</code> and the parse function could return a <code>None</code> as well.</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> doWebRequest req =</span><br><span class="line">  req.tryGetParam <span class="string">"customerId"</span></span><br><span class="line">  |&gt; Option.bind Int32.tryParse       <span class="comment">// Shortcircuit if None</span></span><br><span class="line">  |&gt; Option.map  findCUstomer         <span class="comment">// Shortcircuit if None</span></span><br><span class="line">  |&gt; Option.getOrElse invalidRequest</span><br></pre></td></tr></table></figure><h2>Multiple optional values</h2><p>So far so good with one parameter. But what happens with more than one parameter? The goal is to <em>shortcircuit</em> and if one of the parameters is <code>None</code> then abort and just return <code>None</code>.</p><p>One option is to use <em>FSharpx</em> and the <code>MaybeBuilder</code>. I'm not going to discuss the details of how builders work but I will show you the practical usage to illustrate the point.</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">type</span> <span class="title">Result</span>&lt;<span class="title">'TData</span>&gt; </span>= </span><br><span class="line">  | Success <span class="keyword">of</span> 'TData</span><br><span class="line">  | Error <span class="keyword">of</span> string</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> findCustomers count country city = ... <span class="comment">// implement query</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> invalidRequest = <span class="string">"Boooo! Not all parameters are present!"</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> doWebRequest (req:Request) =</span><br><span class="line">  maybe &#123;</span><br><span class="line">    <span class="keyword">let!</span> strCount = req.tryGetParam <span class="string">"count"</span></span><br><span class="line">    <span class="keyword">let!</span> city     = req.tryGetParam <span class="string">"city"</span></span><br><span class="line">    <span class="keyword">let!</span> country  = req.tryGetParam <span class="string">"country"</span></span><br><span class="line">    <span class="keyword">let!</span> count    = Int32.tryParse strCount</span><br><span class="line">    <span class="keyword">return</span> findCustomers count country city</span><br><span class="line">  &#125;</span><br><span class="line">  |&gt; Option.map Success</span><br><span class="line">  |&gt; Option.getOrElse (invalidRequest |&gt; Error)</span><br></pre></td></tr></table></figure><p>In this scenario we have a happy path:</p><ul><li>All parameters are present, then the result is <code>Success</code> with the output of <code>findCustomers</code>.</li></ul><p>And four <em>unhappy</em> paths:</p><ul><li><code>count</code> is not present, then the <code>maybe</code> builder does shortcircuit to <code>None</code> and <code>getOrElse</code> returns an <code>Error</code>.</li><li><code>city</code> is not present, then the <code>maybe</code> builder does shortcircuit to <code>None</code> and <code>getOrElse</code> returns an <code>Error</code>.</li><li><code>country</code> is not present, then the <code>maybe</code> builder does shortcircuit to <code>None</code> and <code>getOrElse</code> returns an <code>Error</code>.</li><li><code>count</code> is present, but can not be parsed then ... shortcircuit ... and <code>Error</code>.</li></ul><p>The <code>let!</code> is doing the <em>unboxing</em> from <code>Option</code> to the actual type, and when any of the expressions has the value <code>None</code> then the builder does the <em>shortcircuit</em> and returns <code>None</code> as result.</p><h2>Applicative Style</h2><p>Another way to write the same concept (sometimes a bit more clear) is to use the <em>Applicative</em> style by using operators that represent the operations that we already are using that also apply shortcircuit when possible.</p><p>For the functions we have used in the <code>Option</code> type the operators are:</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;!&gt; <span class="comment">// is an alias for map</span></span><br><span class="line">&gt;&gt;= <span class="comment">// is an alias for bind</span></span><br></pre></td></tr></table></figure><p><em>(Find all the definitions <a href="https://github.com/fsprojects/FSharpx.Extras/blob/master/src/FSharpx.Extras/ComputationExpressions/Monad.fs" target="_blank" rel="noopener">here</a>)</em>.</p><p>To use them let's try to read the parameters from the request.</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> city    = req.tryGetParam <span class="string">"city"</span></span><br><span class="line"><span class="keyword">let</span> country = req.tryGetParam <span class="string">"country"</span></span><br></pre></td></tr></table></figure><p>Good, now <code>count</code> needs to be parsed as well, so we can use <code>tryParse</code> that returns an <code>Option</code>. What can we use when we need to apply a function that returns also an <code>Option</code>? <code>Bind</code> of course, or <code>&gt;&gt;=</code>.</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> count = req.tryGetParam <span class="string">"count"</span> &gt;&gt;= Int32.tryParse</span><br></pre></td></tr></table></figure><p>All the parameters are parsed into <code>Option</code> and <code>findCustomers</code> can be invoked.</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">findCustomers &lt;!&gt; count ???? city ??? country</span><br></pre></td></tr></table></figure><p>To apply a function over an <code>Option</code> we can use the operator <code>&lt;!&gt;</code> (<code>map</code>), but what about the other two parameters?</p><p>Let me rephrase, what happens when we apply a function that takes three parameters to just one parameter? Exactly! A partial application!</p><p>Same happens when applying the operator <code>&lt;!&gt;</code> to a function that takes three parameters, the difference is that the partially applied function gets <em>boxed</em> in an <code>Option</code>.</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> count = req.tryGetParam <span class="string">"count"</span> &gt;&gt;= Int32.tryParse </span><br><span class="line"></span><br><span class="line">findCustomers &lt;!&gt; count ... <span class="comment">// returns an Option&lt;int -&gt; int -&gt; Customer list&gt;</span></span><br></pre></td></tr></table></figure><p>Now we need to apply the boxed function to a boxed value, and for that we can use the operator <code>&lt;*&gt;</code> that takes a boxed function and a boxed value and returns the boxed version of applying the function to the value.</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">findCustomers &lt;!&gt; count &lt;*&gt; city  ... <span class="comment">// partial application of the function to city</span></span><br></pre></td></tr></table></figure><p>In this case we have two more parameters so the full version would be:</p><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> findCustomers count city country = [] </span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> doWebRequest (req:Request) =</span><br><span class="line">  <span class="keyword">let</span> city    = req.tryGetParam <span class="string">"city"</span></span><br><span class="line">  <span class="keyword">let</span> country = req.tryGetParam <span class="string">"country"</span></span><br><span class="line">  <span class="keyword">let</span> count   = req.tryGetParam <span class="string">"count"</span> &gt;&gt;= Int32.tryParse </span><br><span class="line"></span><br><span class="line">  findCustomers &lt;!&gt; count &lt;*&gt; city &lt;*&gt; country</span><br><span class="line">  |&gt; Option.map Success</span><br><span class="line">  |&gt; Option.getOrElse (invalidRequest |&gt; Error)</span><br></pre></td></tr></table></figure><h2>Summary</h2><p>Using an <code>Option</code> to represent when a value may not be present has many advantages.</p><p>Not only is easier to deal with cases that produce no results, but also the code is clear an easy to follow.</p><p>Though here the code is in F# you could implement similar features in your favourite language.</p><p>(<em>Check out Java 8 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html" target="_blank" rel="noopener">Optional class</a> or this <a href="https://github.com/adamkrieger/FSBetter/blob/master/functions/0.2_Maybe.csx" target="_blank" rel="noopener">C# implementation of Maybe</a> by <a href="https://twitter.com/AdamKrieger" target="_blank" rel="noopener">Adam Krieger</a></em>).</p><p>All the code can be found <a href="https://github.com/amirci/option-sample" target="_blank" rel="noopener">here</a>. I included a series of tests that show how the builder and applicative style apply a shortcircuit when one of the parameters is missing.</p><p>Thanks to my good friend <a href="https://twitter.com/Dead_Stroke" target="_blank" rel="noopener">Shane</a> for helping me to test the code in all platforms.</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;&lt;a href=&quot;http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Tony Hoare&lt;/a&gt; calls &lt;em&gt;null references&lt;/em&gt; his &lt;strong&gt;billion dollar mistake&lt;/strong&gt;. Using &lt;code&gt;null&lt;/code&gt; values (&lt;code&gt;NULL&lt;/code&gt;, &lt;code&gt;Null&lt;/code&gt;, &lt;code&gt;nil&lt;/code&gt;, etc) makes code harder to maintain and to understand.&lt;/p&gt;
&lt;p&gt;But what can we do about it? To start let&#39;s review the meaning of &lt;code&gt;null&lt;/code&gt; values ...&lt;/p&gt;
    
    </summary>
    
      <category term="Fsharp" scheme="https://westerndevs.com/categories/Fsharp/"/>
    
      <category term="Functional programming" scheme="https://westerndevs.com/categories/Fsharp/Functional-programming/"/>
    
    
      <category term="design" scheme="https://westerndevs.com/tags/design/"/>
    
      <category term="functional" scheme="https://westerndevs.com/tags/functional/"/>
    
      <category term="fsharp" scheme="https://westerndevs.com/tags/fsharp/"/>
    
  </entry>
  
  <entry>
    <title type="html">Idiomatic Iterative Design</title>
    <link href="https://westerndevs.com/Design/idiomatic-iterative-design/" rel="alternate" type="text/html"/>
    <id>https://westerndevs.com/Design/idiomatic-iterative-design/</id>
    <published>2016-03-23T20:00:00.000Z</published>
    <updated>2025-11-05T18:40:02.083Z</updated>
	<author>
	
	  
	  <name>Amir Barylko</name>
	  <email>amir@barylko.com</email>
	
	  <uri>https://westerndevs.com</uri>
	</author>
    
    <content type="html"><![CDATA[<p>Lately I have been having fun solving the <a href="http://adventofcode.com" target="_blank" rel="noopener">AdventOfCode</a>. I mainly used <em>Haskell</em> to solve each day so I can learn a bit about <em>Haskell</em> and as a byproduct <em>VIM</em> as well.</p><p>In the last <a href="http://winnipegrb.org" target="_blank" rel="noopener">Ruby Meetup</a> we used <a href="http://adventofcode.com/day/7" target="_blank" rel="noopener">Day 7</a> to illustrate how to use <a href="https://github.com/abargnesi/rantly" target="_blank" rel="noopener">Rantly</a> for properties testing.</p><p>It was my first try to solve <em>Day 7</em> using <a href="https://github.com/amirci/adventofcode_rb" target="_blank" rel="noopener">Ruby</a>, and I wanted to find an <em>elegant</em>, <em>idiomatic</em>, <em>short</em> way to do it...</p><a id="more"></a><h2>Before we start</h2><p>First I want to give a very big shout out to <a href="http://was.tl/" target="_blank" rel="noopener">Eric Wastl</a> for creating the <a href="http://adventofcode.com/" target="_blank" rel="noopener">Advent Of Code</a>.</p><p>Try it out, and if you like it let <a href="https://twitter.com/ericwastl" target="_blank" rel="noopener">Eric</a> know!</p><h2>Exploring the problem</h2><p>SPOILER ALERT: Yes, we are going to talk about <em>Day 7</em> and how to implement the solution. Feel free to do it on your own first.</p><p>The problem describes a circuit board with instructions to apply signals to circuits using bitwise logic operations.</p><p>The operations are:</p><pre><code>- 123 -&gt; x means that the signal 123 is provided to wire x.- x AND y -&gt; z means that the bitwise AND of wire x and wire y   is provided to wire z.- p LSHIFT 2 -&gt; q means that the value from wire p is left-shifted by 2   and then provided to wire q.- NOT e -&gt; f means that the bitwise complement of the value from   wire e is provided to wire f.Other possible gates include OR (bitwise OR) and RSHIFT (right-shift).</code></pre><p>I implemented the solution in <a href="https://github.com/amirci/adventofcode_hs" target="_blank" rel="noopener">Haskell</a> and found out that not only the parsing was the challenge but also the fact that you get a list of instructions that can be in any order, so some instructions when evaluated may result in having no signal (no value).</p><p>For example, let's consider the following sequence of instructions:</p><pre><code>lx -&gt; a456 -&gt; lx</code></pre><p>When evaluating the first instruction, the wire <code>lx</code> has no signal yet, so it has to be reevaluated later.</p><p>Of course you could create an evaluation tree but that seemed a bit too much for the problem at hand. So I decided to do the following:</p><ol><li>Parse instructions into commands</li><li>Repeat evaluating commands until all pass</li><li>Return the value of wire &quot;a&quot; from the board</li></ol><h2>The <em>classy</em> way</h2><p>As soon as I read about <em>instructions</em> my mind started to race thinking about <em>parsing</em> and <em>patterns</em>.</p><p>My first thought was I could use the <a href="https://en.wikipedia.org/wiki/Interpreter_pattern" target="_blank" rel="noopener">Interpreter Pattern</a> to build a hierarchy of classes to evaluate expressions. But it seemed like an overkill.</p><p>Therefore, I decided to use classes to represent each command:</p><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AndCmd</span>    ;</span> <span class="keyword">end</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">OrCmd</span>     ;</span> <span class="keyword">end</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LShiftCmd</span> ;</span> <span class="keyword">end</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">RShiftCmd</span> ;</span> <span class="keyword">end</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">NotCmd</span>    ;</span> <span class="keyword">end</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">EvalCmd</span>   ;</span> <span class="keyword">end</span></span><br></pre></td></tr></table></figure><p>Each <em>class</em> has two methods with clear responsibilities:</p><ul><li><p><code>parse(token1, token2, token3..., wire)</code> Class factory method that parses the command and returns an instance of the command.</p></li><li><p><code>wireIt(board)</code> Instance method that evaluates the command to assign the result of the operation to the target wire.</p></li></ul><p>The constructor of the class stores the operands and target wire.</p><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AndCmd</span></span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">initialize</span><span class="params">(lhs, rhs, wire)</span></span> ; @lhs, @rhs, @wire = [lhs, rhs, wire] <span class="keyword">end</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">wireIt</span><span class="params">(board)</span></span> </span><br><span class="line">    <span class="comment"># asign the <span class="doctag">@lhs</span> &amp; <span class="doctag">@rhs</span> to the board</span></span><br><span class="line">  <span class="keyword">end</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">self</span>.<span class="title">parse</span><span class="params">(....)</span></span></span><br><span class="line">    <span class="comment"># parse the string to match the AND command and return a new instance</span></span><br><span class="line">  <span class="keyword">end</span></span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure><p>The <code>parse</code> factory method receives the expected tokens. If the <code>cmd</code> token matches the string <code>&quot;AND&quot;</code> then returns an instance of <code>AndCmd</code>.</p><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">self</span>.<span class="title">parse</span><span class="params">(x, cmd, y, arrow, z)</span></span></span><br><span class="line">  AndCmd.new(x, y, z) <span class="keyword">if</span> cmd == <span class="string">"AND"</span></span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure><h3>Abstracting the board</h3><p>Evaluating the command was more complex because the values could be undefined. Some kind of validation was necessary.</p><p>I started using a <code>Hash</code> as a <em>CircuitBoard</em> and then checking if the values were defined. It got a bit more complicated when I realized I had to parse values, because I could get <code>lx AND lb</code> and also <code>1 AND ll</code>.</p><p>Inspired by my <em>Haskell</em> solution I thought that a class that implements a <em>short circuit</em> evaluation could be very useful. That way, if one of the involved values was not defined the whole command was undefined.</p><p>To simplify board access and evaluation I created a <code>Board</code> class that handles the assignment plus the lookup.</p><p>The <code>assign</code> method takes a block that gets evaluated if all the values are defined, otherwise it is ignored.</p><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Board</span></span></span><br><span class="line">  <span class="keyword">attr_reader</span> <span class="symbol">:wires</span>                                           </span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">initialize</span> ;</span> @wires = &#123;&#125; <span class="keyword">end</span>                             </span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">[]</span><span class="params">(y)</span></span> ; @wires[y] <span class="keyword">end</span></span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">assign</span><span class="params">(wire, *exprs)</span></span>                                     </span><br><span class="line">    values = exprs.map &#123; <span class="params">|exp|</span> value exp &#125;                     </span><br><span class="line">    <span class="keyword">return</span> <span class="literal">nil</span> <span class="keyword">if</span> values.any?(&amp;<span class="symbol">:nil?</span>)·                         </span><br><span class="line">    @wires[wire] = block_given? ? <span class="keyword">yield</span>(*values) : values[<span class="number">0</span>]   </span><br><span class="line">  <span class="keyword">end</span>                                                          </span><br><span class="line"></span><br><span class="line">  private·                                                     </span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">value</span><span class="params">(exp)</span></span>                                               </span><br><span class="line">    /\d+<span class="regexp">/.match(exp) ? exp.to_i : @wires[exp]                  </span></span><br><span class="line"><span class="regexp">  end                                                          </span></span><br><span class="line"><span class="regexp">end</span></span><br></pre></td></tr></table></figure><p>This simplifies things quite a bit. Now the <code>wireIt</code> implementation only applies the operation when all the values are defined:</p><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">wireIt</span><span class="params">(board)</span></span></span><br><span class="line">  board.assign(@wire, @lhs, @rhs) &#123; <span class="params">|l, r|</span> l &amp; r &#125;</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure><h3>Parsing instructions into commands</h3><p>To parse the string I created a <code>parse</code> method that splits the instruction into tokens (words) and finds a parsing factory method with the same amount of parameters that returns an actual instance of the command.</p><p>This is similar to a <a href="https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern" target="_blank" rel="noopener">chain of responsibility</a> where the parser tries to parse the instruction. If the parser cannot do it then the parser passes the instruction to the next parser in the chain until one of them succeeds.  If no parser succeeds, <code>nil</code> is returned.</p><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">parse</span><span class="params">(instruction)</span></span>                                              </span><br><span class="line">  tokens = instruction.split                                        </span><br><span class="line">  [AssignCmd, AndCmd, OrCmd, RightShiftCmd, LeftShiftCmd, NotCmd]   </span><br><span class="line">    .map    &#123; <span class="params">|k|</span> k.method(<span class="symbol">:parse</span>) &#125;                                </span><br><span class="line">    .select &#123; <span class="params">|m|</span> m.parameters.length == tokens.length &#125;            </span><br><span class="line">    .map    &#123; <span class="params">|m|</span> m.call(*tokens) &#125;                                 </span><br><span class="line">    .find   &#123; <span class="params">|cmd|</span> cmd &#125;·                                          </span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure><h3>The main loop</h3><p>The last bit of the exercise is to keep evaluating all commands until there are no failing commands left.</p><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">wire</span><span class="params">(instructions)</span></span>                                  </span><br><span class="line">  cmds = instructions.map(&amp;method(<span class="symbol">:parse</span>))              </span><br><span class="line">  board = Board.new                                     </span><br><span class="line">  <span class="keyword">while</span> !cmds.empty?                                    </span><br><span class="line">    cmds = cmds.select &#123; <span class="params">|cmd|</span> cmd.wireIt(board).<span class="literal">nil</span>? &#125; </span><br><span class="line">  <span class="keyword">end</span>                                                   </span><br><span class="line">  board                                                 </span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure><p>You can see the complete source <a href="https://github.com/amirci/adventofcode_rb/blob/master/lib/day7_v2.rb" target="_blank" rel="noopener">here</a>.</p><h2>The <em>Ruby</em> way</h2><p>After finishing the implementation using classes I started to wonder what would be more idiomatic to <em>Ruby</em>.</p><p>Classes are fine, but I wanted to explore the <em>dynamic</em> aspect of <em>Ruby</em> and use <code>eval</code>.</p><h3>Parsing instructions</h3><p>Instead of having a <code>Class</code> factory method to parse, I declared <code>parse_xxx</code> functions that return a string to be evaluated later for the expected command.</p><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">board</span><span class="params">(exp)</span></span> ; <span class="string">"board['<span class="subst">#&#123;exp&#125;</span>']"</span> <span class="keyword">end</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">expr</span><span class="params">(exp)</span></span> ; <span class="regexp">/\d+/</span>.match(exp) ? exp : board(exp) <span class="keyword">end</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">parse_and</span><span class="params">(x, cmd, y, arrow, z)</span></span> </span><br><span class="line">  cmd == <span class="string">"AND"</span> &amp;&amp; <span class="string">"<span class="subst">#&#123;board z&#125;</span> = <span class="subst">#&#123;expr x&#125;</span> &amp; <span class="subst">#&#123;board y&#125;</span>"</span> </span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure><p>The main <code>parse</code> function now uses the <code>parse_xxx</code> functions instead. The parse chooses the function that has the same amount of parameters as the tokens in the instructions and also returns a string with the expression to be evaluated.</p><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">parse</span><span class="params">(instruction)</span></span></span><br><span class="line">  tokens = instruction.split</span><br><span class="line">  [<span class="symbol">:parse_assign</span>, <span class="symbol">:parse_and</span>, <span class="symbol">:parse_or</span>, <span class="symbol">:parse_rshift</span>, <span class="symbol">:parse_lshift</span>, <span class="symbol">:parse_not</span>]</span><br><span class="line">  .map    &#123; <span class="params">|s|</span> method(s) &#125;</span><br><span class="line">  .select &#123; <span class="params">|m|</span> m.parameters.length == tokens.length &#125;                           </span><br><span class="line">  .map    &#123; <span class="params">|m|</span> m.call(*tokens) &#125;</span><br><span class="line">  .find   &#123; <span class="params">|cmd|</span> cmd &#125;</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure><h3>The main loop</h3><p>The main loop is very similar to the main loop of the <em>classy version</em> with the difference that to get the actual value, <code>eval</code> is called for every command. If the command succeeds, the evaluation will return some kind of number.</p><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">wire</span><span class="params">(instructions)</span></span>                                                     </span><br><span class="line">  cmds = instructions.map &#123; <span class="params">|line|</span> <span class="keyword">self</span>.parse line &#125;                       </span><br><span class="line">  board = &#123;&#125;                                                               </span><br><span class="line">  <span class="keyword">while</span> !cmds.empty?·                                                      </span><br><span class="line">    cmds = cmds.reject &#123; <span class="params">|cmd|</span> (eval(cmd) <span class="keyword">rescue</span> <span class="literal">nil</span>).kind_of? Fixnum &#125;    </span><br><span class="line">  <span class="keyword">end</span>                                                                      </span><br><span class="line">  board                                                                    </span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure><h3>The result</h3><p>The solution seems simpler than using classes. The strings make each command quite transparent.</p><p>Probably, even the parsing could be combined into one function and reduce the amount of functions in general.</p><p>You can see the complete solution <a href="https://github.com/amirci/adventofcode_rb/blob/master/lib/day7_v3.rb" target="_blank" rel="noopener">here</a>.</p><h2>The <em>functional</em> way</h2><p>After the <em>Classy</em> and <em>Ruby</em> way I wanted to see if I could be a bit more functional.</p><p>The approach this time was to parse the instruction into a <code>lambda</code> that will evaluate the actual command.</p><p>I decided to reuse the <code>Board</code> class from the first solution to make the value evaluation easier and implement a short circuit when a value is not yet defined.</p><h3>Parsing instructions into commands</h3><p>Third time’s the charm! I reduced the amount of parsing functions by having a binary parsing function that uses a hash to decide which operation to apply.</p><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">parse_bin</span><span class="params">(x, cmd, y, arrow, wire)</span></span>                                 </span><br><span class="line">  op = &#123;<span class="string">"AND"</span> =&gt; <span class="symbol">:&amp;</span>, <span class="string">"OR"</span> =&gt; <span class="symbol">:|</span>, <span class="string">"LSHIFT"</span> =&gt; <span class="symbol">:&lt;&lt;</span>, <span class="string">"RSHIFT"</span> =&gt; <span class="symbol">:&gt;&gt;</span>&#125;[cmd]</span><br><span class="line">  op &amp;&amp; -&gt; (board) &#123; board.assign(wire, x, y, &amp;op) &#125;</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure><p>The hash lookup  decides which operation to use.</p><p>The general <code>parse</code> function is similar to the <em>Ruby</em> way:</p><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">parse</span><span class="params">(instruction)</span></span></span><br><span class="line"> tokens = instruction.split</span><br><span class="line"> [<span class="symbol">:parse_assign</span>, <span class="symbol">:parse_bin</span>, <span class="symbol">:parse_not</span>]</span><br><span class="line">   .map    &#123; <span class="params">|s|</span> method(s) &#125;</span><br><span class="line">   .select &#123; <span class="params">|m|</span> m.parameters.length == tokens.length &#125;</span><br><span class="line">   .map    &#123; <span class="params">|m|</span> m.call(*tokens) &#125;</span><br><span class="line">   .find   &#123; <span class="params">|cmd|</span> cmd &#125;</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure><h3>The main loop</h3><p>Instead of using <code>eval</code>, the <code>call</code> method is used on each <code>lambda</code> to evaluate the command.</p><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">wire</span><span class="params">(instructions)</span></span></span><br><span class="line">  cmds = instructions.map &#123; <span class="params">|line|</span> parse line &#125;</span><br><span class="line">  board = Board.new</span><br><span class="line">  <span class="keyword">while</span> !cmds.empty?</span><br><span class="line">    cmds = cmds.select &#123; <span class="params">|cmd|</span> cmd.call(board).<span class="literal">nil</span>? &#125;</span><br><span class="line">  <span class="keyword">end</span></span><br><span class="line">  board</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure><p>You can see the entire solution <a href="https://github.com/amirci/adventofcode_rb/blob/master/lib/day7_v4.rb" target="_blank" rel="noopener">here</a>.</p><h2>The verdict</h2><p>The version I like the most is the <code>eval</code> version because it is simple and straightforward.</p><p>The second best option, in my opinion, is the <em>functional</em> version because it uses  the bitwise logic operators as functions.</p><p>Last but not least is the <em>classy</em> version. Using instances of classes does not seem to be necessary for this exercise because all parameters can be captured when creating the <code>lambda</code> closure for each instruction.</p><p>Which one is would <strong>you</strong> choose?</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;Lately I have been having fun solving the &lt;a href=&quot;http://adventofcode.com&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;AdventOfCode&lt;/a&gt;. I mainly used &lt;em&gt;Haskell&lt;/em&gt; to solve each day so I can learn a bit about &lt;em&gt;Haskell&lt;/em&gt; and as a byproduct &lt;em&gt;VIM&lt;/em&gt; as well.&lt;/p&gt;
&lt;p&gt;In the last &lt;a href=&quot;http://winnipegrb.org&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Ruby Meetup&lt;/a&gt; we used &lt;a href=&quot;http://adventofcode.com/day/7&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Day 7&lt;/a&gt; to illustrate how to use &lt;a href=&quot;https://github.com/abargnesi/rantly&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Rantly&lt;/a&gt; for properties testing.&lt;/p&gt;
&lt;p&gt;It was my first try to solve &lt;em&gt;Day 7&lt;/em&gt; using &lt;a href=&quot;https://github.com/amirci/adventofcode_rb&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Ruby&lt;/a&gt;, and I wanted to find an &lt;em&gt;elegant&lt;/em&gt;, &lt;em&gt;idiomatic&lt;/em&gt;, &lt;em&gt;short&lt;/em&gt; way to do it...&lt;/p&gt;
    
    </summary>
    
      <category term="Design" scheme="https://westerndevs.com/categories/Design/"/>
    
    
      <category term="design" scheme="https://westerndevs.com/tags/design/"/>
    
      <category term="ruby" scheme="https://westerndevs.com/tags/ruby/"/>
    
      <category term="functional" scheme="https://westerndevs.com/tags/functional/"/>
    
  </entry>
  
  <entry>
    <title type="html">Self generating data</title>
    <link href="https://westerndevs.com/_/property-testing/" rel="alternate" type="text/html"/>
    <id>https://westerndevs.com/_/property-testing/</id>
    <published>2015-10-12T08:39:34.000Z</published>
    <updated>2025-11-05T18:40:02.103Z</updated>
	<author>
	
	  
	  <name>Amir Barylko</name>
	  <email>amir@barylko.com</email>
	
	  <uri>https://westerndevs.com</uri>
	</author>
    
    <content type="html"><![CDATA[<p>Every week I meet for <a href="http://www.meetup.com/wpgcoffeecode" target="_blank" rel="noopener">Code and Coffee</a> with other devs to chat about all kind of topics (often related to software) and lately we have been doing Katas from <a href="http://codewars.com" target="_blank" rel="noopener">CodeWars</a> under the <em>WpgDotNet</em> clan.</p><p>This time around I was working with <a href="https://twitter.com/QuinnWilson" target="_blank" rel="noopener">@QuinnWilson</a> and <a href="https://twitter.com/AdamKrieger" target="_blank" rel="noopener">@AdamKrieger</a> doing the <a href="http://www.codewars.com/kata/highest-and-lowest" target="_blank" rel="noopener">Highest and lowest</a> Kata using Ruby and <a href="http://rspec.info" target="_blank" rel="noopener">RSpec</a>.</p><p>Is a simple Kata but I wanted to put focus on TDD, self data generation and property testing...</p><a id="more"></a><h2>The scenarios</h2><p>I won't go into the TDD steps because I want to fast forward to data generation.</p><p>We created three tests, from the domain of the problem first we selected strings that contain only one number then the result should be that number as max and min.</p><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">context <span class="string">'When the string has only one number'</span> <span class="keyword">do</span>                       </span><br><span class="line">  it <span class="string">'Returns the same number twice'</span> <span class="keyword">do</span>                                </span><br><span class="line">    str = <span class="string">"1"</span>                                                          </span><br><span class="line">    expect(highest_lowest str).to eq <span class="string">"1 1"</span>                             </span><br><span class="line">  <span class="keyword">end</span>                                                                  </span><br><span class="line"><span class="keyword">end</span>   </span><br></pre></td></tr></table></figure><p>For our second scenario we thought that it would be easy if the sequence of numbers where already sorted, so the min and max are the <code>first</code> and <code>last</code>.</p><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">context <span class="string">'When the numbers are sorted'</span> <span class="keyword">do</span>                               </span><br><span class="line">  it <span class="string">'returns the last and the first'</span> <span class="keyword">do</span>                               </span><br><span class="line">    str = <span class="string">'-5 -2 8 12 32 150'</span>                                          </span><br><span class="line">    expect(highest_lowest str).to eq <span class="string">"150 -5"</span>                          </span><br><span class="line">  <span class="keyword">end</span>                                                                  </span><br><span class="line"><span class="keyword">end</span>                                                                    </span><br></pre></td></tr></table></figure><p>The last scenario includes all the strings with sequence of integers.</p><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">context <span class="string">'For any string with one or more numbers separated by space'</span> <span class="keyword">do</span></span><br><span class="line">  it <span class="string">'returns the max and the min'</span> <span class="keyword">do</span>                                  </span><br><span class="line">    str = <span class="string">'22 -5 28 -294 33 1794 10000 2'</span>                              </span><br><span class="line">    expect(highest_lowest str).to eq <span class="string">"10000 -294"</span>                      </span><br><span class="line">  <span class="keyword">end</span>                                                                  </span><br><span class="line"><span class="keyword">end</span>                                                                    </span><br></pre></td></tr></table></figure><p>Ruby has a <code>minmax</code> method on <code>Array</code> but we wanted to do it using a <code>fold</code>. So here is our implementation:</p><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">highest_lowest</span><span class="params">(str)</span></span></span><br><span class="line">  str.split.map(&amp;<span class="symbol">:to_i</span>).inject([MIN, MAX]) <span class="keyword">do</span> <span class="params">|mm, i|</span></span><br><span class="line">    [[mm.first, i].max, [mm.last, i].min]</span><br><span class="line">  <span class="keyword">end</span>.join <span class="string">' '</span></span><br><span class="line"><span class="keyword">end</span>                                                </span><br></pre></td></tr></table></figure><h2>Self generating data</h2><p>We did write only one example on each scenario to start but now we wanted to cover more cases.</p><p>Not only I want to generate random data but I want to have a reasonable distribution of values anddescribe it as a property that the function has to satisfy.</p><p>Libraries like <a href="https://hackage.haskell.org/package/QuickCheck" target="_blank" rel="noopener">QuickCheck</a> (for <em>Haskell</em>, main inspiration to others), <a href="https://fscheck.github.io/FsCheck/" target="_blank" rel="noopener">FsCheck</a> (for <em>F#</em>) and <a href="https://www.scalacheck.org" target="_blank" rel="noopener">ScalaCheck</a> (for <em>Scala</em>) do exactly that. For Ruby we used <a href="https://github.com/hayeah/rantly" target="_blank" rel="noopener">Rantly</a>.</p><h3>Single integer</h3><p>The first scenario requires a string with one <em>integer</em>.</p><p>To do that with <em>Rantly</em> we can use the <em>integer</em> generator <code>integer</code>. After converting it to a <code>string</code> we have the input for the test.</p><p>Imagine a property that looks like:</p><pre><code>Given a string with a single integer NThe result is a string like &quot;{N} {N}&quot;</code></pre><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">it <span class="string">'Returns the same number twice'</span> <span class="keyword">do</span></span><br><span class="line">  property_of &#123;</span><br><span class="line">    n = integer</span><br><span class="line">    n.to_s</span><br><span class="line">  &#125;.check <span class="keyword">do</span> <span class="params">|str, n|</span></span><br><span class="line">    expect(highest_lowest str).to eq <span class="string">"<span class="subst">#&#123;n&#125;</span> <span class="subst">#&#123;n&#125;</span>"</span></span><br><span class="line">  <span class="keyword">end</span></span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure><p><em>Rantly</em> is going to generate 100 cases by default and <em>check</em> to make sure the equality holds for all of them.</p><h3>Sorted integers</h3><p>Having a sorted sequence of numbers is easy to specify where the min and max should be.</p><p>So the property could be something like (in pseudo formal but not so much english):</p><pre><code>Given an ordered string of integersThen the first is the MIN and the last is the MAXAnd the result is a string like &quot;{MAX} {MIN}&quot;    </code></pre><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">context <span class="string">'When the numbers are sorted'</span> <span class="keyword">do</span></span><br><span class="line">  it <span class="string">'returns the last and the first'</span> <span class="keyword">do</span></span><br><span class="line">    property_of &#123;</span><br><span class="line">      arr = array &#123; integer &#125;.sort</span><br><span class="line">      [arr.join(<span class="string">' '</span>), arr.last, arr.first]</span><br><span class="line">    &#125;.check &#123; <span class="params">|str, max, min|</span></span><br><span class="line">      expect(highest_lowest str).to eq <span class="string">"<span class="subst">#&#123;max&#125;</span> <span class="subst">#&#123;min&#125;</span>"</span></span><br><span class="line">    &#125;</span><br><span class="line">  <span class="keyword">end</span></span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure><h3>The general case</h3><p>Thinking of the actual <em>postcondition</em> of the problem, the property could be something like:</p><pre><code>Given any non empty collection of integersAnd exist MIN and MAX that belong to the collectionWhere MAX is the maximum and MIN is the minimumThen the result is a string like &quot;{MAX} {MIN}&quot;</code></pre><p>Considering for a moment the domain (all the possible instances) of <em>any non empty collection of integers</em>. That would include sets of a single element and also sorted elements, thus we cover the other two scenarios.</p><p>I want to generate all the integers but at the same time have the max and min so I don't write the test duplicating the implementation to find maximum and minimum.</p><p>To do that, I will generate first the min and max and then add integers in between.</p><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">it <span class="string">'returns the max and the min'</span> <span class="keyword">do</span></span><br><span class="line">  property_of &#123;</span><br><span class="line">    min, max = [integer, integer].minmax</span><br><span class="line">    arr = array &#123; range min, max &#125; + [max, min]</span><br><span class="line">    [arr.shuffle.join(<span class="string">' '</span>), max, min]</span><br><span class="line">  &#125;.check(<span class="number">200</span>) <span class="keyword">do</span> <span class="params">|str, max, min|</span></span><br><span class="line">    expect(highest_lowest str).to eq <span class="string">"<span class="subst">#&#123;max&#125;</span> <span class="subst">#&#123;min&#125;</span>"</span></span><br><span class="line">  <span class="keyword">end</span></span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure><p>I changed the number of cases to <em>200</em> just to illustrate how to override the default.</p><h2>Using properties</h2><p>Properties can be very useful to help identify test cases plus data generation makes much easier to describe a scenario and find a domain for the test.</p><p>Having a combination of both <em>property testing</em> and <em>example testing</em> can be a good balance to have tests that are accurate and also descriptive.</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;Every week I meet for &lt;a href=&quot;http://www.meetup.com/wpgcoffeecode&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Code and Coffee&lt;/a&gt; with other devs to chat about all kind of topics (often related to software) and lately we have been doing Katas from &lt;a href=&quot;http://codewars.com&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;CodeWars&lt;/a&gt; under the &lt;em&gt;WpgDotNet&lt;/em&gt; clan.&lt;/p&gt;
&lt;p&gt;This time around I was working with &lt;a href=&quot;https://twitter.com/QuinnWilson&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;@QuinnWilson&lt;/a&gt; and &lt;a href=&quot;https://twitter.com/AdamKrieger&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;@AdamKrieger&lt;/a&gt; doing the &lt;a href=&quot;http://www.codewars.com/kata/highest-and-lowest&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Highest and lowest&lt;/a&gt; Kata using Ruby and &lt;a href=&quot;http://rspec.info&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;RSpec&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Is a simple Kata but I wanted to put focus on TDD, self data generation and property testing...&lt;/p&gt;
    
    </summary>
    
    
  </entry>
  
  <entry>
    <title type="html">BDD vs TDD</title>
    <link href="https://westerndevs.com/_/bdd-vs-tdd/" rel="alternate" type="text/html"/>
    <id>https://westerndevs.com/_/bdd-vs-tdd/</id>
    <published>2015-08-11T11:09:40.000Z</published>
    <updated>2025-11-05T18:40:02.097Z</updated>
	<author>
	
	  
	  <name>Amir Barylko</name>
	  <email>amir@barylko.com</email>
	
	  <uri>https://westerndevs.com</uri>
	</author>
    
    <content type="html"><![CDATA[<p>Testing is a very important part of software development, but should we do black box testing or test every line of code?</p><p>How can we find balance between writing the right thing (BDD) and writing things right (TDD)?</p><a id="more"></a><h2>A perfect world</h2><p><em>Test Driven Development</em> (TDD) makes you think. It’s not only about the <em>test first approach</em> but also about following some <a href="http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd" target="_blank" rel="noopener">rules</a>.</p><p>TDD, however, is not enough to ensure that you are building the features that address the client’s expectations.</p><p><em>Behaviour Driven Development</em> (BDD) makes you think about the whole feature. Owners/champions participation helps you write the acceptance criteria and drive the implementation until the feature is implemented.</p><p>When you combine <em>BDD</em> and <em>TDD</em> you get the best of both worlds: you are able to match your clients’ expectations with high quality code and write the minimum amount of code possible.</p><p><img src="http://orthocoders.com/images/bdd_cycle.jpg" alt=""></p><p>Clearly both BDD and TDD are useful, however, is not always easy to implement both.</p><p>Ideally, to find balance you need to make sure you are well versed in both BDD and TDD, so you can be objective and, in the words of a great wizard, choose <a href="http://www.goodreads.com/quotes/701025-dark-times-lie-ahead-of-us-and-there-will-be" target="_blank" rel="noopener">between what is easy and what is right</a>.That means we need to overcome two major drawbacks (to start): the <strong>learning curve</strong> and <strong>poor tooling</strong>.</p><h3>Learning curve</h3><p>As with any new methodology both TDD and BDD share a big drawback: <em>the learning curve</em>. Not only you need to learn how to implement each of them but also how to combine them.</p><p>This part is <strong>crucial</strong> to find balance between these methodologies. If your team doesn't feel comfortable doing either and see it as a <em>drag</em> they will resist using them or will implement only the bit they can do quickly and find an excuse not to use the other methodology.</p><p>Online you can find multiple examples of how to use both BDD and TDD, but they are often simplified and don't deal with more complex and frequent problems such as populating databases, generating testing data, making sure the acceptance cases run on multiple browsers, brittle tests, etc.</p><p>Don't get discouraged because you don't get results as quickly as you thought you would. Implementing BDD and TDD is going to take time, but it is very, very, very important (can't stress it enough) to make sure that writing tests is as natural for your team as <em>using the turn signal when you drive</em>. You just do it, don't think if it is worth it or not, it is automatic.</p><p>Many of these problems in your path, find you will… mitigate them you shall...</p><p>What’s the secret?  Training, practice, exercise and...then more practice.  There are great <a href="https://pragprog.com/book/hwcuc/the-cucumber-book" target="_blank" rel="noopener">books</a> to help you follow the process and <em>tooling</em> is essential.</p><h3>The right tool for the job</h3><p>In my experience, one of the main reasons developers get discouraged to write tests is <em>poor tooling</em>.</p><p>I think we are past finding testing frameworks and runners, still we have areas that are not always covered.</p><h4>Testing hooks &amp; metadata</h4><p>Most frameworks you may choose to work with will have some kind of <em>hooks</em>. Before tests, after tests, before all tests, etc.</p><p>Being able to <em>tag</em> tests and identify which ones are using the database, is another great feature to have.</p><p><a href="http://rspec.info/" target="_blank" rel="noopener">RSpec</a> is a great tool to take a peek at what kind of features could be useful for your current need. Probably there are similar libraries with the language you work with that implement the same or similar ideas.</p><p>And if not, why not collaborate with the community and build some?</p><h4>Fake domain data</h4><p>It is very common to need dummy data to support your scenarios. They can be useful for acceptance testing and for unit testing as well.</p><p>Having an easy way to generate fake data is really important. It will simplify how you write your tests and also give you the feeling that you are talking in domain terms, making much easier to understand what the test is about and implement the code.</p><p>Libraries like <a href="https://github.com/thoughtbot/factory_girl" target="_blank" rel="noopener">Factory Girl</a>,  <a href="https://fscheck.github.io/FsCheck/" target="_blank" rel="noopener">FsCheck</a>, <a href="https://github.com/AutoFixture" target="_blank" rel="noopener">AutoFixture</a>, <a href="https://github.com/MisterJames/GenFu" target="_blank" rel="noopener">GenFu</a> are great examples to know what to look for.</p><p>Not only you can generate good customers, bad customers, etc., but you can also generate multiple cases to test and, even further, why not look into some automatic generation.</p><h4>Databases</h4><p>Working with databases should be simple and straightforward: loading data before the tests, cleaning after, restoring the schema (if you have one) to a certain point, and populating.</p><p>Combining database manipulation with hooks and data generation will give you lots of power to set up scenarios in an easy, painless way.</p><h2>The importance of <em>NOT</em> being <em>brittle</em></h2><p>Let’s imagine we have mastered the <em>tools</em> and <em>the learning curve</em>. Now we have a new foe that threatens our Ninja skills and may bring our confidence to the floor: <strong>Brittle tests</strong>.</p><p>Tests have to be maintained and kept running <em>green</em> all the time, otherwise defeats the purpose.</p><p>You want your tests to be (somehow) <em>resilient</em> to changes in the code. This is a more advanced problem and here are a few tips to check before we can talk about balance.</p><h3>Isolation</h3><p>Avoid assuming a particular state that you don't control. Tests may run now but may not run in the future.</p><p>Any state needed to make the test pass has to be set up before the test runs and torn down after the test finishes running.</p><h3>Implementation coupling</h3><p>Tests should be about <em>inputs</em> and <em>outputs</em>, not about how they are implemented. Testing a method that queries the database by mocking the way the query is implemented is <strong>dangerous</strong>.  Similarly, testing with a very small set of data may lead you to the wrong conclusion.</p><p>You will be better off focusing on <a href="http://fsharpforfunandprofit.com/posts/property-based-testing" target="_blank" rel="noopener"><em>Properties Testing</em></a>. Libraries like <a href="https://fscheck.github.io/FsCheck/" target="_blank" rel="noopener">FsCheck</a> and <a href="https://github.com/abargnesi/rantly" target="_blank" rel="noopener">Rantly</a> can help you enforce pre-conditions and post-conditions and inspire you to look for other ways to make your testing robust.</p><h3>Mocking the mock</h3><p>Tests should work for you, not the other way around. If you find yourself creating more code in order to make your current code <em>testable</em> that deserves some attention.</p><p>Whenever you postpone testing complexity and replace it with a mock remember that the same complexity will have to be tested later.</p><p>Creating interfaces to improve reusability is a great idea, however, make sure you need it.For example, abstracting the <a href="/repository-nightmares/">repository</a> pattern may not be always a good idea.</p><h3>Tools galore</h3><p>Though there's a plethora of tools to choose from, they will not always come in the exact shape and color you need them.</p><p>Don't get discouraged! Learn from it and build your own.</p><p>For example, writing domain specific languages to help you test will ease the pain of repeating common scenarios again and again. The investment will pay off.</p><h2>Finding balance</h2><p>Now our team has overcome the hardships of testing and is quite confident implementing both <em>TDD</em> and <em>BDD</em>. They can do them with their eyes closed while dancing the Macarena.</p><p>So what's the right balance? For me it is <strong>confidence</strong>.</p><pre><code>Do I need to write tests for every domain model, every single scenario, every api call?</code></pre><p>Well...it is up to you.</p><p>I lean on the <strong>acceptance</strong> side often to ensure everything works as expected.</p><p>If you have a senior team then perhaps you can relax a bit more and choose which unit tests should be implemented to find a balance between BDD and TDD that you feel comfortable with.</p><p>If you don't see crystal clear that writing one test will imply that the other part is working, then don't skip anything.</p><p>Even for acceptance test if you find features that are really similar to others and strongly believe that implementing test scenarios won’t be necessary because your unit/integration test already covers that, go ahead and skip them.</p><p>It is a fine line, but the key is <strong>confidence</strong>.</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;Testing is a very important part of software development, but should we do black box testing or test every line of code?&lt;/p&gt;
&lt;p&gt;How can we find balance between writing the right thing (BDD) and writing things right (TDD)?&lt;/p&gt;
    
    </summary>
    
    
  </entry>
  
  <entry>
    <title type="html">Repository nightmares</title>
    <link href="https://westerndevs.com/_/repository-nightmares/" rel="alternate" type="text/html"/>
    <id>https://westerndevs.com/_/repository-nightmares/</id>
    <published>2015-06-24T06:17:24.000Z</published>
    <updated>2025-11-05T18:40:02.103Z</updated>
	<author>
	
	  
	  <name>Amir Barylko</name>
	  <email>amir@barylko.com</email>
	
	  <uri>https://westerndevs.com</uri>
	</author>
    
    <content type="html"><![CDATA[<p>The <code>Repository</code> pattern is a famous (or infamous?) pattern that we can find in Martin Fowler's <em><a href="http://martinfowler.com/eaaCatalog/repository.html" target="_blank" rel="noopener">Patterns of Enterprise Application Architecture</a></em>.</p><p>It was meant to be used as an interface to a collection, but what I have seen more often is that it becomes an abstraction to the data layer or ORM framework.</p><p>Not so long ago I did a presentation on <em><a href="http://www.slideshare.net/amirbarylko/who-killed-object-oriented-design" target="_blank" rel="noopener">Who killed object oriented programming</a></em>, and I mentioned the <code>Repository</code> implementation as one of the culprits.</p><p>So what's so bad about it? What can we do to improve it?</p><p>I have a counter-proposal: What if we don't need it at all?</p><a id="more"></a><h2>Repository overpopulation</h2><p>Have you ever felt that the <code>Repositories</code> in your code replicate at night? You remember that when you started the project you had one or two, but now the number has grown to almost one per data model.</p><p>I remember starting with something like this:</p><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title">ICustomerRepository</span> &#123;</span><br><span class="line">  <span class="function">IEnumerable&lt;Customer&gt; <span class="title">GetCustomers</span>(<span class="params"></span>)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>And then I needed to add queries such as find by <code>Address</code>, find by <code>Id</code> and find which customers have pending invoices:</p><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title">ICustomerRepository</span> &#123;</span><br><span class="line">  <span class="function">IEnumerable&lt;Customer&gt; <span class="title">FindByAddress</span>(<span class="params">Address address</span>)</span>;</span><br><span class="line">  <span class="function">IEnumerable&lt;Customer&gt; <span class="title">FindById</span>(<span class="params">CustomerId id</span>)</span>;</span><br><span class="line">  <span class="function">IEnumerable&lt;Customer&gt; <span class="title">FindWithPendingInvoices</span>(<span class="params"></span>)</span>;    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>Once one was completed, I replicated the recipe for each model: <code>Invoice</code>, <code>Car</code>, <code>Chair</code>, etc.</p><h3>Eliminating the repetition</h3><p>Maybe we don't need one per model. What if we could use a generic class?</p><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public interface IRepository&lt;T&gt; &#123;</span><br><span class="line">  <span class="function">IEnumerable&lt;T&gt; <span class="title">GetAll</span>(<span class="params"></span>)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>Now that's slightly better, but what happened to the queries?</p><h3>Extracting custom queries</h3><p>Instead of adding queries following the business rules and modifying the <code>Repository</code> each time, why not use <code>IQueryable</code> instead? That way we can combine the results with further queries.</p><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public interface IRepository&lt;out T&gt; &#123;</span><br><span class="line">  <span class="function">IQueryable&lt;T&gt; <span class="title">GetAll</span>(<span class="params"></span>)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>Then we just need to combine the filtering and ordering after:</p><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> customers = ... <span class="comment">// instantiate a concrete IRepository&lt;Customer&gt;</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">return</span> customers</span><br><span class="line">    .Where(c =&gt; c.Address != <span class="literal">null</span>)</span><br><span class="line">    .OrderBy(c =&gt; c.Name)</span><br><span class="line">    .Take(<span class="number">100</span>);</span><br></pre></td></tr></table></figure><h3>Common queries</h3><p>We can easily put this logic (if it's really commonly used) into an extension method (or another class) to avoid repeating the same query and to capture complexity.</p><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">class</span> <span class="title">CustomerQueries</span> &#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> IQueryable&lt;Customer&gt; <span class="title">WithAddress</span>(<span class="params"><span class="keyword">this</span> IQueryable&lt;Customer&gt; customers</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> customers</span><br><span class="line">        .Where(c =&gt; c.Address != <span class="literal">null</span>)</span><br><span class="line">        .OrderBy(c =&gt; c.Name)</span><br><span class="line">        .Take(<span class="number">100</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>This would allow us to reuse the query when needed:</p><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> customers = ... <span class="comment">// instantiate a concrete IRepository&lt;Customer&gt;</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">return</span> customers.WithAddress();</span><br></pre></td></tr></table></figure><h2>Abstracted abstraction</h2><p>We’ve done a great job (pat on the back) so far! Now it’s much simpler...so simple that when we look at the implementation, we see we are just using the ORM (NHibernate, Entity Framework, etc.) to return the main collection and nothing else.</p><p>The result in this case is that the <code>Repository</code> pattern is abstracting us from another abstraction! What are we gaining from this?</p><p>Why not just use the ORM? Are we planning on changing ORMs in the middle on the project?</p><p>We should take advantage of all the power that the ORM is giving us. We don't want to have to copy the ability to do joins, sorting, etc.</p><p>Weeping...is cathartic...</p><h2>What about testing?</h2><p>Another reason to introduce the <code>Repository</code> could be testing.</p><p>But what are we going to test? Should we be testing the method <code>GetAll</code>?</p><p>Are we planning to mock <code>IQueryable</code>? Or, even further, mock all the methods that come with the ORM session?</p><p>The logic is mostly in the queries or, for example, in controllers doing queries in a web application.</p><p>Unit tests won't work in this case. We don't want to depend on which methods are used or in which order they’re used, so we need to make sure the inputs are defined and write assertions on the outputs enforcing the pre- and post-condition.</p><p>This case calls for integration tests (small tests setting up the database before and cleaning it up after) to help us ensure it works as expected.</p><h2>Summary</h2><p>Make your life simpler and your work more enjoyable. Abstractions should make the code easier to read. If that is not the case, then it is time to reflect and review.</p><p>Perhaps it is a bit late to change the project we are working on right now, but next time let’s make sure we give quite a bit more thought to using the <code>Repository</code> pattern.</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;The &lt;code&gt;Repository&lt;/code&gt; pattern is a famous (or infamous?) pattern that we can find in Martin Fowler&#39;s &lt;em&gt;&lt;a href=&quot;http://martinfowler.com/eaaCatalog/repository.html&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Patterns of Enterprise Application Architecture&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;It was meant to be used as an interface to a collection, but what I have seen more often is that it becomes an abstraction to the data layer or ORM framework.&lt;/p&gt;
&lt;p&gt;Not so long ago I did a presentation on &lt;em&gt;&lt;a href=&quot;http://www.slideshare.net/amirbarylko/who-killed-object-oriented-design&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Who killed object oriented programming&lt;/a&gt;&lt;/em&gt;, and I mentioned the &lt;code&gt;Repository&lt;/code&gt; implementation as one of the culprits.&lt;/p&gt;
&lt;p&gt;So what&#39;s so bad about it? What can we do to improve it?&lt;/p&gt;
&lt;p&gt;I have a counter-proposal: What if we don&#39;t need it at all?&lt;/p&gt;
    
    </summary>
    
    
  </entry>
  
</feed>
